From 52cac4a3eba5b948d8358adab0304a234c902ac9 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Thu, 1 May 2025 15:45:12 -0700 Subject: [PATCH] tweak; apply suspension force at contact point, not caster origin --- src/bike.rs | 31 +++++++++++++++---------------- src/physics.rs | 32 ++++++++++++++------------------ 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/bike.rs b/src/bike.rs index 0e8a4d8..4e819e5 100644 --- a/src/bike.rs +++ b/src/bike.rs @@ -92,6 +92,19 @@ fn spawn_bike( let body_collider = Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8)); + let pbr_bundle = { + let color = Color::srgb(0.7, 0.05, 0.7); + let mut xform = Transform::default(); + xform.rotate_x(FRAC_PI_2); + ( + Mesh3d(meshes.add(Capsule3d::new(0.5, 1.45))), + xform, + Visibility::Visible, + MeshMaterial3d(materials.add(color)), + TransformInterpolation, + ) + }; + let mut body = commands.spawn((xform, Visibility::default())); body.insert(( Name::new("body"), @@ -108,25 +121,12 @@ fn spawn_bike( ExternalTorque::ZERO.with_persistence(false), )); - let pbr_bundle = { - let color = Color::srgb(0.7, 0.05, 0.7); - let mut xform = Transform::default(); - xform.rotate_x(FRAC_PI_2); - ( - Mesh3d(meshes.add(Capsule3d::new(0.5, 1.45))), - xform, - Visibility::Visible, - MeshMaterial3d(materials.add(color)), - TransformInterpolation, - ) - }; - let parent = body.id(); - body.insert(spawn_children(parent, pbr_bundle, meshes, materials)); + body.insert(spawn_children(body.id(), meshes, materials)) + .with_child(pbr_bundle); } fn spawn_children( parent: Entity, - body: impl Bundle, mut meshes: ResMut>, mut materials: ResMut>, ) -> impl Bundle { @@ -147,7 +147,6 @@ fn spawn_children( let rear_wheel_pos = REAR_ATTACH + (rear_rake * REST_DISTANCE); children![ - body, ( RayCaster::new(FRONT_ATTACH, Dir3::new_unchecked(front_rake)) .with_max_hits(1) diff --git a/src/physics.rs b/src/physics.rs index f75c372..c26b8b8 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -20,9 +20,9 @@ pub struct CatControllerSettings { impl Default for CatControllerSettings { fn default() -> Self { Self { - kp: 55.0, - kd: 20.0, - ki: 12.5, + kp: 60.0, + kd: 30.0, + ki: 0.0, } } } @@ -260,11 +260,7 @@ mod systems { hit_point + force, Color::linear_rgb(1., 0.5, 0.2), ); - bike_forces.apply_force_at_point( - force, - caster.global_origin(), - bike_xform.translation + com.0, - ); + bike_forces.apply_force_at_point(force, hit_point, bike_xform.translation + com.0); } else { xform.translation = config.attach + (caster.direction.as_vec3() * config.rest_dist); state.reset(); @@ -272,7 +268,7 @@ mod systems { } } - pub(super) fn steering( + pub(super) fn wheel_action( bike_query: Single< ( &Transform, @@ -291,7 +287,7 @@ mod systems { let bike_vel = bike_vel.0; let dt = time.delta().as_secs_f32(); - let max_thrust = 1000.0; + let max_thrust = 500.0; let yaw_angle = -yaw_to_angle(input.yaw); for (mut state, config, wheel) in wheels.iter_mut() { @@ -304,8 +300,8 @@ mod systems { let forward = normal.cross(*bike_xform.right()); let thrust_mag = input.throttle * max_thrust * dt; let (thrust_dir, thrust_force) = match wheel { - CyberWheel::Rear => (forward, thrust_mag * 0.5), - CyberWheel::Front => (rot * forward, thrust_mag), + CyberWheel::Rear => (forward, thrust_mag), + CyberWheel::Front => (rot * forward, thrust_mag * 0.1), }; let thrust = thrust_force * thrust_dir; @@ -368,11 +364,11 @@ mod systems { let dt = time.delta_secs(); - let speed = vel.length(); - let dspeed = speed.powf(1.2) * 0.01; + let speed = vel.length_squared(); + let dspeed = speed * 0.0000001; let dir = vel.normalize_or_zero(); let drag = -dspeed * dt * DRAG_COEFF * dir; - if speed > 1.0 { + if speed > 0.1 { force.apply_force_at_point( drag, xform.translation - (0.2 * *xform.down()), @@ -380,7 +376,7 @@ mod systems { ); } - bevy::log::debug!("speed: {}, drag force: {}", speed, drag.length()); + bevy::log::debug!("speed: {}, drag force: {}", vel.length(), drag.length()); gizmos.arrow( xform.translation, @@ -427,7 +423,7 @@ mod systems { } } -use systems::{apply_lean, calculate_lean, drag, steering, suspension, tweak}; +use systems::{apply_lean, calculate_lean, drag, suspension, tweak, wheel_action}; pub struct CyberPhysicsPlugin; @@ -442,7 +438,7 @@ impl Plugin for CyberPhysicsPlugin { }) .add_systems( FixedUpdate, - (calculate_lean, apply_lean, suspension, steering, drag).chain(), + (calculate_lean, apply_lean, suspension, wheel_action, drag).chain(), ) .add_systems(Update, tweak); }