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

Commit

Permalink
Introduce rotation contraints
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Mar 8, 2021
1 parent ed90487 commit 3bb189d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
38 changes: 38 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl Plugin for CorePlugin {
.register_type::<BodyType>()
.register_type::<PhysicMaterial>()
.register_type::<Velocity>()
.register_type::<RotationConstraints>()
.add_stage_after(bevy::app::stage::UPDATE, crate::stage::ROOT, {
let mut schedule = Schedule::default();

Expand Down Expand Up @@ -276,3 +277,40 @@ impl Default for PhysicMaterial {
}
}
}

/// Components that restrict rotations due to forces
///
/// Note that angular velocity may still be applied programmatically.
#[derive(Debug, Copy, Clone, Reflect)]
pub struct RotationConstraints {
/// Set to true to prevent rotations around the x axis
pub allow_x: bool,

/// Set to true to prevent rotations around the y axis
pub allow_y: bool,

/// Set to true to prevent rotations around the Z axis
pub allow_z: bool,
}

impl Default for RotationConstraints {
fn default() -> Self {
Self {
allow_x: true,
allow_y: true,
allow_z: true,
}
}
}

impl RotationConstraints {
/// Lock rotations around all axes
#[must_use]
pub fn lock() -> Self {
Self {
allow_x: false,
allow_y: false,
allow_z: false,
}
}
}
23 changes: 21 additions & 2 deletions rapier/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::math::prelude::*;
use bevy::transform::prelude::*;
use fnv::FnvHashMap;

use heron_core::{Body, BodyType, PhysicMaterial, Velocity};
use heron_core::{Body, BodyType, PhysicMaterial, RotationConstraints, Velocity};

use crate::convert::{IntoBevy, IntoRapier};
use crate::rapier::dynamics::{
Expand All @@ -29,17 +29,36 @@ pub(crate) fn create(
Option<&BodyType>,
Option<&Velocity>,
Option<&PhysicMaterial>,
Option<&RotationConstraints>,
),
Without<BodyHandle>,
>,
) {
for (entity, body, transform, body_type, velocity, material) in query.iter() {
for (entity, body, transform, body_type, velocity, material, restrict_rotation) in query.iter()
{
let body_type = body_type.cloned().unwrap_or_default();

let mut builder = RigidBodyBuilder::new(body_status(body_type))
.user_data(entity.to_bits().into())
.position((transform.translation, transform.rotation).into_rapier());

#[allow(unused_variables)]
if let Some(RotationConstraints {
allow_x,
allow_y,
allow_z,
}) = restrict_rotation.cloned()
{
#[cfg(feature = "2d")]
if !allow_z {
builder = builder.lock_rotations();
}
#[cfg(feature = "3d")]
{
builder = builder.restrict_rotations(allow_x, allow_y, allow_z);
}
}

if let Some(v) = velocity {
#[cfg(feature = "2d")]
{
Expand Down

0 comments on commit 3bb189d

Please sign in to comment.