#Rapier collider and sprite have different sizes and anchor points

11 messages · Page 1 of 1 (latest)

devout rover
#

I am new to game development scene so I maybe missing something obvious but I can't figure it out

I have this player entity, with a sprite that is 128*128px

pub fn spawn_player(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
    commands
        .spawn(Player)
        .insert(Name::new("Player"))
        .insert(Camera2dBundle::default())
        .insert(Player::sprite_sheet_bundle(
            &asset_server,
            &mut texture_atlases,
        ))
        .insert(Collider::capsule_y(20.0, 15.0))
        .insert(RigidBody::Dynamic)
        .insert(KinematicCharacterController::default());
}

and the collider doesn't match the size of the sprite nor have the same anchor point

lucid wedge
#

I don't have any experience with 2D but you could make the sprite a child instead, that way you can set it's offset.

Example (untested):

    commands
        .spawn(Player)
        .insert(Name::new("Player"))
        .insert(Camera2dBundle::default())
        .insert(Collider::capsule_y(20.0, 15.0))
        .insert(RigidBody::Dynamic)
        .insert(KinematicCharacterController::default())
        .with_children(|parent| {
          parent
            .spawn(Player::sprite_sheet_bundle(
              &asset_server,
              &mut texture_atlases,
            ))
            .insert(TransformBundle {
              local: Transform::from_xyz(0.0, 0.5, 0.0)
              ..default(),
            });
        });
#

local means it's relative to it's parent

wise lion
#

Hi Amr!

For 2D, you would want to use a SpriteBundle, so you can have all of that information contained into one sprite.

#
commands.spawn(SpriteBundle{..default()});

Before the ..default() part, you should be able to control parts of the sprite like the transform, and what the sprite is visually.

#

Typically Bevy likes to use Components in a struct form like so:

#[derive(Component)]
struct Player {
    jump_cooldown: Timer,
    shot_cooldown: Timer,
    shot_limit: i32,
    position: Vec2,
}

This is an example from my 2D code for a game similar to Galaga

#

I have these pieces within the Player component because I want to be able to keep track of them to allow the Player to do something.

#

Take a look at these 2D examples and they will show you how Bevy likes its code structured, and how you can use different types of Bundles to spawn things.

devout rover
devout rover