checkpoint
This commit is contained in:
parent
f65282bb08
commit
cf449d9e56
4 changed files with 23 additions and 31 deletions
|
@ -45,12 +45,7 @@ fn gravity(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn falling_cat_pid(
|
fn falling_cat_pid(
|
||||||
mut bike_query: Query<(
|
mut bike_query: Query<(&Transform, &mut ExternalForce, &mut CyberBikeControl)>,
|
||||||
&Transform,
|
|
||||||
//&Velocity,
|
|
||||||
&mut ExternalForce,
|
|
||||||
&mut CyberBikeControl,
|
|
||||||
)>,
|
|
||||||
diagnostics: Res<Diagnostics>,
|
diagnostics: Res<Diagnostics>,
|
||||||
) {
|
) {
|
||||||
let (xform, mut forces, mut control_vars) = bike_query.single_mut();
|
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 error = 1.0 - cos;
|
||||||
let derivative = error - control_vars.prev_error;
|
let derivative = error - control_vars.prev_error;
|
||||||
control_vars.prev_error = error;
|
control_vars.prev_error = error;
|
||||||
let integral = (control_vars.error_sum * 0.8) + error;
|
// this integral term is not an integral, it's more like a weighted moving sum
|
||||||
control_vars.error_sum = integral;
|
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 kp = 13.1;
|
||||||
let ki = 6.0;
|
let ki = 1.1;
|
||||||
let kd = 13.1;
|
let kd = 8.1;
|
||||||
let mag = (kp * error) + (ki * integral) + (kd * derivative);
|
let mag = (kp * error) + (ki * integral) + (kd * derivative);
|
||||||
|
|
||||||
if let Some(count) = diagnostics
|
if let Some(count) = diagnostics
|
||||||
.get(FrameTimeDiagnosticsPlugin::FRAME_COUNT)
|
.get(FrameTimeDiagnosticsPlugin::FRAME_COUNT)
|
||||||
.and_then(|d| d.smoothed())
|
.and_then(|d| d.smoothed())
|
||||||
.map(|x| x as u128)
|
.map(|x| x as u64)
|
||||||
{
|
{
|
||||||
if count % 30 == 0 {
|
if count % 30 == 0 {
|
||||||
dbg!(&control_vars, mag);
|
dbg!(&control_vars, mag);
|
||||||
|
@ -104,8 +100,6 @@ fn input_forces(
|
||||||
// steering
|
// steering
|
||||||
let torque = xform.down() * input.yaw * settings.sensitivity;
|
let torque = xform.down() * input.yaw * settings.sensitivity;
|
||||||
forces.torque += torque;
|
forces.torque += torque;
|
||||||
|
|
||||||
//dbg!(&input);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn drag(mut query: Query<(&Velocity, &mut ExternalForce), With<CyberBikeBody>>) {
|
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() {
|
if let Some(vel) = vels.linvel.try_normalize() {
|
||||||
let v2 = vels.linvel.length_squared();
|
let v2 = vels.linvel.length_squared();
|
||||||
forces.force -= vel * v2 * 0.02;
|
forces.force -= vel * (v2 * 0.02);
|
||||||
}
|
}
|
||||||
|
|
||||||
//dbg!(&forces);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CyberActionPlugin;
|
pub struct CyberActionPlugin;
|
||||||
|
|
20
src/bike.rs
20
src/bike.rs
|
@ -61,8 +61,7 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
};
|
};
|
||||||
|
|
||||||
let bike = commands
|
let bike = commands
|
||||||
.spawn_empty()
|
.spawn(RigidBody::Dynamic)
|
||||||
.insert(RigidBody::Dynamic)
|
|
||||||
.insert(spatialbundle)
|
.insert(spatialbundle)
|
||||||
.insert((
|
.insert((
|
||||||
bcollider_shape,
|
bcollider_shape,
|
||||||
|
@ -92,7 +91,8 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
.insert(CyberBikeControl::default())
|
.insert(CyberBikeControl::default())
|
||||||
.id();
|
.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;
|
let wheel_y = -1.0f32;
|
||||||
|
|
||||||
// re-set the collision group
|
// 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() {
|
for (i, &wheel_z) in wheel_z_positions.iter().enumerate() {
|
||||||
let (wheel_x, wheel_rad, stiffness) = match i {
|
let (wheel_x, wheel_rad, stiffness) = match i {
|
||||||
0 => (-1.1, 0.5, 0.026),
|
0 => (-1.1, 0.5, 2.0),
|
||||||
2 => (1.1, 0.5, 0.026),
|
2 => (1.1, 0.5, 2.0),
|
||||||
1 => (0.0, 0.8, 0.020),
|
1 => (0.0, 0.5, 1.8),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
|
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 damping = 0.3;
|
||||||
let prismatic = PrismaticJointBuilder::new(Vec3::Y)
|
let prismatic = PrismaticJointBuilder::new(Vec3::Y)
|
||||||
.local_anchor1(offset)
|
.local_anchor2(offset)
|
||||||
.motor_position(-0.4, stiffness, damping);
|
.limits([-1.0, 0.9])
|
||||||
|
.motor_position(0.0, stiffness, damping);
|
||||||
let joint = ImpulseJoint::new(bike, prismatic);
|
let joint = ImpulseJoint::new(bike, prismatic);
|
||||||
|
|
||||||
let _wheel_rb = commands
|
let _wheel_rb = commands
|
||||||
.spawn_empty()
|
.spawn(RigidBody::Dynamic)
|
||||||
.insert(RigidBody::Dynamic)
|
|
||||||
.insert((wheel_pos_in_world, GlobalTransform::default()))
|
.insert((wheel_pos_in_world, GlobalTransform::default()))
|
||||||
.insert((
|
.insert((
|
||||||
wheel_collider,
|
wheel_collider,
|
||||||
|
|
|
@ -65,8 +65,8 @@ fn follow_cyberbike(
|
||||||
}
|
}
|
||||||
CyberCameras::Debug => {
|
CyberCameras::Debug => {
|
||||||
let pos = bike_xform.translation
|
let pos = bike_xform.translation
|
||||||
+ (bike_xform.forward() * 9.0)
|
+ (bike_xform.forward() * 20.0)
|
||||||
+ (bike_xform.left() * 20.0)
|
+ (bike_xform.left() * 2.0)
|
||||||
+ (bike_xform.up() * 5.0);
|
+ (bike_xform.up() * 5.0);
|
||||||
cam_xform.translation = pos;
|
cam_xform.translation = pos;
|
||||||
cam_xform.look_at(bike_xform.translation, up);
|
cam_xform.look_at(bike_xform.translation, up);
|
||||||
|
|
|
@ -12,9 +12,9 @@ use cyber_rider::{
|
||||||
};
|
};
|
||||||
|
|
||||||
const MOVEMENT_SETTINGS: MovementSettings = MovementSettings {
|
const MOVEMENT_SETTINGS: MovementSettings = MovementSettings {
|
||||||
sensitivity: 10.0, // steering
|
sensitivity: 8.0, // steering
|
||||||
accel: 20.0, // thrust
|
accel: 30.0, // thrust
|
||||||
gravity: 10.0,
|
gravity: 9.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in a new issue