Skip to content

Commit

Permalink
Component and system to preserve current frame physics state
Browse files Browse the repository at this point in the history
  • Loading branch information
smokku committed Nov 2, 2021
1 parent 02951a7 commit b972d71
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
9 changes: 8 additions & 1 deletion client/src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl Game for GameState {
},
render::components::Position(position),
game::systems::ForceMovement,
game::physics::PreviousPhysics::default(),
));
self.world.make_active_camera(player).unwrap();
self.world
Expand Down Expand Up @@ -220,7 +221,11 @@ impl Game for GameState {
},
)
.unwrap();
let legs = self.world.spawn((components::Legs, Parent(player)));
let legs = self.world.spawn((
components::Legs,
Parent(player),
game::physics::PreviousPhysics::default(),
));
self.world
.insert(
legs,
Expand Down Expand Up @@ -341,6 +346,8 @@ impl Game for GameState {

self.config_update();

game::physics::update_previous_physics(&mut self.world);

self.world.clear_trackers();
// self.resources.get_mut::<AppEventsQueue>().unwrap().clear();
}
Expand Down
45 changes: 45 additions & 0 deletions client/src/game/physics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,48 @@ impl PhysicsHooksWithWorld for SameParentFilter {
Some(SolverFlags::all())
}
}

#[derive(Debug)]
pub struct PreviousPhysics {
pub position: Isometry<Real>,
pub linvel: Vector<Real>,
pub angvel: AngVector<Real>,
pub force: Vector<Real>,
pub torque: AngVector<Real>,
}

impl Default for PreviousPhysics {
fn default() -> Self {
Self {
position: Isometry::identity(),
linvel: Default::default(),
angvel: Default::default(),
force: Default::default(),
torque: Default::default(),
}
}
}

pub fn update_previous_physics(world: &mut World) {
for (_entity, (mut prev, pos, vel, force)) in world
.query::<(
&mut PreviousPhysics,
Option<&RigidBodyPosition>,
Option<&RigidBodyVelocity>,
Option<&RigidBodyForces>,
)>()
.iter()
{
if let Some(pos) = pos {
prev.position = pos.position;
}
if let Some(vel) = vel {
prev.linvel = vel.linvel;
prev.angvel = vel.angvel;
}
if let Some(force) = force {
prev.force = force.force;
prev.torque = force.torque;
}
}
}

0 comments on commit b972d71

Please sign in to comment.