Split rigid-bodies and colliders into multiple components

This commit is contained in:
Crozet Sébastien
2021-04-26 17:59:25 +02:00
parent aaf80bfa87
commit c32da78f2a
91 changed files with 5969 additions and 3653 deletions

View File

@@ -9,36 +9,39 @@ struct OneWayPlatformHook {
platform2: ColliderHandle,
}
impl PhysicsHooks for OneWayPlatformHook {
impl PhysicsHooks<RigidBodySet, ColliderSet> for OneWayPlatformHook {
fn active_hooks(&self) -> PhysicsHooksFlags {
PhysicsHooksFlags::MODIFY_SOLVER_CONTACTS
}
fn modify_solver_contacts(&self, context: &mut ContactModificationContext) {
fn modify_solver_contacts(
&self,
context: &mut ContactModificationContext<RigidBodySet, ColliderSet>,
) {
// The allowed normal for the first platform is its local +y axis, and the
// allowed normal for the second platform is its local -y axis.
//
// Now we have to be careful because the `manifold.local_n1` normal points
// toward the outside of the shape of `context.co1`. So we need to flip the
// allowed normal direction if the platform is in `context.collider_handle2`.
// allowed normal direction if the platform is in `context.collider2`.
//
// Therefore:
// - If context.collider_handle1 == self.platform1 then the allowed normal is +y.
// - If context.collider_handle2 == self.platform1 then the allowed normal is -y.
// - If context.collider_handle1 == self.platform2 then its allowed normal +y needs to be flipped to -y.
// - If context.collider_handle2 == self.platform2 then the allowed normal -y needs to be flipped to +y.
// - If context.collider1 == self.platform1 then the allowed normal is +y.
// - If context.collider2 == self.platform1 then the allowed normal is -y.
// - If context.collider1 == self.platform2 then its allowed normal +y needs to be flipped to -y.
// - If context.collider2 == self.platform2 then the allowed normal -y needs to be flipped to +y.
let mut allowed_local_n1 = Vector3::zeros();
if context.collider_handle1 == self.platform1 {
if context.collider1 == self.platform1 {
allowed_local_n1 = Vector3::y();
} else if context.collider_handle2 == self.platform1 {
} else if context.collider2 == self.platform1 {
// Flip the allowed direction.
allowed_local_n1 = -Vector3::y();
}
if context.collider_handle1 == self.platform2 {
if context.collider1 == self.platform2 {
allowed_local_n1 = -Vector3::y();
} else if context.collider_handle2 == self.platform2 {
} else if context.collider2 == self.platform2 {
// Flip the allowed direction.
allowed_local_n1 = Vector3::y();
}
@@ -47,13 +50,12 @@ impl PhysicsHooks for OneWayPlatformHook {
context.update_as_oneway_platform(&allowed_local_n1, 0.1);
// Set the surface velocity of the accepted contacts.
let tangent_velocity = if context.collider_handle1 == self.platform1
|| context.collider_handle2 == self.platform2
{
-12.0
} else {
12.0
};
let tangent_velocity =
if context.collider1 == self.platform1 || context.collider2 == self.platform2 {
-12.0
} else {
12.0
};
for contact in context.solver_contacts.iter_mut() {
contact.tangent_velocity.z = tangent_velocity;
@@ -115,13 +117,14 @@ pub fn init_world(testbed: &mut Testbed) {
}
}
physics.bodies.foreach_active_dynamic_body_mut(|_, body| {
for handle in physics.islands.active_dynamic_bodies() {
let body = physics.bodies.get_mut(*handle).unwrap();
if body.position().translation.y > 1.0 {
body.set_gravity_scale(1.0, false);
} else if body.position().translation.y < -1.0 {
body.set_gravity_scale(-1.0, false);
}
});
}
});
/*