Tweak some physics, try to reduce pitch stabilization.

This commit is contained in:
Joe Ardent 2023-02-18 13:49:06 -08:00
parent 4a80554740
commit 2933dca90e
4 changed files with 25 additions and 17 deletions

View file

@ -85,7 +85,7 @@ impl Default for CatControllerState {
pitch_prev: Default::default(), pitch_prev: Default::default(),
decay_factor: 0.99, decay_factor: 0.99,
roll_limit: 1.5, roll_limit: 1.5,
pitch_limit: 0.2, pitch_limit: 0.1,
} }
} }
} }

View file

@ -48,10 +48,10 @@ pub(super) fn falling_cat(
let wright = xform.forward().cross(wup).normalize(); let wright = xform.forward().cross(wup).normalize();
let roll_error = wright.dot(bike_up); let roll_error = wright.dot(bike_up);
let pitch_error = wup.dot(xform.back()) * PITCH_SCALE; let pitch_error = wup.dot(xform.back());
// only try to correct roll if we're not totally vertical // only try to correct roll if we're not totally vertical
if pitch_error.abs() < 0.8 { if pitch_error.abs() < 0.95 {
let (derivative, integral) = control_vars.update_roll(roll_error, time.delta_seconds()); let (derivative, integral) = control_vars.update_roll(roll_error, time.delta_seconds());
let mag = let mag =
(settings.kp * roll_error) + (settings.ki * integral) + (settings.kd * derivative); (settings.kp * roll_error) + (settings.ki * integral) + (settings.kd * derivative);
@ -61,17 +61,20 @@ pub(super) fn falling_cat(
} }
// we can do pitch correction any time, it's not coupled to roll // we can do pitch correction any time, it's not coupled to roll
let (derivative, integral) = control_vars.update_pitch(pitch_error, time.delta_seconds()); let scaled_pitch_error = pitch_error * PITCH_SCALE;
let mag = (settings.kp * pitch_error) + (settings.ki * integral) + (settings.kd * derivative); let (derivative, integral) =
control_vars.update_pitch(scaled_pitch_error, time.delta_seconds());
let mag =
(settings.kp * scaled_pitch_error) + (settings.ki * integral) + (settings.kd * derivative);
if mag.is_finite() { if mag.is_finite() {
forces.torque += wright * mag * 0.25; forces.torque += wright * mag;
} }
#[cfg(feature = "inspector")] #[cfg(feature = "inspector")]
{ {
let now = Instant::now(); let now = Instant::now();
if (now - debug_instant.0).as_millis() > 500 { if (now - debug_instant.0).as_millis() > 1000 {
dbg!(roll_error, pitch_error, &control_vars, mag); dbg!(&control_vars, mag);
debug_instant.0 = now; debug_instant.0 = now;
} }
} }
@ -99,7 +102,7 @@ pub(super) fn input_forces(
// brake // brake
for mut motor in braking_query.iter_mut() { for mut motor in braking_query.iter_mut() {
let factor = if input.brake { 100.00 } else { 0.0 }; let factor = if input.brake { 200.00 } else { 0.0 };
motor.data = (*motor motor.data = (*motor
.data .data
.as_revolute_mut() .as_revolute_mut()

View file

@ -23,6 +23,8 @@ pub struct WheelConfig {
pub stiffness: f32, pub stiffness: f32,
pub damping: f32, pub damping: f32,
pub radius: f32, pub radius: f32,
pub friction: f32,
pub restitution: f32,
} }
impl Default for WheelConfig { impl Default for WheelConfig {
@ -36,6 +38,8 @@ impl Default for WheelConfig {
stiffness: 90.0, stiffness: 90.0,
damping: 8.0, damping: 8.0,
radius: 0.3, radius: 0.3,
friction: 0.9,
restitution: 0.8,
} }
} }
} }

View file

@ -47,8 +47,8 @@ pub fn spawn_tires(
}; };
let friction = Friction { let friction = Friction {
coefficient: 0.8, coefficient: conf.friction,
combine_rule: CoefficientCombineRule::Min, combine_rule: CoefficientCombineRule::Average,
}; };
let mut wheel_poses = Vec::with_capacity(2); let mut wheel_poses = Vec::with_capacity(2);
@ -86,24 +86,25 @@ pub fn spawn_tires(
let fork_rb_entity = commands let fork_rb_entity = commands
.spawn(RigidBody::Dynamic) .spawn(RigidBody::Dynamic)
.insert(prismatic_joint) .insert(prismatic_joint)
.insert(not_sleeping)
.id(); .id();
let axel_parent = if let Some(steering) = steering { let axel_parent_entity = if let Some(steering) = steering {
let neck_builder = RevoluteJointBuilder::new(Vec3::Y); let neck_builder = RevoluteJointBuilder::new(Vec3::Y);
let neck_joint = MultibodyJoint::new(fork_rb_entity, neck_builder); let neck_joint = MultibodyJoint::new(fork_rb_entity, neck_builder);
let hub = commands let neck = commands
.spawn(RigidBody::Dynamic) .spawn(RigidBody::Dynamic)
.insert(neck_joint) .insert(neck_joint)
.insert(steering) .insert(steering)
.insert(not_sleeping)
.id(); .id();
dbg!(hub); neck
hub
} else { } else {
fork_rb_entity fork_rb_entity
}; };
let revolute_builder = RevoluteJointBuilder::new(Vec3::X); let revolute_builder = RevoluteJointBuilder::new(Vec3::X);
let axel_joint = MultibodyJoint::new(axel_parent, revolute_builder); let axel_joint = MultibodyJoint::new(axel_parent_entity, revolute_builder);
commands.spawn(pbr_bundle.clone()).insert(( commands.spawn(pbr_bundle.clone()).insert((
wheel_collider, wheel_collider,
@ -116,7 +117,7 @@ pub fn spawn_tires(
friction, friction,
CyberWheel, CyberWheel,
ExternalForce::default(), ExternalForce::default(),
Restitution::new(0.2), Restitution::new(conf.restitution),
SpatialBundle::default(), SpatialBundle::default(),
TransformInterpolation::default(), TransformInterpolation::default(),
RigidBody::Dynamic, RigidBody::Dynamic,