From cf449d9e56b218f6cd2eb9cdacd650ce4fcb2fc7 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 22 Jan 2023 16:10:59 -0800 Subject: [PATCH] checkpoint --- src/action.rs | 24 ++++++++---------------- src/bike.rs | 20 ++++++++++---------- src/camera.rs | 4 ++-- src/main.rs | 6 +++--- 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/action.rs b/src/action.rs index 893c6d7..6860e80 100644 --- a/src/action.rs +++ b/src/action.rs @@ -45,12 +45,7 @@ fn gravity( } fn falling_cat_pid( - mut bike_query: Query<( - &Transform, - //&Velocity, - &mut ExternalForce, - &mut CyberBikeControl, - )>, + mut bike_query: Query<(&Transform, &mut ExternalForce, &mut CyberBikeControl)>, diagnostics: Res, ) { let (xform, mut forces, mut control_vars) = bike_query.single_mut(); @@ -64,18 +59,19 @@ fn falling_cat_pid( let error = 1.0 - cos; let derivative = error - control_vars.prev_error; control_vars.prev_error = error; - let integral = (control_vars.error_sum * 0.8) + error; - control_vars.error_sum = integral; + // this integral term is not an integral, it's more like a weighted moving sum + let integral = (control_vars.error_sum + error) * 0.8; + control_vars.error_sum = integral; //.min(2.0).max(-2.0); let kp = 13.1; - let ki = 6.0; - let kd = 13.1; + let ki = 1.1; + let kd = 8.1; let mag = (kp * error) + (ki * integral) + (kd * derivative); if let Some(count) = diagnostics .get(FrameTimeDiagnosticsPlugin::FRAME_COUNT) .and_then(|d| d.smoothed()) - .map(|x| x as u128) + .map(|x| x as u64) { if count % 30 == 0 { dbg!(&control_vars, mag); @@ -104,8 +100,6 @@ fn input_forces( // steering let torque = xform.down() * input.yaw * settings.sensitivity; forces.torque += torque; - - //dbg!(&input); } fn drag(mut query: Query<(&Velocity, &mut ExternalForce), With>) { @@ -113,10 +107,8 @@ fn drag(mut query: Query<(&Velocity, &mut ExternalForce), With>) if let Some(vel) = vels.linvel.try_normalize() { let v2 = vels.linvel.length_squared(); - forces.force -= vel * v2 * 0.02; + forces.force -= vel * (v2 * 0.02); } - - //dbg!(&forces); } pub struct CyberActionPlugin; diff --git a/src/bike.rs b/src/bike.rs index 4abc585..5b3632a 100644 --- a/src/bike.rs +++ b/src/bike.rs @@ -61,8 +61,7 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { }; let bike = commands - .spawn_empty() - .insert(RigidBody::Dynamic) + .spawn(RigidBody::Dynamic) .insert(spatialbundle) .insert(( bcollider_shape, @@ -92,7 +91,8 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { .insert(CyberBikeControl::default()) .id(); - let wheel_z_positions = vec![-1.2, 1.5, -1.2]; + //return; + let wheel_z_positions = vec![-1.0, 1.2, -1.0]; let wheel_y = -1.0f32; // re-set the collision group @@ -101,9 +101,9 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { for (i, &wheel_z) in wheel_z_positions.iter().enumerate() { let (wheel_x, wheel_rad, stiffness) = match i { - 0 => (-1.1, 0.5, 0.026), - 2 => (1.1, 0.5, 0.026), - 1 => (0.0, 0.8, 0.020), + 0 => (-1.1, 0.5, 2.0), + 2 => (1.1, 0.5, 2.0), + 1 => (0.0, 0.5, 1.8), _ => unreachable!(), }; let offset = Vec3::new(wheel_x, wheel_y, wheel_z); @@ -118,13 +118,13 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res) { let damping = 0.3; let prismatic = PrismaticJointBuilder::new(Vec3::Y) - .local_anchor1(offset) - .motor_position(-0.4, stiffness, damping); + .local_anchor2(offset) + .limits([-1.0, 0.9]) + .motor_position(0.0, stiffness, damping); let joint = ImpulseJoint::new(bike, prismatic); let _wheel_rb = commands - .spawn_empty() - .insert(RigidBody::Dynamic) + .spawn(RigidBody::Dynamic) .insert((wheel_pos_in_world, GlobalTransform::default())) .insert(( wheel_collider, diff --git a/src/camera.rs b/src/camera.rs index 4ed58a5..8e3937c 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -65,8 +65,8 @@ fn follow_cyberbike( } CyberCameras::Debug => { let pos = bike_xform.translation - + (bike_xform.forward() * 9.0) - + (bike_xform.left() * 20.0) + + (bike_xform.forward() * 20.0) + + (bike_xform.left() * 2.0) + (bike_xform.up() * 5.0); cam_xform.translation = pos; cam_xform.look_at(bike_xform.translation, up); diff --git a/src/main.rs b/src/main.rs index b0936f5..8ee6143 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,9 +12,9 @@ use cyber_rider::{ }; const MOVEMENT_SETTINGS: MovementSettings = MovementSettings { - sensitivity: 10.0, // steering - accel: 20.0, // thrust - gravity: 10.0, + sensitivity: 8.0, // steering + accel: 30.0, // thrust + gravity: 9.0, }; fn main() {