checkpoint

This commit is contained in:
Joe Ardent 2023-01-22 16:10:59 -08:00
parent f65282bb08
commit cf449d9e56
4 changed files with 23 additions and 31 deletions

View file

@ -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<Diagnostics>,
) {
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<CyberBikeBody>>) {
@ -113,10 +107,8 @@ fn drag(mut query: Query<(&Velocity, &mut ExternalForce), With<CyberBikeBody>>)
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;

View file

@ -61,8 +61,7 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
};
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<AssetServer>) {
.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<AssetServer>) {
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<AssetServer>) {
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,

View file

@ -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);

View file

@ -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() {