Skip to content

Commit 0c06418

Browse files
authored
Forces read only (#908)
# Objective Fixes #907 ## Solution This PR splits the `RigidBodyForces` trait into `ReadRigidbodyForces` and `WriteRigidBodyForces`, and implements `ReadRigidBodyForces` for the `ForcesReadOnlyItem` type. This allows for immutable functions to be called on a `Forces` item immutably retrieved from queries, as shown below. ## Testing Tested by building the example shown in the issue: ```rust fn get_total_inertia(objects: Query<(Forces, &Mass)>) { let total = objects .iter() .map(|(forces, mass)| forces.linear_velocity() * mass.0) .reduce(|a, b| a + b); } ``` Which now compiles without errors and works as expected.
1 parent 567b9f5 commit 0c06418

File tree

4 files changed

+230
-149
lines changed

4 files changed

+230
-149
lines changed

migration-guides/0.4-to-main.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@ since the latest release. These guides are evolving and may not be polished yet.
55

66
See [migration-guides/README.md](./README.md) and existing entries for information about Avian's
77
migration guide process and what to put here.
8+
9+
## `ReadRigidBody` and `WriteRigidBody`
10+
11+
PR [#908](https://github.com/avianphysics/avian/pull/908) introduced two new traits: `ReadRigidBody` and `WriteRigidBody`, and `RigidyBodyForces` is now defined as:
12+
13+
```rust
14+
pub trait RigidBodyForces: ReadRigidBodyForces + WriteRigidBodyForces {}
15+
```
16+
17+
In most cases this should just work, but if it doesn't, you can replace your implementation for `RigidBodyForces` with both `ReadRigidBodyForces` and `WriteRigidBodyForces` where it is used / needed. Both traits are required to implement `RigidBodyForces`, but you can implement them separately.

src/dynamics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub mod prelude {
8787
forces::{
8888
ConstantAngularAcceleration, ConstantForce, ConstantLinearAcceleration,
8989
ConstantLocalForce, ConstantLocalLinearAcceleration, ConstantTorque, ForcePlugin,
90-
ForceSystems, Forces, RigidBodyForces,
90+
ForceSystems, Forces, ReadRigidBodyForces, RigidBodyForces, WriteRigidBodyForces,
9191
},
9292
mass_properties::{
9393
MassPropertiesExt, MassPropertyHelper, MassPropertyPlugin,

src/dynamics/rigid_body/forces/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ mod query_data;
201201
mod tests;
202202

203203
pub use plugin::{ForcePlugin, ForceSystems};
204-
pub use query_data::{Forces, ForcesItem, NonWakingForcesItem, RigidBodyForces};
204+
pub use query_data::{
205+
Forces, ForcesItem, NonWakingForcesItem, ReadRigidBodyForces, RigidBodyForces,
206+
WriteRigidBodyForces,
207+
};
205208

206209
use crate::prelude::*;
207210
use bevy::prelude::*;

0 commit comments

Comments
 (0)