bevy_rapier icon indicating copy to clipboard operation
bevy_rapier copied to clipboard

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

Open HeikoRibberink opened this issue 4 months ago • 0 comments

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

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

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

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins((
            RapierPhysicsPlugin::<NoUserData>::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),
    )); 
}

HeikoRibberink avatar Oct 04 '24 11:10 HeikoRibberink