#2D and bevy_rapier2d issues with rendering and intersection checking.

3 messages · Page 1 of 1 (latest)

severe kelp
#

Hello! I have two large issues in a fairly simple codebase (200lines of code). I wish to use SI units in this system, that is when I render rectangles, I want to use 1.0 as 1 meter.

I spawn a rectangle, surrounded by rectangles in a circle, but no boxes are shown.

code:

    commands.spawn((
        Camera2dBundle {
            projection: OrthographicProjection {
                scale: 0.1,
                ..default()
            }
            ..default()
        },
    ));

    // Rectangle representing the center
    commands
        .spawn((
            SpriteBundle {
                sprite: Sprite {
                    color: Color::ORANGE,
                    custom_size: Some(Vec2::new(12.0, 1.0)),
                    ..default()
                },
                transform: Transform::from_translation(Vec3::new(0., 0., 3.)).with_scale(Vec3::ONE),
                ..default()
            },
        ))
        .with_children(|parent| {
            for dir in 0..directions {
//omitted code for charactedlimit
             let mut transform = Transform::from_translation(Vec3::new(dist, 0., 33.)).with_scale(Vec3::ONE);
                    transform.rotate_around(Vec3::ZERO, Quat::from_rotation_z(rad));
                    // Rectangle
                    parent.spawn((
                        Bar,
                        SensorBundle {
                            collider: Collider::cuboid(width / 2., height / 2.),
                            ..default()
                        },
                        SpriteBundle {
                            sprite: Sprite {
                                color: Color::YELLOW,
                                custom_size: Some(Vec2::new(width, height)),
                                ..default()
                            },
                            transform,
                            ..default()
                        },
                    ));
                }
            }
        });

severe kelp
#

Okay this was solved, I had the 2D camera placed in Z = 0, which means that everything rendered was "above" the camera. Just putting the Camera at a higher Z made it work

obtuse depot
#

This might be useful for you:

If you initialize Rapier with the following code,

app.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(
  PIXELS_PER_METER,
));

Where PIXELS_PER_METER is an f32 value, you can use pixel units in your code and the physics engine will translate them with said value when doing calculations.