Skip to content

Commit

Permalink
Use hecs_rapier with physics_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
smokku committed Oct 28, 2021
1 parent 58ece7e commit a5a6820
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions client/src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use gvfs::filesystem::Filesystem;
use hecs::{With, World};

pub mod components;
pub mod physics;
pub mod systems;

pub struct GameState {
Expand Down Expand Up @@ -81,13 +82,13 @@ impl GameState {
let mut narrow_phase = self.resources.get_mut::<NarrowPhase>().unwrap();
let mut ccd_solver = self.resources.get_mut::<CCDSolver>().unwrap();
let mut joint_set = self.resources.get_mut::<JointSet>().unwrap();
// let mut joints_entity_map = self.resources.get_mut::<JointsEntityMap>().unwrap();
// let physics_hooks = ();
let mut joints_entity_map = self.resources.get_mut::<JointsEntityMap>().unwrap();
const PHYSICS_HOOKS: physics::SameParentFilter = physics::SameParentFilter {};
let event_handler = ();

// TODO: make all preparations before looping necessary number of steps
attach_bodies_and_colliders(&mut self.world);
// create_joints_system();
create_joints(&mut self.world, &mut joint_set, &mut joints_entity_map);
finalize_collider_attach_to_bodies(&mut self.world, &mut modifs_tracker);

prepare_step(&mut self.world, &mut modifs_tracker);
Expand All @@ -103,6 +104,7 @@ impl GameState {
&mut narrow_phase,
&mut joint_set,
&mut ccd_solver,
&PHYSICS_HOOKS,
&event_handler,
);

Expand Down Expand Up @@ -190,8 +192,7 @@ impl Game for GameState {
Vec2::new(0., 3. / self.config.phys.scale).into(),
2. / self.config.phys.scale,
),
// shape: ColliderShape::ball(5. / self.config.phys.scale),
// position: Vec2::new(-3. / self.config.phys.scale, 0.0).into(),
flags: ColliderFlags::from(ActiveHooks::FILTER_CONTACT_PAIRS),
..Default::default()
},
)
Expand Down
37 changes: 37 additions & 0 deletions client/src/game/physics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::physics::*;
use hecs::World;

pub struct SameParentFilter;
impl PhysicsHooksWithWorld for SameParentFilter {
fn filter_contact_pair(
&self,
context: &PairFilterContext<RigidBodyComponentsSet, ColliderComponentsSet>,
world: &World,
) -> Option<SolverFlags> {
// if collider1 and collider2 have the same Parent() component
// or collider1 is Parent() of collider2
// or collider2 is Parent() of collider1
// then there is no contact

let collider1 = context.collider1.entity();
let collider2 = context.collider2.entity();
let parent1 = world.get::<Parent>(collider1).ok();
let parent2 = world.get::<Parent>(collider2).ok();

if let Some(parent1) = parent1 {
if let Some(parent2) = parent2 {
if **parent1 == **parent2 || **parent1 == collider2 || **parent2 == collider1 {
return None;
}
} else if **parent1 == collider2 {
return None;
}
} else if let Some(parent2) = parent2 {
if **parent2 == collider1 {
return None;
}
}

Some(SolverFlags::all())
}
}
2 changes: 1 addition & 1 deletion shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ clap = "2.33"
rapier2d = { version = "0.11", features = ["simd-stable"] }
nalgebra = { version = "0.29", features = ["convert-glam014"] }
glam = "0.14" # bound to gfx2d's dependency
hecs_rapier = "0.10.2"
hecs_rapier = "0.11.0"
derive_deref = "1.1"
simple-error = "0.2"

Expand Down
1 change: 1 addition & 0 deletions shared/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl Stepper for GameWorld {
&mut self.physics.joint_set,
&mut self.physics.ccd_solver,
&(),
&(),
);

physics::despawn_outliers(&mut self.world, 2500., 16.); // FIXME: use config.phys.scale
Expand Down

0 comments on commit a5a6820

Please sign in to comment.