Split rigid-bodies and colliders into multiple components
This commit is contained in:
@@ -31,9 +31,12 @@ pub fn init_world(testbed: &mut Testbed) {
|
||||
.map(|e| e.0)
|
||||
.collect();
|
||||
for handle in to_remove {
|
||||
physics
|
||||
.bodies
|
||||
.remove(handle, &mut physics.colliders, &mut physics.joints);
|
||||
physics.bodies.remove(
|
||||
handle,
|
||||
&mut physics.islands,
|
||||
&mut physics.colliders,
|
||||
&mut physics.joints,
|
||||
);
|
||||
|
||||
if let (Some(graphics), Some(window)) = (&mut graphics, &mut window) {
|
||||
graphics.remove_body_nodes(*window, handle);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use na::Point2;
|
||||
use rapier2d::dynamics::{BallJoint, BodyStatus, JointSet, RigidBodyBuilder, RigidBodySet};
|
||||
use rapier2d::dynamics::{BallJoint, JointSet, RigidBodyBuilder, RigidBodySet, RigidBodyType};
|
||||
use rapier2d::geometry::{ColliderBuilder, ColliderSet};
|
||||
use rapier_testbed2d::Testbed;
|
||||
|
||||
@@ -31,9 +31,9 @@ pub fn init_world(testbed: &mut Testbed) {
|
||||
let fi = i as f32;
|
||||
|
||||
let status = if i == 0 && k == 0 {
|
||||
BodyStatus::Static
|
||||
RigidBodyType::Static
|
||||
} else {
|
||||
BodyStatus::Dynamic
|
||||
RigidBodyType::Dynamic
|
||||
};
|
||||
|
||||
let rigid_body = RigidBodyBuilder::new(status)
|
||||
|
||||
@@ -9,12 +9,15 @@ 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.
|
||||
//
|
||||
@@ -29,16 +32,16 @@ impl PhysicsHooks for OneWayPlatformHook {
|
||||
// - If context.collider_handle2 == self.platform2 then the allowed normal -y needs to be flipped to +y.
|
||||
let mut allowed_local_n1 = Vector2::zeros();
|
||||
|
||||
if context.collider_handle1 == self.platform1 {
|
||||
if context.collider1 == self.platform1 {
|
||||
allowed_local_n1 = Vector2::y();
|
||||
} else if context.collider_handle2 == self.platform1 {
|
||||
} else if context.collider2 == self.platform1 {
|
||||
// Flip the allowed direction.
|
||||
allowed_local_n1 = -Vector2::y();
|
||||
}
|
||||
|
||||
if context.collider_handle1 == self.platform2 {
|
||||
if context.collider1 == self.platform2 {
|
||||
allowed_local_n1 = -Vector2::y();
|
||||
} else if context.collider_handle2 == self.platform2 {
|
||||
} else if context.collider2 == self.platform2 {
|
||||
// Flip the allowed direction.
|
||||
allowed_local_n1 = Vector2::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.x = 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 = &mut physics.bodies[*handle];
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user