From 68449f2b6ecd01adc0f0cc48c32dd6af09db881b Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 24 Jul 2024 22:08:16 -0700 Subject: [PATCH] checkpoint, mostly working --- src/bike.rs | 31 ++++++++++++++++++++----------- src/main.rs | 26 ++++++++++++++++---------- src/physics.rs | 7 ++++--- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/bike.rs b/src/bike.rs index 4318574..6504a17 100644 --- a/src/bike.rs +++ b/src/bike.rs @@ -37,13 +37,14 @@ fn spawn_bike( let bike = commands .spawn(SpatialBundle::from_transform(xform)) .insert(( + Name::new("body"), RigidBody::Dynamic, body_collider, CollisionLayers::from_bits(1, 1), SleepingDisabled, CyberBikeBody, CatControllerState::default(), - ColliderDensity(0.12), + ColliderDensity(0.4), LinearDamping(0.1), AngularDamping(2.0), LinearVelocity::ZERO, @@ -75,8 +76,8 @@ fn spawn_wheels( xform: Transform, body: Entity, ) { - let rake_vec = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees - let front_pos = xform.translation + rake_vec; + let front_rake = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees + let front_pos = xform.translation + front_rake; let (mesh, collider) = gen_tire(); @@ -91,10 +92,12 @@ fn spawn_wheels( commands.entity(front_hub).insert(FrontHub); commands.spawn(( 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( &mut commands, &mut meshes, @@ -106,7 +109,7 @@ fn spawn_wheels( commands.entity(rear_hub).insert(RearHub); commands.spawn(( 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) { - 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 .attribute(Mesh::ATTRIBUTE_POSITION) .unwrap() @@ -133,7 +136,7 @@ fn gen_tire() -> (Mesh, Collider) { tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION); 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) } @@ -157,9 +160,10 @@ fn wheels_helper( let hub = commands .spawn(( + Name::new("hub"), SpatialBundle::from_transform(xform), RigidBody::Dynamic, - MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 0.01), + MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 8.0), )) .with_children(|parent| { let mesh: Mesh = Sphere::new(0.1).into(); @@ -173,13 +177,18 @@ fn wheels_helper( let tire = commands .spawn(( + Name::new("tire"), SpatialBundle::from_transform(xform), CyberWheel, RigidBody::Dynamic, - collider.clone(), - ColliderDensity(0.05), + collider, + Friction::new(3.0).with_dynamic_coefficient(0.9), + Restitution::new(0.1), + ColliderDensity(0.1), CollisionLayers::from_bits(2, 2), ExternalTorque::ZERO.with_persistence(false), + CollisionMargin(0.05), + SweptCcd::NON_LINEAR, )) .with_children(|p| { p.spawn(PbrBundle { diff --git a/src/main.rs b/src/main.rs index c2f0489..dbc8c63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,16 +41,22 @@ fn ground_and_light( mut meshes: ResMut>, mut materials: ResMut>, ) { - commands.spawn(( - RigidBody::Static, - Collider::cuboid(50.0, 0.5, 50.0), - PbrBundle { - mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)), - material: materials.add(Color::from(SILVER)), - transform: Transform::from_xyz(0.0, -3., 0.0), - ..default() - }, - )); + commands + .spawn(( + RigidBody::Static, + Collider::cuboid(50.0, 0.5, 50.0), + CollisionMargin(0.1), + ColliderDensity(1000.0), + Friction::new(3.0), + )) + .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 { point_light: PointLight { diff --git a/src/physics.rs b/src/physics.rs index fcc334b..7ba5c83 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -18,9 +18,9 @@ pub struct CatControllerSettings { impl Default for CatControllerSettings { fn default() -> Self { Self { - kp: 10.0, - kd: 1.2, - ki: 0.2, + kp: 20.0, + kd: 2.0, + ki: 0.7, } } } @@ -174,6 +174,7 @@ impl Plugin for CyberPhysicsPlugin { .init_resource::() .register_type::() .add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default())) + .insert_resource(SubstepCount(12)) .add_systems(Update, (apply_lean, calculate_lean)); } }