Bring in wheels from rolling2, and shorten the body collider.

This commit is contained in:
Joe Ardent 2023-02-15 11:38:36 -08:00
parent 18615e9ac8
commit 421f34fab7
3 changed files with 54 additions and 95 deletions

View File

@ -108,9 +108,9 @@ pub(super) fn input_forces(
// steering // steering
let angle = input.yaw * (PI / 2.0); let angle = input.yaw * (PI / 2.0);
let mut steering = steering_query.single_mut(); //let mut steering = steering_query.single_mut();
steering.rotation = Quat::from_axis_angle(Vec3::Y, angle + 90.0f32.to_radians()); //steering.rotation = Quat::from_axis_angle(Vec3::Y, angle +
//dbg!(steering.rotation); // 90.0f32.to_radians()); dbg!(steering.rotation);
} }
/// Don't let the wheels get stuck underneat the planet /// Don't let the wheels get stuck underneat the planet

View File

@ -51,7 +51,7 @@ pub(super) fn spawn_cyberbike(
.spawn(RigidBody::Dynamic) .spawn(RigidBody::Dynamic)
.insert(spatialbundle) .insert(spatialbundle)
.insert(( .insert((
Collider::capsule(Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, 0.0, 1.0), 0.50), Collider::capsule(Vec3::new(0.0, 0.0, -0.7), Vec3::new(0.0, 0.0, 1.0), 0.50),
bike_collision_group, bike_collision_group,
mass_properties, mass_properties,
damping, damping,
@ -77,5 +77,5 @@ pub(super) fn spawn_cyberbike(
.insert(CatControllerState::default()) .insert(CatControllerState::default())
.id(); .id();
spawn_tires(&mut commands, bike, xform, &wheel_conf, &mut meshterials); spawn_tires(&mut commands, &xform, bike, &wheel_conf, &mut meshterials);
} }

View File

@ -1,22 +1,16 @@
use bevy::prelude::{ use bevy::prelude::{shape::UVSphere as Tire, *};
shape::Capsule as Tire, AlphaMode, BuildChildren, Color, Commands, Entity, Mesh, PbrBundle, use bevy_rapier3d::prelude::{
Quat, SpatialBundle, StandardMaterial, Transform, Vec3, Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction,
}; MultibodyJoint, PrismaticJointBuilder, Restitution, RigidBody, Sleeping,
use bevy_rapier3d::{ TransformInterpolation,
prelude::{
Ccd, CoefficientCombineRule, Collider, ColliderMassProperties, CollisionGroups, Damping,
ExternalForce, Friction, LockedAxes, MultibodyJoint, PrismaticJointBuilder, Restitution,
RevoluteJointBuilder, RigidBody, Sleeping, TransformInterpolation,
},
render::ColliderDebugColor,
}; };
use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP}; use super::{CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP};
pub fn spawn_tires( pub fn spawn_tires(
commands: &mut Commands, commands: &mut Commands,
xform: &Transform,
bike: Entity, bike: Entity,
xform: Transform,
conf: &WheelConfig, conf: &WheelConfig,
meshterials: &mut Meshterial, meshterials: &mut Meshterial,
) { ) {
@ -53,13 +47,8 @@ pub fn spawn_tires(
}; };
let friction = Friction { let friction = Friction {
coefficient: 1.0, coefficient: 0.0,
combine_rule: CoefficientCombineRule::Min, ..Default::default()
};
let restitution = Restitution {
coefficient: 0.8,
combine_rule: CoefficientCombineRule::Average,
}; };
let mut wheel_poses = Vec::with_capacity(2); let mut wheel_poses = Vec::with_capacity(2);
@ -69,7 +58,7 @@ pub fn spawn_tires(
let wheel_x = 0.0; let wheel_x = 0.0;
let wheel_z = -conf.front_forward; let wheel_z = -conf.front_forward;
let offset = Vec3::new(wheel_x, wheel_y, wheel_z); let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
wheel_poses.push((offset, Some(CyberSteering))); wheel_poses.push(offset);
} }
// rear // rear
@ -77,89 +66,59 @@ pub fn spawn_tires(
let wheel_x = 0.0; let wheel_x = 0.0;
let wheel_z = conf.rear_back; let wheel_z = conf.rear_back;
let offset = Vec3::new(wheel_x, wheel_y, wheel_z); let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
wheel_poses.push((offset, None)); wheel_poses.push(offset);
} }
for (offset, steering) in wheel_poses { for offset in wheel_poses {
let trans = xform.translation + offset;
let wheel_pos_in_world = Transform::from_rotation(xform.rotation).with_translation(trans);
let wheel_damping = Damping { let wheel_damping = Damping {
linear_damping: 0.8, linear_damping: 0.8,
..Default::default() ..Default::default()
}; };
let wheel_collider = Collider::capsule(-Vec3::Y, Vec3::Y, 0.3); let wheel_collider = Collider::ball(wheel_rad);
let mass_props = ColliderMassProperties::Density(0.1); let mass_props = ColliderMassProperties::Density(0.1);
let damping = conf.damping; let damping = conf.damping;
let prismatic = PrismaticJointBuilder::new(Vec3::Y) let prismatic = PrismaticJointBuilder::new(Vec3::Y)
.local_anchor1(offset) .local_anchor1(offset)
.limits(limits) .limits(limits)
.motor_position(limits[0], stiffness, damping); .motor_position(-0.1, stiffness, damping);
let joint = MultibodyJoint::new(bike, prismatic);
let fork = commands let spatial_bundle = SpatialBundle {
.spawn(SpatialBundle { transform: wheel_pos_in_world,
transform: Transform::from_translation(offset), ..Default::default()
..Default::default() };
})
.set_parent(bike)
.id();
let suspension = MultibodyJoint::new(fork, prismatic); let tire_spundle = SpatialBundle {
let sentity = commands transform: Transform::IDENTITY,
..Default::default()
};
commands
.spawn(RigidBody::Dynamic) .spawn(RigidBody::Dynamic)
.insert(suspension) .insert(spatial_bundle)
.insert(SpatialBundle { .insert((
transform: Transform::from_translation(offset), wheel_collider,
..Default::default() mass_props,
}) wheel_damping,
.set_parent(bike) ccd,
.id(); not_sleeping,
joint,
if let Some(steering) = steering { wheels_collision_group,
commands.entity(sentity).insert(steering); friction,
} CyberWheel,
ExternalForce::default(),
let revolute = RevoluteJointBuilder::new(Vec3::Y); Restitution::new(0.2),
let axel = MultibodyJoint::new(sentity, revolute); ))
let wheel_bundle = ( .with_children(|wheel| {
wheel_collider, wheel.spawn(tire_spundle).insert(pbr_bundle.clone()).insert(
mass_props, TransformInterpolation {
wheel_damping, start: None,
ccd, end: None,
wheels_collision_group, },
friction, );
restitution, });
CyberWheel,
ExternalForce::default(),
not_sleeping,
ColliderDebugColor(Color::Rgba {
red: 0.1,
green: 0.1,
blue: 0.1,
alpha: 0.5,
}), //axel,
);
let _wentity = commands
.spawn(RigidBody::Dynamic)
.insert(SpatialBundle {
// transform: Transform::from_rotation(xform.rotation)
// .with_translation(xform.translation)
transform: Transform::from_rotation(Quat::from_axis_angle(
Vec3::Z,
90.0f32.to_radians(),
)),
..Default::default()
})
.insert(wheel_bundle)
// .with_children(|wheel| {
// wheel
// .spawn(SpatialBundle::default())
// .insert(pbr_bundle.clone())
// .insert(TransformInterpolation {
// start: None,
// end: None,
// });
// })
.set_parent(sentity)
.id();
} }
} }