Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into read-mass-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
sebcrozet committed Aug 6, 2023
2 parents b8e7ad3 + c5d12af commit 37a099d
Show file tree
Hide file tree
Showing 24 changed files with 265 additions and 86 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## Unreleased
### Added
- `ColliderView::as_typed_shape` and `::to_shared_shape` to convert a `ColliderView` to a parry’s
`TypedShape` or `SharedShape`. The `From` trait has also been implemented accordingly.
- Implement `Copy` for `ColliderView` and all the other non-mut shape views.
- Add `RapierContext::rigid_body_colliders` to retrieve all collider entities attached to this rigid-body.
- Add `RapierPhysicsPlugin::in_fixed_schedule`/`::in_schedude` to add rapier’s systems to a fixed/custom
schedule.

### Fix
- Fix debug-renderer lagging one frame behind.
- Fix Collider `Transform` rotation change not being taken into account by the physics engine.

## 0.22.0 (10 July 2023)
### Modified
- Update to Bevy 0.11.
Expand Down
3 changes: 0 additions & 3 deletions bevy_rapier2d/examples/custom_system_setup2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ fn main() {
PostUpdate,
(
PhysicsSet::SyncBackend,
PhysicsSet::SyncBackendFlush,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
)
Expand All @@ -33,8 +32,6 @@ fn main() {
(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackend)
.in_set(PhysicsSet::SyncBackend),
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackendFlush)
.in_set(PhysicsSet::SyncBackendFlush),
(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::StepSimulation),
despawn_one_box,
Expand Down
3 changes: 0 additions & 3 deletions bevy_rapier3d/examples/custom_system_setup3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ fn main() {
PostUpdate,
(
PhysicsSet::SyncBackend,
PhysicsSet::SyncBackendFlush,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
)
Expand All @@ -33,8 +32,6 @@ fn main() {
(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackend)
.in_set(PhysicsSet::SyncBackend),
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackendFlush)
.in_set(PhysicsSet::SyncBackendFlush),
(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::StepSimulation),
despawn_one_box,
Expand Down
2 changes: 2 additions & 0 deletions src/geometry/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ impl Default for ContactForceEventThreshold {

/// Component which will be filled (if present) with a list of entities with which the current
/// entity is currently in contact.
///
/// This currently only updates when on an entity with a `Collider`.
#[derive(Component, Default, Reflect)]
#[reflect(Component)]
pub struct CollidingEntities(pub(crate) HashSet<Entity>);
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::Real;
use rapier::parry::shape::Ball;

/// Read-only access to the properties of a ball.
#[derive(Copy, Clone)]
pub struct BallView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Ball,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::math::{Real, Rot, Vect};
use rapier::parry::shape::Capsule;

/// Read-only access to the properties of a capsule.
#[derive(Copy, Clone)]
pub struct CapsuleView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Capsule,
Expand Down
101 changes: 101 additions & 0 deletions src/geometry/shape_views/collider_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rapier::parry::either::Either;
use rapier::parry::shape::TypedShape;

/// Read-only access to the properties of a collider.
#[derive(Copy, Clone)]
pub enum ColliderView<'a> {
/// A ball shape.
Ball(BallView<'a>),
Expand Down Expand Up @@ -146,7 +147,107 @@ impl<'a> From<TypedShape<'a>> for ColliderView<'a> {
}
}

impl<'a> From<ColliderView<'a>> for TypedShape<'a> {
fn from(collider_view: ColliderView<'a>) -> TypedShape<'a> {
collider_view.as_typed_shape()
}
}

impl<'a> From<ColliderView<'a>> for SharedShape {
fn from(collider_view: ColliderView<'a>) -> SharedShape {
collider_view.to_shared_shape()
}
}

impl<'a> ColliderView<'a> {
/// Convert to [`parry::TypedShape`].
pub fn as_typed_shape(self) -> TypedShape<'a> {
match self {
ColliderView::Ball(BallView { raw: s }) => TypedShape::Ball(s),
ColliderView::Cuboid(CuboidView { raw: s }) => TypedShape::Cuboid(s),
ColliderView::Capsule(CapsuleView { raw: s }) => TypedShape::Capsule(s),
ColliderView::Segment(SegmentView { raw: s }) => TypedShape::Segment(s),
ColliderView::Triangle(TriangleView { raw: s }) => TypedShape::Triangle(s),
ColliderView::TriMesh(TriMeshView { raw: s }) => TypedShape::TriMesh(s),
ColliderView::Polyline(PolylineView { raw: s }) => TypedShape::Polyline(s),
ColliderView::HalfSpace(HalfSpaceView { raw: s }) => TypedShape::HalfSpace(s),
ColliderView::HeightField(HeightFieldView { raw: s }) => TypedShape::HeightField(s),
ColliderView::Compound(CompoundView { raw: s }) => TypedShape::Compound(s),
#[cfg(feature = "dim2")]
ColliderView::ConvexPolygon(ConvexPolygonView { raw: s }) => {
TypedShape::ConvexPolygon(s)
}
#[cfg(feature = "dim3")]
ColliderView::ConvexPolyhedron(ConvexPolyhedronView { raw: s }) => {
TypedShape::ConvexPolyhedron(s)
}
#[cfg(feature = "dim3")]
ColliderView::Cylinder(CylinderView { raw: s }) => TypedShape::Cylinder(s),
#[cfg(feature = "dim3")]
ColliderView::Cone(ConeView { raw: s }) => TypedShape::Cone(s),
ColliderView::RoundCuboid(RoundCuboidView { raw: s }) => TypedShape::RoundCuboid(s),
ColliderView::RoundTriangle(RoundTriangleView { raw: s }) => {
TypedShape::RoundTriangle(s)
}
// RoundedTriMesh,
// RoundedHeightField,
#[cfg(feature = "dim2")]
ColliderView::RoundConvexPolygon(RoundConvexPolygonView { raw: s }) => {
TypedShape::RoundConvexPolygon(s)
}
#[cfg(feature = "dim3")]
ColliderView::RoundCylinder(RoundCylinderView { raw: s }) => {
TypedShape::RoundCylinder(s)
}
#[cfg(feature = "dim3")]
ColliderView::RoundCone(RoundConeView { raw: s }) => TypedShape::RoundCone(s),
#[cfg(feature = "dim3")]
ColliderView::RoundConvexPolyhedron(RoundConvexPolyhedronView { raw: s }) => {
TypedShape::RoundConvexPolyhedron(s)
}
}
}

/// Convert to [`parry::SharedShape`].
pub fn to_shared_shape(self) -> SharedShape {
match self {
ColliderView::Ball(BallView { raw }) => SharedShape::new(*raw),
ColliderView::Cuboid(CuboidView { raw }) => SharedShape::new(*raw),
ColliderView::Capsule(CapsuleView { raw }) => SharedShape::new(*raw),
ColliderView::Segment(SegmentView { raw }) => SharedShape::new(*raw),
ColliderView::Triangle(TriangleView { raw }) => SharedShape::new(*raw),
ColliderView::TriMesh(TriMeshView { raw }) => SharedShape::new(raw.clone()),
ColliderView::Polyline(PolylineView { raw }) => SharedShape::new(raw.clone()),
ColliderView::HalfSpace(HalfSpaceView { raw }) => SharedShape::new(*raw),
ColliderView::HeightField(HeightFieldView { raw }) => SharedShape::new(raw.clone()),
ColliderView::Compound(CompoundView { raw }) => SharedShape::new(raw.clone()),
#[cfg(feature = "dim2")]
ColliderView::ConvexPolygon(ConvexPolygonView { raw }) => SharedShape::new(raw.clone()),
#[cfg(feature = "dim3")]
ColliderView::ConvexPolyhedron(ConvexPolyhedronView { raw }) => {
SharedShape::new(raw.clone())
}
#[cfg(feature = "dim3")]
ColliderView::Cylinder(CylinderView { raw }) => SharedShape::new(*raw),
#[cfg(feature = "dim3")]
ColliderView::Cone(ConeView { raw }) => SharedShape::new(*raw),
ColliderView::RoundCuboid(RoundCuboidView { raw }) => SharedShape::new(*raw),
ColliderView::RoundTriangle(RoundTriangleView { raw }) => SharedShape::new(*raw),
#[cfg(feature = "dim2")]
ColliderView::RoundConvexPolygon(RoundConvexPolygonView { raw }) => {
SharedShape::new(raw.clone())
}
#[cfg(feature = "dim3")]
ColliderView::RoundCylinder(RoundCylinderView { raw }) => SharedShape::new(*raw),
#[cfg(feature = "dim3")]
ColliderView::RoundCone(RoundConeView { raw }) => SharedShape::new(*raw),
#[cfg(feature = "dim3")]
ColliderView::RoundConvexPolyhedron(RoundConvexPolyhedronView { raw }) => {
SharedShape::new(raw.clone())
}
}
}

/// Compute the scaled version of `self.raw`.
pub fn raw_scale_by(&self, scale: Vect, num_subdivisions: u32) -> Option<SharedShape> {
let result = match self {
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::math::{Rot, Vect};
use rapier::parry::shape::Compound;

/// Read-only access to the properties of a compound shape.
#[derive(Copy, Clone)]
pub struct CompoundView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Compound,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/cone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::Real;
use rapier::parry::shape::Cone;

/// Read-only access to the properties of a cone.
#[derive(Copy, Clone)]
pub struct ConeView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Cone,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/convex_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::Vect;
use rapier::parry::shape::ConvexPolygon;

/// Read-only access to the properties of a convex polygon.
#[derive(Copy, Clone)]
pub struct ConvexPolygonView<'a> {
/// The raw shape from Rapier.
pub raw: &'a ConvexPolygon,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/convex_polyhedron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::Vect;
use rapier::parry::shape::ConvexPolyhedron;

/// Read-only access to the properties of a convex polyhedron.
#[derive(Copy, Clone)]
pub struct ConvexPolyhedronView<'a> {
/// The raw shape from Rapier.
pub raw: &'a ConvexPolyhedron,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/cuboid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::Vect;
use rapier::geometry::Cuboid;

/// Read-only access to the properties of a cuboid.
#[derive(Copy, Clone)]
pub struct CuboidView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Cuboid,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/cylinder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::Real;
use rapier::parry::shape::Cylinder;

/// Read-only access to the properties of a cylinder.
#[derive(Copy, Clone)]
pub struct CylinderView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Cylinder,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/halfspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::Vect;
use rapier::parry::shape::HalfSpace;

/// Read-only access to the properties of a half-space.
#[derive(Copy, Clone)]
pub struct HalfSpaceView<'a> {
/// The raw shape from Rapier.
pub raw: &'a HalfSpace,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/heightfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rapier::parry::shape::HeightField;
pub use rapier::parry::shape::HeightFieldCellStatus;

/// Read-only access to the properties of a heightfield.
#[derive(Copy, Clone)]
pub struct HeightFieldView<'a> {
/// The raw shape from Rapier.
pub raw: &'a HeightField,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/polyline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::Vect;
use rapier::parry::shape::Polyline;

/// Read-only access to the properties of a polyline.
#[derive(Copy, Clone)]
pub struct PolylineView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Polyline,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/round_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use {
macro_rules! round_shape_view(
($RoundShape: ident, $RoundShapeView: ident, $ShapeView: ident, $RoundShapeViewMut: ident, $ShapeViewMut: ident) => {
/// Read-only access to the properties of a round shape.
#[derive(Copy, Clone)]
pub struct $RoundShapeView<'a> {
/// The raw shape from Rapier.
pub raw: &'a $RoundShape,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::{Real, Vect};
use rapier::parry::shape::Segment;

/// Read-only access to the properties of a segment.
#[derive(Copy, Clone)]
pub struct SegmentView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Segment,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::{Real, Vect};
use rapier::parry::shape::Triangle;

/// Read-only access to the properties of a triangle.
#[derive(Copy, Clone)]
pub struct TriangleView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Triangle,
Expand Down
1 change: 1 addition & 0 deletions src/geometry/shape_views/trimesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::math::Vect;
use rapier::parry::shape::{TopologyError, TriMesh, TriMeshFlags};

/// Read-only access to the properties of a triangle mesh.
#[derive(Copy, Clone)]
pub struct TriMeshView<'a> {
/// The raw shape from Rapier.
pub raw: &'a TriMesh,
Expand Down
15 changes: 15 additions & 0 deletions src/plugin/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ impl RapierContext {
.and_then(|h| self.rigid_body_entity(h))
}

/// If entity is a rigid-body, this returns the collider `Entity`s attached
/// to that rigid-body.
pub fn rigid_body_colliders(&self, entity: Entity) -> impl Iterator<Item = Entity> + '_ {
self.entity2body()
.get(&entity)
.and_then(|handle| self.bodies.get(*handle))
.map(|body| {
body.colliders()
.iter()
.filter_map(|handle| self.collider_entity(*handle))
})
.into_iter()
.flatten()
}

/// Retrieve the Bevy entity the given Rapier collider (identified by its handle) is attached.
pub fn collider_entity(&self, handle: ColliderHandle) -> Option<Entity> {
Self::collider_entity_with_set(&self.colliders, handle)
Expand Down
Loading

0 comments on commit 37a099d

Please sign in to comment.