Skip to content

Commit

Permalink
use a RapierContext systemparam for a better API ; rework modules + u…
Browse files Browse the repository at this point in the history
…pdated changelog
  • Loading branch information
Vrixyz committed Sep 19, 2024
1 parent 7519a8c commit 4df1d88
Show file tree
Hide file tree
Showing 26 changed files with 375 additions and 244 deletions.
27 changes: 17 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ which was its hardcoded behaviour.

### Modified

- `RapierContext`, `RapierConfiguration` and `RenderToSimulationTime` are now a `Component` instead of resources.
- Rapier now supports multiple independent physics worlds, see example `multi_world3` for usage details.
- Migration guide:
- `ResMut<mut RapierContext>` -> `WriteDefaultRapierContext`
- `Res<RapierContext>` -> `ReadDefaultRapierContext`
- Access to `RapierConfiguration` and `RenderToSimulationTime` should query for it
on the responsible entity owning the `RenderContext`.
- If you are building a library on top of `bevy_rapier` and would want to support multiple independent physics worlds too,
you can check out the details of [#545](https://github.com/dimforge/bevy_rapier/pull/545)
to get more context and information.
- Rapier now supports multiple independent physics worlds, see example `multi_world3` for usage details.
- `RapierContext` is no longer a resource: it has been split in multiple `Component`s:
- `RapierContextColliders`
- `RapierContextJoints`
- `RapierContextSimulation`
- `RapierRigidBodySet`
- Each entity managed by bevy_rapier has a `RapierContextEntityLink` pointing to the entity containing the components above.
- `RapierContext` was reworked to be a `SystemParam`, to help with migration. See `ray_casting` examples for a usage example.
- `RapierConfiguration` and `RenderToSimulationTime` are now `Component`s instead of resources.
- Migration guide:
- `ResMut<mut RapierContext>` -> `WriteRapierContext`
- `Res<RapierContext>` -> `ReadRapierContext`
- Access to `RapierConfiguration` and `RenderToSimulationTime` should query for it
on the responsible entity owning the `RenderContext`.
- If you are building a library on top of `bevy_rapier` and would want to support multiple independent physics worlds too,
you can check out the details of [#545](https://github.com/dimforge/bevy_rapier/pull/545)
to get more context and information.

## v0.27.0 (07 July 2024)

Expand Down
5 changes: 2 additions & 3 deletions bevy_rapier2d/examples/testbed2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,10 @@ fn main() {
OnExit(Examples::PlayerMovement2),
(
cleanup,
|mut rapier_config: Query<&mut RapierConfiguration>,
ctxt: ReadDefaultRapierContext| {
|mut rapier_config: Query<&mut RapierConfiguration>, ctxt: ReadRapierContext| {
let mut rapier_config = rapier_config.single_mut();
rapier_config.gravity = RapierConfiguration::new(
ctxt.single().context.integration_parameters.length_unit,
ctxt.single().simulation.integration_parameters.length_unit,
)
.gravity;
},
Expand Down
2 changes: 1 addition & 1 deletion bevy_rapier3d/examples/joints3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub fn setup_physics(mut commands: Commands) {
}

pub fn print_impulse_revolute_joints(
context: ReadDefaultRapierContext,
context: ReadRapierContext,
joints: Query<(Entity, &ImpulseJoint)>,
) {
for (entity, impulse_joint) in joints.iter() {
Expand Down
4 changes: 2 additions & 2 deletions bevy_rapier3d/examples/multi_world3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {

fn create_worlds(mut commands: Commands) {
for i in 0..N_WORLDS {
let mut world = commands.spawn((RapierContext::default(), WorldId(i)));
let mut world = commands.spawn((RapierContextSimulation::default(), WorldId(i)));
if i == 0 {
world.insert(DefaultRapierContext);
}
Expand Down Expand Up @@ -75,7 +75,7 @@ fn change_world(
}

pub fn setup_physics(
context: Query<(Entity, &WorldId), With<RapierContext>>,
context: Query<(Entity, &WorldId), With<RapierContextSimulation>>,
mut commands: Commands,
) {
for (context_entity, id) in context.iter() {
Expand Down
3 changes: 1 addition & 2 deletions bevy_rapier3d/examples/ray_casting3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ pub fn setup_physics(mut commands: Commands) {
pub fn cast_ray(
mut commands: Commands,
windows: Query<&Window, With<PrimaryWindow>>,
rapier_context: ReadDefaultRapierContext,
rapier_context_colliders: ReadDefaultRapierContextColliders,
rapier_context: ReadRapierContext,
cameras: Query<(&Camera, &GlobalTransform)>,
) {
let window = windows.single();
Expand Down
4 changes: 2 additions & 2 deletions bevy_rapier_benches3d/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use common::default_app;
use common::wait_app_start;

use bevy::prelude::*;
use bevy_rapier3d::plugin::RapierContext;
use bevy_rapier3d::plugin::context::RapierContextSimulation;

pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) {
let mut app = default_app();
Expand All @@ -28,7 +28,7 @@ pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) {
let elapsed_time = timer_full_update.time() as f32;
let rc = app
.world_mut()
.query::<&RapierContext>()
.query::<&RapierContextSimulation>()
.single(app.world());
rapier_step_times.push(rc.pipeline.counters.step_time.time() as f32);
total_update_times.push(elapsed_time);
Expand Down
2 changes: 1 addition & 1 deletion src/control/character_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::geometry::{Collider, CollisionGroups, ShapeCastHit};
use crate::math::{Real, Rot, Vect};
use bevy::prelude::*;

use crate::plugin::{RapierContext, RapierContextColliders};
use crate::plugin::context::RapierContextColliders;
pub use rapier::control::CharacterAutostep;
pub use rapier::control::CharacterLength;
use rapier::prelude::{ColliderSet, QueryFilterFlags};
Expand Down
5 changes: 4 additions & 1 deletion src/dynamics/revolute_joint.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::dynamics::{GenericJoint, GenericJointBuilder};
use crate::math::{Real, Vect};
use crate::plugin::RapierRigidBodySet;
use crate::plugin::context::RapierRigidBodySet;
use bevy::prelude::Entity;
use rapier::dynamics::{
JointAxesMask, JointAxis, JointLimits, JointMotor, MotorModel, RigidBodyHandle, RigidBodySet,
};

#[cfg(doc)]
use crate::prelude::RapierContext;

use super::TypedJoint;

#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub mod prelude {
pub use crate::geometry::*;
pub use crate::math::*;
pub use crate::pipeline::*;
pub use crate::plugin::context::systemparams::*;
pub use crate::plugin::context::*;
pub use crate::plugin::*;
#[cfg(any(feature = "debug-render-3d", feature = "debug-render-2d"))]
pub use crate::render::*;
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline/query_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use rapier::pipeline::QueryFilterFlags;
use crate::geometry::CollisionGroups;

#[cfg(doc)]
use crate::prelude::RapierContext;
use crate::prelude::{RapierContext, RapierContextSimulation};

/// A filter that describes what collider should be included or excluded from a scene query.
///
Expand Down
9 changes: 2 additions & 7 deletions src/plugin/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Components used to configure a simulation run by rapier, these are not modified by bevy_rapier.

use bevy::{
prelude::{Component, Resource},
reflect::Reflect,
Expand All @@ -8,13 +10,6 @@ use crate::math::{Real, Vect};
#[cfg(doc)]
use {crate::prelude::TransformInterpolation, rapier::dynamics::IntegrationParameters};

/// Difference between simulation and rendering time
#[derive(Component, Default, Reflect)]
pub struct SimulationToRenderTime {
/// Difference between simulation and rendering time
pub diff: f32,
}

/// The different ways of adjusting the timestep length each frame.
#[derive(Copy, Clone, Debug, PartialEq, Resource)]
pub enum TimestepMode {
Expand Down
38 changes: 26 additions & 12 deletions src/plugin/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! These are components used and modified during a simulation frame.

pub mod systemparams;

use bevy::prelude::*;
Expand All @@ -19,33 +21,45 @@ use bevy::prelude::{Entity, EventWriter, GlobalTransform, Query};
use crate::control::{CharacterCollision, MoveShapeOptions, MoveShapeOutput};
use crate::dynamics::TransformInterpolation;
use crate::parry::query::details::ShapeCastOptions;
use crate::plugin::configuration::{SimulationToRenderTime, TimestepMode};
use crate::plugin::configuration::TimestepMode;
use crate::prelude::{CollisionGroups, RapierRigidBodyHandle};
use rapier::control::CharacterAutostep;
use rapier::geometry::DefaultBroadPhase;

#[cfg(doc)]
use crate::prelude::{ImpulseJoint, MultibodyJoint, RevoluteJoint, TypedJoint};
use crate::prelude::{
systemparams::{RapierContext, ReadRapierContext},
ImpulseJoint, MultibodyJoint, RevoluteJoint, TypedJoint,
};

/// Difference between simulation and rendering time
#[derive(Component, Default, Reflect)]
pub struct SimulationToRenderTime {
/// Difference between simulation and rendering time
pub diff: f32,
}

/// Marker component for to access the default [`RapierContext`].
/// Marker component for to access the default [`ReadRapierContext`].
///
/// This is used by [`systemparams::ReadDefaultRapierContext`] and other default accesses
/// This is use as the default marker filter for [`systemparams::ReadRapierContext`] and [`systemparams::WriteRapierContext`]
/// to help with getting a reference to the correct RapierContext.
///
/// If you're making a library, you might be interested in [`RapierContextEntityLink`]
/// and leverage a [`Query<&RapierContext>`] to find the correct [`RapierContext`] of an entity.
/// and leverage a [`Query`] to have precise access to relevant components (for example [`RapierContextSimulation`]).
///
/// See the list of full components in [`RapierContext`]
#[derive(Component, Reflect, Debug, Clone, Copy)]
pub struct DefaultRapierContext;

/// This is a component applied to any entity containing a rapier handle component.
/// The inner Entity referred to has the component [`RapierContext`] responsible for handling
/// The inner Entity referred to has the component [`RapierContextSimulation`] responsible for handling
/// its rapier data.
#[derive(Component, Reflect, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RapierContextEntityLink(pub Entity);

/// The set of colliders part of the simulation.
///
/// This should be attached on an entity with a [`RapierContext`]
/// This should be attached on an entity with a [`RapierContextSimulation`]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Component, Default)]
pub struct RapierContextColliders {
Expand Down Expand Up @@ -113,7 +127,7 @@ impl RapierContextColliders {

/// The sets of joints part of the simulation.
///
/// This should be attached on an entity with a [`RapierContext`]
/// This should be attached on an entity with a [`RapierContextSimulation`]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Component, Default)]
pub struct RapierContextJoints {
Expand Down Expand Up @@ -643,7 +657,7 @@ impl RapierQueryPipeline {

/// The set of rigid-bodies part of the simulation.
///
/// This should be attached on an entity with a [`RapierContext`]
/// This should be attached on an entity with a [`RapierContextSimulation`]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Component, Default)]
pub struct RapierRigidBodySet {
Expand Down Expand Up @@ -720,7 +734,7 @@ impl RapierRigidBodySet {
/// The Rapier context, containing all the state of the physics engine.
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Component)]
pub struct RapierContext {
pub struct RapierContextSimulation {
/// The island manager, which detects what object is sleeping
/// (not moving much) to reduce computations.
pub islands: IslandManager,
Expand Down Expand Up @@ -752,7 +766,7 @@ pub struct RapierContext {
pub(crate) character_collisions_collector: Vec<rapier::control::CharacterCollision>,
}

impl Default for RapierContext {
impl Default for RapierContextSimulation {
fn default() -> Self {
Self {
islands: IslandManager::new(),
Expand All @@ -770,7 +784,7 @@ impl Default for RapierContext {
}
}

impl RapierContext {
impl RapierContextSimulation {
/// Advance the simulation, based on the given timestep mode.
#[allow(clippy::too_many_arguments)]
pub fn step_simulation(
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/context/systemparams/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use rapier_context_systemparam::*;
use super::RapierContextEntityLink;

#[derive(QueryData)]
pub struct RapierEntity {
pub(crate) struct RapierEntity {
pub entity: Entity,
pub rapier_context_link: &'static RapierContextEntityLink,
}
Loading

0 comments on commit 4df1d88

Please sign in to comment.