Preserve velocities

main
Elijah Voigt 3 months ago
parent 8d891e74d0
commit ef190538ff

@ -94,6 +94,8 @@ enum PlayerState {
struct Tape { struct Tape {
translations: Vec<Vec3>, translations: Vec<Vec3>,
rotations: Vec<Quat>, rotations: Vec<Quat>,
linear_velocities: Vec<LinearVelocity>,
angular_velocities: Vec<AngularVelocity>,
} }
fn init_bird( fn init_bird(
@ -119,7 +121,6 @@ fn init_bird(
RigidBody::Static, RigidBody::Static,
Collider::capsule(1.0, 1.0), Collider::capsule(1.0, 1.0),
Mass(1.0), Mass(1.0),
ExternalForce::new(Vec3::X * 1.0).with_persistence(true),
ExternalImpulse::default().with_persistence(false), ExternalImpulse::default().with_persistence(false),
LockedAxes::ROTATION_LOCKED.lock_translation_z(), LockedAxes::ROTATION_LOCKED.lock_translation_z(),
); );
@ -271,7 +272,7 @@ fn flap(
); );
bird.iter_mut().for_each(|(t, mut f)| { bird.iter_mut().for_each(|(t, mut f)| {
f.apply_impulse(t.rotation * Vec3::NEG_Z * 5.0); f.apply_impulse(t.rotation * Vec3::NEG_Z * 5.0 + t.rotation * Vec3::X);
}); });
} }
@ -292,35 +293,43 @@ fn toggle_rewind(keycode: Res<ButtonInput<KeyCode>>, mut next: ResMut<NextState<
fn record( fn record(
#[cfg(debug_assertions)] state: Res<State<PlayerState>>, #[cfg(debug_assertions)] state: Res<State<PlayerState>>,
mut birds: Query<(&Transform, &mut Tape), With<Bird>>, mut birds: Query<(&Transform, &LinearVelocity, &AngularVelocity, &mut Tape), With<Bird>>,
) { ) {
debug_assert!( debug_assert!(
matches!(state.get(), PlayerState::Alive), matches!(state.get(), PlayerState::Alive),
"Only record in the alive state" "Only record in the alive state"
); );
birds.iter_mut().for_each(|(transform, mut tape)| { birds.iter_mut().for_each(|(transform, linear_velocity, angular_velocity, mut tape)| {
tape.translations.push(transform.translation); tape.translations.push(transform.translation);
tape.rotations.push(transform.rotation); tape.rotations.push(transform.rotation);
tape.linear_velocities.push(*linear_velocity);
tape.angular_velocities.push(*angular_velocity);
}); });
} }
fn rewind( fn rewind(
#[cfg(debug_assertions)] state: Res<State<PlayerState>>, #[cfg(debug_assertions)] state: Res<State<PlayerState>>,
mut birds: Query<(&mut Transform, &mut Tape), With<Bird>>, mut birds: Query<(&mut Transform, &mut AngularVelocity, &mut LinearVelocity, &mut Tape), With<Bird>>,
) { ) {
debug_assert!( debug_assert!(
matches!(state.get(), PlayerState::Rewind), matches!(state.get(), PlayerState::Rewind),
"Only rewind in the rewinding state" "Only rewind in the rewinding state"
); );
birds.iter_mut().for_each(|(mut transform, mut tape)| { birds.iter_mut().for_each(|(mut transform, mut angular_velocity, mut linear_velocity, mut tape)| {
if let Some(t) = tape.translations.pop() { if let Some(t) = tape.translations.pop() {
transform.translation = t; transform.translation = t;
} }
if let Some(r) = tape.rotations.pop() { if let Some(r) = tape.rotations.pop() {
transform.rotation = r; transform.rotation = r;
} }
if let Some(av) = tape.angular_velocities.pop() {
*angular_velocity = av;
}
if let Some(lv) = tape.linear_velocities.pop() {
*linear_velocity = lv;
}
}); });
} }

Loading…
Cancel
Save