From ef190538ff268319f53b7501e451e450d9701534 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Mon, 28 Jul 2025 20:12:56 -0700 Subject: [PATCH] Preserve velocities --- src/bin/flappy/main.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index affcc82..566a2e6 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -94,6 +94,8 @@ enum PlayerState { struct Tape { translations: Vec, rotations: Vec, + linear_velocities: Vec, + angular_velocities: Vec, } fn init_bird( @@ -119,7 +121,6 @@ fn init_bird( RigidBody::Static, Collider::capsule(1.0, 1.0), Mass(1.0), - ExternalForce::new(Vec3::X * 1.0).with_persistence(true), ExternalImpulse::default().with_persistence(false), LockedAxes::ROTATION_LOCKED.lock_translation_z(), ); @@ -271,7 +272,7 @@ fn flap( ); 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>, mut next: ResMut>, - mut birds: Query<(&Transform, &mut Tape), With>, + mut birds: Query<(&Transform, &LinearVelocity, &AngularVelocity, &mut Tape), With>, ) { debug_assert!( matches!(state.get(), PlayerState::Alive), "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.rotations.push(transform.rotation); + tape.linear_velocities.push(*linear_velocity); + tape.angular_velocities.push(*angular_velocity); }); } fn rewind( #[cfg(debug_assertions)] state: Res>, - mut birds: Query<(&mut Transform, &mut Tape), With>, + mut birds: Query<(&mut Transform, &mut AngularVelocity, &mut LinearVelocity, &mut Tape), With>, ) { debug_assert!( matches!(state.get(), PlayerState::Rewind), "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() { transform.translation = t; } if let Some(r) = tape.rotations.pop() { 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; + } }); }