dimforge / dimforge/bevy_rapier

Border radius of Collider::round_cuboid does not scale correctly with GlobalTransform in 2D.

Open
#590 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.6k
Forks
282
PR merge metrics
No merged PRs in 30d

Description

TL;DR: `round_cuboid` border radius does not scale correctly with `GlobalTransform`.

In the following example, I spawn a parent entity that is scaled down to 0.1 unit. Then, I spawn two children for that parent, one with a `Collider::round_cuboid(0.0, 0.0, 0.5)` and another with `Collider::ball(0.5)`. In this case, I expect both colliders to be basically the same, however the `round_cuboid` is 10x larger than the `ball`.

Then, when I spawn another child of the parent entity, but now with `Collider::round_cuboid(0.25, 0.25, 0.25)`, we can see that the collider is nearly half as small as the first `round_cuboid`.
That shows me that the border radius of `Collider::round_cuboid` does not scale correctly with the `GlobalTransform` of an entity.
To show that this does not affect only the debug renderer, I added a dynamic rigidbody (in red) that sits on top of the other colliders.
![Screenshot_20241004_130324](https://github.com/user-attachments/assets/94b624f3-2b29-4ddc-9377-17d3f53c8433)

The following code produces the example of which the attached screenshot was taken.

```rs
use bevy::{prelude::*, render::camera::ScalingMode};
use bevy_rapier2d::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins((
RapierPhysicsPlugin::::pixels_per_meter(1.0),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, setup)
.run();
}

fn setup(mut commands: Commands) {
// Setup the camera.
let mut cam = Camera2dBundle::default();
// Makes the height of the unit exactly one window, for easy reference.
cam.projection.scaling_mode = ScalingMode::FixedVertical(1.0);
commands.spawn(cam);

// Spawn the scaled-down parent entity.
commands
.spawn(SpatialBundle::from_transform(Transform::from_scale(
Vec3::splat(0.1),
)))
// Spawn the three children.
.with_children(|commands| {
commands.spawn((
SpriteBundle {
transform: Transform::from_xyz(-4.0, 0.0, 0.0),
..default()
},
Collider::round_cuboid(0.0, 0.0, 0.5), // Expect this...
));
commands.spawn((
SpriteBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default()
},
Collider::ball(0.5), // ... to be effectively the same as this.
));
commands.spawn((
SpriteBundle {
transform: Transform::from_xyz(4.0, 0.0, 0.0),
..default()
},
Collider::round_cuboid(0.25, 0.25, 0.25), // Compare this to the first child.
));
});

// The following entity shows that it not only affects the debug renderer, but also the
// simulation.
commands.spawn((
SpatialBundle::from_transform(Transform::from_xyz(0.0, 1.0, 0.0)),
RigidBody::Dynamic,
Collider::ball(0.1),
));
}
```

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.