Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

feat: introduce RigidBodyDominance component #233

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl Plugin for CorePlugin {
.init_resource::<PhysicsSteps>()
.register_type::<CollisionShape>()
.register_type::<RigidBody>()
.register_type::<RigidBodyDominance>()
.register_type::<PhysicMaterial>()
.register_type::<Velocity>()
.register_type::<Acceleration>()
Expand Down Expand Up @@ -328,6 +329,32 @@ impl RigidBody {
}
}

/// Component that defines the dominance of the rigid body
///
/// When two dynamic rigid bodies collide, the body with the higher dominance will be treated with infinite mass (i.e. will not be pushed).
/// This is particularly useful in player controller scenarios
///
/// It must be inserted on the same entity of a [`RigidBody`]
///
/// # Example
///
/// ```
/// # use bevy::prelude::*;
/// # use heron_core::*;
/// fn spawn(mut commands: Commands) {
/// commands.spawn_bundle(todo!("Spawn your sprite/mesh, incl. at least a GlobalTransform"))
/// .insert(CollisionShape::Sphere { radius: 1.0 }) // Make a body (is dynamic by default)
/// .insert(RigidBodyDominance {
/// dominance: i8::MAX, // Define the dominance of the dynamic rigid body. Higher will dominate
/// });
/// }
/// ```
#[derive(Debug, Component, Copy, Clone, Default, Reflect)]
pub struct RigidBodyDominance {
/// The dominance of the rigid body. The higher value in a collision will dominate
pub dominance: i8,
}
Comment on lines +352 to +356
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here I would prefer to have the field private and implement From<i8> for RigidBodyDominance and From<RigidBodyDominance> for i8.

So that we promise a little bit less, and keep the door open to change the internal representation.


/// Mark the [`CollisionShape`] of the same entity as being a *sensor*.
///
/// This is especially useful to add sensor to an existing (non-sensor) rigid body without the need to create a [`RigidBody::Sensor`] in between.
Expand Down
13 changes: 11 additions & 2 deletions rapier/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use bevy::ecs::prelude::*;
use bevy::transform::prelude::*;
use fnv::FnvHashMap;

use heron_core::{Damping, PhysicMaterial, RigidBody, RotationConstraints, Velocity};
use heron_core::{
Damping, PhysicMaterial, RigidBody, RigidBodyDominance, RotationConstraints, Velocity,
};

use crate::convert::{IntoBevy, IntoRapier};
use crate::rapier::dynamics::{
Expand All @@ -27,11 +29,14 @@ pub(crate) fn create(
Option<&Velocity>,
Option<&Damping>,
Option<&RotationConstraints>,
Option<&RigidBodyDominance>,
),
Without<super::RigidBodyHandle>,
>,
) {
for (entity, transform, body, velocity, damping, rotation_constraints) in query.iter() {
for (entity, transform, body, velocity, damping, rotation_constraints, dominance) in
query.iter()
{
let mut builder = RigidBodyBuilder::new(body_status(*body))
.user_data(entity.to_bits().into())
.position((transform.translation, transform.rotation).into_rapier());
Expand Down Expand Up @@ -63,6 +68,10 @@ pub(crate) fn create(
builder = builder.linear_damping(d.linear).angular_damping(d.angular);
}

if let Some(d) = dominance {
builder = builder.dominance_group(d.dominance);
}

let rigid_body_handle = bodies.insert(builder.build());

handles.insert(entity, rigid_body_handle);
Expand Down