checkpoint, mostly working

This commit is contained in:
Joe Ardent 2024-07-24 22:08:16 -07:00
parent e4c491768c
commit 68449f2b6e
3 changed files with 40 additions and 24 deletions

View File

@ -37,13 +37,14 @@ fn spawn_bike(
let bike = commands let bike = commands
.spawn(SpatialBundle::from_transform(xform)) .spawn(SpatialBundle::from_transform(xform))
.insert(( .insert((
Name::new("body"),
RigidBody::Dynamic, RigidBody::Dynamic,
body_collider, body_collider,
CollisionLayers::from_bits(1, 1), CollisionLayers::from_bits(1, 1),
SleepingDisabled, SleepingDisabled,
CyberBikeBody, CyberBikeBody,
CatControllerState::default(), CatControllerState::default(),
ColliderDensity(0.12), ColliderDensity(0.4),
LinearDamping(0.1), LinearDamping(0.1),
AngularDamping(2.0), AngularDamping(2.0),
LinearVelocity::ZERO, LinearVelocity::ZERO,
@ -75,8 +76,8 @@ fn spawn_wheels(
xform: Transform, xform: Transform,
body: Entity, body: Entity,
) { ) {
let rake_vec = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees let front_rake = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees
let front_pos = xform.translation + rake_vec; let front_pos = xform.translation + front_rake;
let (mesh, collider) = gen_tire(); let (mesh, collider) = gen_tire();
@ -91,10 +92,12 @@ fn spawn_wheels(
commands.entity(front_hub).insert(FrontHub); commands.entity(front_hub).insert(FrontHub);
commands.spawn(( commands.spawn((
Steering, Steering,
FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + rake_vec), FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + front_rake),
)); ));
let rear_pos = xform.translation + Vec3::new(0.0, -1.0, 0.57).normalize(); let rear_rake = Vec3::new(0.0, -1.0, 0.57).normalize();
let rear_pos = xform.translation + rear_rake;
let rear_hub = wheels_helper( let rear_hub = wheels_helper(
&mut commands, &mut commands,
&mut meshes, &mut meshes,
@ -106,7 +109,7 @@ fn spawn_wheels(
commands.entity(rear_hub).insert(RearHub); commands.entity(rear_hub).insert(RearHub);
commands.spawn(( commands.spawn((
Rearing, Rearing,
FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rake_vec.y), FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rear_rake),
)); ));
} }
@ -115,7 +118,7 @@ fn spawn_wheels(
//-************************************************************************ //-************************************************************************
fn gen_tire() -> (Mesh, Collider) { fn gen_tire() -> (Mesh, Collider) {
let mut tire_mesh: Mesh = Torus::new(0.30, 0.40).into(); let mut tire_mesh: Mesh = Torus::new(0.25, 0.40).into();
let tire_verts = tire_mesh let tire_verts = tire_mesh
.attribute(Mesh::ATTRIBUTE_POSITION) .attribute(Mesh::ATTRIBUTE_POSITION)
.unwrap() .unwrap()
@ -133,7 +136,7 @@ fn gen_tire() -> (Mesh, Collider) {
tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION); tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION);
tire_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, tire_verts); tire_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, tire_verts);
let collider = Collider::convex_decomposition_from_mesh(&tire_mesh).unwrap(); let collider = Collider::convex_hull_from_mesh(&tire_mesh).unwrap();
(tire_mesh, collider) (tire_mesh, collider)
} }
@ -157,9 +160,10 @@ fn wheels_helper(
let hub = commands let hub = commands
.spawn(( .spawn((
Name::new("hub"),
SpatialBundle::from_transform(xform), SpatialBundle::from_transform(xform),
RigidBody::Dynamic, RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 0.01), MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 8.0),
)) ))
.with_children(|parent| { .with_children(|parent| {
let mesh: Mesh = Sphere::new(0.1).into(); let mesh: Mesh = Sphere::new(0.1).into();
@ -173,13 +177,18 @@ fn wheels_helper(
let tire = commands let tire = commands
.spawn(( .spawn((
Name::new("tire"),
SpatialBundle::from_transform(xform), SpatialBundle::from_transform(xform),
CyberWheel, CyberWheel,
RigidBody::Dynamic, RigidBody::Dynamic,
collider.clone(), collider,
ColliderDensity(0.05), Friction::new(3.0).with_dynamic_coefficient(0.9),
Restitution::new(0.1),
ColliderDensity(0.1),
CollisionLayers::from_bits(2, 2), CollisionLayers::from_bits(2, 2),
ExternalTorque::ZERO.with_persistence(false), ExternalTorque::ZERO.with_persistence(false),
CollisionMargin(0.05),
SweptCcd::NON_LINEAR,
)) ))
.with_children(|p| { .with_children(|p| {
p.spawn(PbrBundle { p.spawn(PbrBundle {

View File

@ -41,16 +41,22 @@ fn ground_and_light(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
commands.spawn(( commands
RigidBody::Static, .spawn((
Collider::cuboid(50.0, 0.5, 50.0), RigidBody::Static,
PbrBundle { Collider::cuboid(50.0, 0.5, 50.0),
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)), CollisionMargin(0.1),
material: materials.add(Color::from(SILVER)), ColliderDensity(1000.0),
transform: Transform::from_xyz(0.0, -3., 0.0), Friction::new(3.0),
..default() ))
}, .with_children(|p| {
)); p.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
material: materials.add(Color::from(SILVER)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
});
commands.spawn(PointLightBundle { commands.spawn(PointLightBundle {
point_light: PointLight { point_light: PointLight {

View File

@ -18,9 +18,9 @@ pub struct CatControllerSettings {
impl Default for CatControllerSettings { impl Default for CatControllerSettings {
fn default() -> Self { fn default() -> Self {
Self { Self {
kp: 10.0, kp: 20.0,
kd: 1.2, kd: 2.0,
ki: 0.2, ki: 0.7,
} }
} }
} }
@ -174,6 +174,7 @@ impl Plugin for CyberPhysicsPlugin {
.init_resource::<CyberLean>() .init_resource::<CyberLean>()
.register_type::<CyberLean>() .register_type::<CyberLean>()
.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default())) .add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()))
.insert_resource(SubstepCount(12))
.add_systems(Update, (apply_lean, calculate_lean)); .add_systems(Update, (apply_lean, calculate_lean));
} }
} }