Skip to content
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Changelog

## Unreleased

### Added

- Add a `PhysicsContext` struct containing top level rapier structs to help with reducing boilerplate.

## v0.23.0 (08 Jan 2025)

### Fix
Expand All @@ -16,7 +24,6 @@
- `RigidBodySet` and `ColliderSet` have a new constructor `with_capacity`.
- Use `profiling` crate to provide helpful profiling information in different tools.
- The testbeds have been updated to use `puffin_egui`

### Modified

- `InteractionGroups` default value for `memberships` is now `GROUP_1` (#706)
Expand Down
22 changes: 7 additions & 15 deletions examples2d/add_remove2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,32 @@ pub fn init_world(testbed: &mut Testbed) {
testbed.add_callback(move |mut graphics, physics, _, state| {
let rot = state.time * -1.0;
for rb_handle in &platform_handles {
let rb = physics.bodies.get_mut(*rb_handle).unwrap();
let rb = physics.context.bodies.get_mut(*rb_handle).unwrap();
rb.set_next_kinematic_rotation(UnitComplex::new(rot));
}

if state.timestep_id % 10 == 0 {
let x = rand::random::<f32>() * 10.0 - 5.0;
let y = rand::random::<f32>() * 10.0 + 10.0;
let rigid_body = RigidBodyBuilder::dynamic().translation(vector![x, y]);
let handle = physics.bodies.insert(rigid_body);
let collider = ColliderBuilder::cuboid(rad, rad);
physics
.colliders
.insert_with_parent(collider, handle, &mut physics.bodies);
let (handle, _) = physics
.context
.insert_body_and_collider(rigid_body, ColliderBuilder::cuboid(rad, rad));

if let Some(graphics) = &mut graphics {
graphics.add_body(handle, &physics.bodies, &physics.colliders);
graphics.add_body(handle, &physics.context.bodies, &physics.context.colliders);
}
}

let to_remove: Vec<_> = physics
.context
.bodies
.iter()
.filter(|(_, b)| b.position().translation.vector.y < -10.0)
.map(|e| e.0)
.collect();
for handle in to_remove {
physics.bodies.remove(
handle,
&mut physics.islands,
&mut physics.colliders,
&mut physics.impulse_joints,
&mut physics.multibody_joints,
true,
);
physics.context.remove_rigidbody(handle, true);

if let Some(graphics) = &mut graphics {
graphics.remove_body(handle);
Expand Down
2 changes: 2 additions & 0 deletions examples2d/ccd2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ pub fn init_world(testbed: &mut Testbed) {
};

let parent_handle1 = physics
.context
.colliders
.get(prox.collider1())
.unwrap()
.parent()
.unwrap();
let parent_handle2 = physics
.context
.colliders
.get(prox.collider2())
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion examples2d/character_controller2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn init_world(testbed: &mut Testbed) {
// let angvel = run_state.time.sin() * 0.5;

// Update the velocity-based kinematic body by setting its velocity.
if let Some(platform) = physics.bodies.get_mut(platform_handle) {
if let Some(platform) = physics.context.bodies.get_mut(platform_handle) {
platform.set_linvel(linvel, true);
// NOTE: interaction with rotating platforms isn’t handled very well yet.
// platform.set_angvel(angvel, true);
Expand Down
4 changes: 2 additions & 2 deletions examples2d/debug_compression2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ pub fn init_world(testbed: &mut Testbed) {
let mut force = vector![0.0, 0.0];

testbed.add_callback(move |_, physics, _, _| {
let left_plank = &mut physics.bodies[handles[0]];
let left_plank = &mut physics.context.bodies[handles[0]];
left_plank.reset_forces(true);
left_plank.add_force(force, true);

let right_plank = &mut physics.bodies[handles[1]];
let right_plank = &mut physics.context.bodies[handles[1]];
right_plank.reset_forces(true);
right_plank.add_force(-force, true);

Expand Down
8 changes: 3 additions & 5 deletions examples2d/debug_intersection2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,18 @@ pub fn init_world(testbed: &mut Testbed) {

testbed.add_callback(move |graphics, physics, _, run| {
let slow_time = run.timestep_id as f32 / 3.0;
let intersection = physics.query_pipeline.intersection_with_shape(
&physics.bodies,
&physics.colliders,
let intersection = physics.context.intersection_with_shape(
&Isometry::translation(slow_time.cos() * 10.0, slow_time.sin() * 10.0),
&Ball::new(rad / 2.0),
QueryFilter::default(),
);

if let Some(graphics) = graphics {
for (handle, _) in physics.bodies.iter() {
for (handle, _) in physics.context.bodies.iter() {
graphics.set_body_color(handle, [0.5, 0.5, 0.5]);
}
if let Some(intersection) = intersection {
let collider = physics.colliders.get(intersection).unwrap();
let collider = physics.context.colliders.get(intersection).unwrap();
let body_handle = collider.parent().unwrap();

graphics.set_body_color(body_handle, [1.0, 0.0, 0.0]);
Expand Down
6 changes: 5 additions & 1 deletion examples2d/drum2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ pub fn init_world(testbed: &mut Testbed) {
*/
testbed.add_callback(move |_, physics, _, _| {
// Update the velocity-based kinematic body by setting its velocity.
if let Some(platform) = physics.bodies.get_mut(velocity_based_platform_handle) {
if let Some(platform) = physics
.context
.bodies
.get_mut(velocity_based_platform_handle)
{
platform.set_angvel(-0.15, true);
}
});
Expand Down
4 changes: 2 additions & 2 deletions examples2d/inverse_kinematics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn init_world(testbed: &mut Testbed) {

testbed.add_callback(move |graphics, physics, _, _| {
let Some(graphics) = graphics else { return };
if let Some((multibody, link_id)) = physics.multibody_joints.get_mut(last_link) {
if let Some((multibody, link_id)) = physics.context.multibody_joints.get_mut(last_link) {
// Ensure our displacement vector has the right number of elements.
if displacements.nrows() < multibody.ndofs() {
displacements = DVector::zeros(multibody.ndofs());
Expand All @@ -78,7 +78,7 @@ pub fn init_world(testbed: &mut Testbed) {
};

multibody.inverse_kinematics(
&physics.bodies,
&physics.context.bodies,
link_id,
&options,
&Isometry::from(target_point),
Expand Down
17 changes: 8 additions & 9 deletions examples2d/one_way_platforms2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,21 @@ pub fn init_world(testbed: &mut Testbed) {
* depending on their position.
*/
testbed.add_callback(move |graphics, physics, _, run_state| {
if run_state.timestep_id % 200 == 0 && physics.bodies.len() <= 7 {
if run_state.timestep_id % 200 == 0 && physics.context.bodies.len() <= 7 {
// Spawn a new cube.
let collider = ColliderBuilder::cuboid(1.5, 2.0);
let body = RigidBodyBuilder::dynamic().translation(vector![20.0, 10.0]);
let handle = physics.bodies.insert(body);
physics
.colliders
.insert_with_parent(collider, handle, &mut physics.bodies);
let (handle, _) = physics.context.insert_body_and_collider(
RigidBodyBuilder::dynamic().translation(vector![20.0, 10.0]),
collider,
);

if let Some(graphics) = graphics {
graphics.add_body(handle, &physics.bodies, &physics.colliders);
graphics.add_body(handle, &physics.context.bodies, &physics.context.colliders);
}
}

for handle in physics.islands.active_dynamic_bodies() {
let body = &mut physics.bodies[*handle];
for handle in physics.context.island_manager.active_dynamic_bodies() {
let body = &mut physics.context.bodies[*handle];
if body.position().translation.y > 1.0 {
body.set_gravity_scale(1.0, false);
} else if body.position().translation.y < -1.0 {
Expand Down
14 changes: 11 additions & 3 deletions examples2d/platform2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,22 @@ pub fn init_world(testbed: &mut Testbed) {
let velocity = vector![run_state.time.sin() * 5.0, (run_state.time * 5.0).sin()];

// Update the velocity-based kinematic body by setting its velocity.
if let Some(platform) = physics.bodies.get_mut(velocity_based_platform_handle) {
if let Some(platform) = physics
.context
.bodies
.get_mut(velocity_based_platform_handle)
{
platform.set_linvel(velocity, true);
}

// Update the position-based kinematic body by setting its next position.
if let Some(platform) = physics.bodies.get_mut(position_based_platform_handle) {
if let Some(platform) = physics
.context
.bodies
.get_mut(position_based_platform_handle)
{
let mut next_tra = *platform.translation();
next_tra += velocity * physics.integration_parameters.dt;
next_tra += velocity * physics.context.integration_parameters.dt;
platform.set_next_kinematic_translation(next_tra);
}
});
Expand Down
8 changes: 6 additions & 2 deletions examples2d/sensor2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ pub fn init_world(testbed: &mut Testbed) {
[0.5, 0.5, 1.0]
};

let parent_handle1 = physics.colliders[prox.collider1()].parent().unwrap();
let parent_handle2 = physics.colliders[prox.collider2()].parent().unwrap();
let parent_handle1 = physics.context.colliders[prox.collider1()]
.parent()
.unwrap();
let parent_handle2 = physics.context.colliders[prox.collider2()]
.parent()
.unwrap();

if let Some(graphics) = &mut graphics {
if parent_handle1 != ground_handle && parent_handle1 != sensor_handle {
Expand Down
8 changes: 4 additions & 4 deletions examples3d-f64/debug_serialized3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ pub fn init_world(testbed: &mut Testbed) {
state.impulse_joints,
state.multibody_joints,
);
testbed.harness_mut().physics.islands = state.islands;
testbed.harness_mut().physics.broad_phase = state.broad_phase;
testbed.harness_mut().physics.narrow_phase = state.narrow_phase;
testbed.harness_mut().physics.ccd_solver = state.ccd_solver;
testbed.harness_mut().physics.context.island_manager = state.islands;
testbed.harness_mut().physics.context.broad_phase = state.broad_phase;
testbed.harness_mut().physics.context.narrow_phase = state.narrow_phase;
testbed.harness_mut().physics.context.ccd_solver = state.ccd_solver;

testbed.set_graphics_shift(vector![-541.0, -6377257.0, -61.0]);
testbed.look_at(point![10.0, 10.0, 10.0], point![0.0, 0.0, 0.0]);
Expand Down
2 changes: 2 additions & 0 deletions examples3d/ccd3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ pub fn init_world(testbed: &mut Testbed) {
};

let parent_handle1 = physics
.context
.colliders
.get(prox.collider1())
.unwrap()
.parent()
.unwrap();
let parent_handle2 = physics
.context
.colliders
.get(prox.collider2())
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion examples3d/character_controller3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn init_world(testbed: &mut Testbed) {
// let angvel = run_state.time.sin() * 0.5;

// Update the velocity-based kinematic body by setting its velocity.
if let Some(platform) = physics.bodies.get_mut(platform_handle) {
if let Some(platform) = physics.context.bodies.get_mut(platform_handle) {
platform.set_linvel(linvel, true);
// NOTE: interaction with rotating platforms isn’t handled very well yet.
// platform.set_angvel(angvel, true);
Expand Down
14 changes: 8 additions & 6 deletions examples3d/debug_add_remove_collider3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ pub fn init_world(testbed: &mut Testbed) {
// Remove then re-add the ground collider.
let removed_collider_handle = ground_collider_handle;
let coll = physics
.context
.colliders
.remove(
removed_collider_handle,
&mut physics.islands,
&mut physics.bodies,
&mut physics.context.island_manager,
&mut physics.context.bodies,
true,
)
.unwrap();
ground_collider_handle =
physics
.colliders
.insert_with_parent(coll, ground_handle, &mut physics.bodies);
ground_collider_handle = physics.context.colliders.insert_with_parent(
coll,
ground_handle,
&mut physics.context.bodies,
);
});

/*
Expand Down
11 changes: 6 additions & 5 deletions examples3d/debug_deserialize3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ pub fn init_world(testbed: &mut Testbed) {
state.impulse_joints,
state.multibody_joints,
);
testbed.harness_mut().physics.islands = state.islands;
testbed.harness_mut().physics.broad_phase = state.broad_phase;
testbed.harness_mut().physics.narrow_phase = state.narrow_phase;
testbed.harness_mut().physics.integration_parameters = state.integration_parameters;
testbed.harness_mut().physics.gravity = state.gravity;
testbed.harness_mut().physics.context.island_manager = state.islands;
testbed.harness_mut().physics.context.broad_phase = state.broad_phase;
testbed.harness_mut().physics.context.narrow_phase = state.narrow_phase;
testbed.harness_mut().physics.context.integration_parameters =
state.integration_parameters;
testbed.harness_mut().physics.context.gravity = state.gravity;

testbed.set_graphics_shift(vector![-541.0, -6377257.0, -61.0]);
testbed.look_at(point![10.0, 10.0, 10.0], point![0.0, 0.0, 0.0]);
Expand Down
30 changes: 15 additions & 15 deletions examples3d/debug_dynamic_collider_add3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@ pub fn init_world(testbed: &mut Testbed) {

// Add a bigger ball collider
let collider = ColliderBuilder::ball(ball_rad + 0.01 * (step as f32)).density(100.0);
let new_ball_collider_handle =
physics
.colliders
.insert_with_parent(collider, ball_handle, &mut physics.bodies);
let new_ball_collider_handle = physics.context.colliders.insert_with_parent(
collider,
ball_handle,
&mut physics.context.bodies,
);

if let Some(graphics) = &mut graphics {
graphics.add_collider(new_ball_collider_handle, &physics.colliders);
graphics.add_collider(new_ball_collider_handle, &physics.context.colliders);
}

extra_colliders.push(new_ball_collider_handle);

// Snap the ball velocity or restore it.
let ball = physics.bodies.get_mut(ball_handle).unwrap();
let ball = physics.context.bodies.get_mut(ball_handle).unwrap();

if step == snapped_frame {
linvel = *ball.linvel();
Expand All @@ -75,12 +76,10 @@ pub fn init_world(testbed: &mut Testbed) {

for handle in &extra_colliders {
if let Some(graphics) = &mut graphics {
graphics.remove_collider(*handle, &physics.colliders);
graphics.remove_collider(*handle, &physics.context.colliders);
}

physics
.colliders
.remove(*handle, &mut physics.islands, &mut physics.bodies, true);
physics.context.remove_collider(*handle, true);
}

extra_colliders.clear();
Expand All @@ -95,13 +94,14 @@ pub fn init_world(testbed: &mut Testbed) {
// .unwrap();
let coll = ColliderBuilder::cuboid(ground_size, ground_height + step as f32 * 0.01, 0.4)
.friction(0.15);
let new_ground_collider_handle =
physics
.colliders
.insert_with_parent(coll, ground_handle, &mut physics.bodies);
let new_ground_collider_handle = physics.context.colliders.insert_with_parent(
coll,
ground_handle,
&mut physics.context.bodies,
);

if let Some(graphics) = &mut graphics {
graphics.add_collider(new_ground_collider_handle, &physics.colliders);
graphics.add_collider(new_ground_collider_handle, &physics.context.colliders);
}

extra_colliders.push(new_ground_collider_handle);
Expand Down
2 changes: 1 addition & 1 deletion examples3d/debug_rollback3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn init_world(testbed: &mut Testbed) {
step += 1;

// Snap the ball velocity or restore it.
let ball = physics.bodies.get_mut(ball_handle).unwrap();
let ball = physics.context.bodies.get_mut(ball_handle).unwrap();

if step == snapped_frame {
linvel = *ball.linvel();
Expand Down
Loading