dimforge / dimforge/bevy_rapier

Limited RigidBody rotation in 2d

Open
#246 4 comments 1 reaction 0 assignees View on GitHub
A-Integration C-Enhancement D-Difficult P-Low S-not-started
Dominant language
Rust
Stars
1.6k
Forks
282
PR merge metrics
No merged PRs in 30d

Description

I've noticed that directly altering the rotation of an entity containing a `RigidBody` component is only possible around the z-axis; however, entities without a `RigidBody` (even entities containing a `Collider`) can have their rotations directly altered:

```rust
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(RapierPhysicsPlugin::::pixels_per_meter(10.))
.add_startup_system(setup)
.add_system(flip_colliders)
.add_system(flip_rigid_bodies)
.add_system(test)
.run();
}

fn setup(
mut commands: Commands
) {
// spawn entity with collider
commands
.spawn()
.insert(Collider::cuboid(10., 10.))
.insert(Transform::from_rotation(Quat::IDENTITY))
.insert(GlobalTransform::default());

// spawn entity with rigid body
commands
.spawn()
.insert(RigidBody::Dynamic)
.insert(Transform::from_rotation(Quat::IDENTITY))
.insert(GlobalTransform::default());
}

fn flip_colliders(
mut colliders: Query<&mut Transform, Added>,
) {
for mut transform in colliders.iter_mut() {
transform.rotation = Quat::from_rotation_y(std::f32::consts::PI);
}
}

fn flip_rigid_bodies(
mut rigid_bodies: Query<&mut Transform, Added>
) {
for mut transform in rigid_bodies.iter_mut() {
transform.rotation = Quat::from_rotation_y(std::f32::consts::PI);
}
}

fn test(
colliders: Query<&Transform, With>,
rigid_bodies: Query<&Transform, With>
) {
// check if collider rotation changed
for transform in colliders.iter() {
assert_eq!(transform.rotation, Quat::from_rotation_y(std::f32::consts::PI),
"Collider rotation didn't change!");
}

// check if rigid body rotation changed
for transform in rigid_bodies.iter() {
assert_eq!(transform.rotation, Quat::from_rotation_y(std::f32::consts::PI),
"RigidBody rotation didn't change!");
}
}
```

The above code panics with the following:

```
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `Quat(0.0, 0.0, 0.0, 1.0)`,
right: `Quat(0.0, 1.0, 0.0, -4.371139e-8)`: RigidBody rotation didn't change!', src\main.rs:61:9
```

Is there an effective way to instantly change the rotation of a `RigidBody`? I understand that `bevy_rapier2d` is a 2d physics engine, but the ability to rotate the entity around the x- and y-axes is especially useful for changing its orientation (e.g., flipping the player sprite in a platformer).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.