Trouv / bevy_ecs_ldtk

ECS-friendly ldtk plugin for bevy, leveraging bevy_ecs_tilemap
Other
672 stars 76 forks source link

Specifying pixels per unit #283

Open piedoom opened 9 months ago

piedoom commented 9 months ago

Hey! I am using this with bevy_xpbd physics which does not seem to have any sort of scaling. Ideally, my tilemap is 16x16, so it'd be useful if 16px was equal to 1 bevy unit - but currently, I believe every pixel is 1 bevy unit. This leads to xpbd's physics needing to be objects that would be 16x16m, with a weird mass for a visually small object.

Is there a way to specify the scale of how many units a tile should render for? Sorry if this isn't related to this crate specifically.

I see a potential workaround where I an set the LdtkWorldBundle Transform to Transform::default().with_scale(Vec2::splat(1f32 / 16f32).extend(1f32)), but then I need to find the GlobalTransform whenever I want to find the actual position of something in the world which seems like the incorrect route.

Trouv commented 9 months ago

I think I understand what the problem is. Just to be clear - your issue is with spawning colliders for tiles specifically right? Not Entity-layer entities?

I do have one question - are these tiles w/ collisions going to be plenty? Like a rectangle of wall tiles that could theoretically be combined into one? This is a little bit off-topic but in my experience with other physics engines having 1 collider per tile is pretty unperformant and you should probably have some logic that instead post-processes tiles into fewer collider entities. The platformerexample does this - see these code snippets:

Part of the reason that I bring this up is that, even if you do want 1 collider-per-tile, adding this collision in post-processing (maybe as a child of the tile if you can't set the scale on the tile itself?) might be the best option for giving it the appropriate size.

piedoom commented 9 months ago

I believe I understand correctly. For my first run I intend to have a collider per tile, and then optimize to combine where I can.

Trouv commented 9 months ago

Well if you really want one collider per tile you could probably do something a little simpler than what the platformer example does, but similar. Register some marker component to spawn for those tiles with LdtkIntCell and then spawn colliders as children of those tiles in a later system...

fn spawn_colliders_for_wall_tiles(commands: mut Commands, marker_query: Query<Entity, Added<MyMarkerComponent>>) {
    for marked_entity in marker_query {
        let collider = commands
            .spawn(
                // collider components...
            )
            .id();
        commands.entity(marked_entity).add_child(collider);
    }
}