Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LDTK region strange in runtime #267

Closed
darkautism opened this issue Nov 24, 2023 · 9 comments · Fixed by #271
Closed

LDTK region strange in runtime #267

darkautism opened this issue Nov 24, 2023 · 9 comments · Fixed by #271

Comments

@darkautism
Copy link

pub struct PlayerChatBundle {
    #[with(player_chat_collider)]
    collider: Collider,
    #[with(player_chat_active_events)]
    active_events: ActiveEvents,
    sensor: Sensor,
    #[with(Dialogue::from_field)]
    pub value: Dialogue,
}
fn player_chat_active_events(_: &EntityInstance) -> ActiveEvents {
    ActiveEvents::COLLISION_EVENTS
}
fn player_chat_collider(ei: &EntityInstance) -> Collider {
    info!("{:?}, {:?}", ei.grid.x as f32 / 2., ei.grid.y as f32 / 2.);
    info!("{:?}, {:?}", ei.height as f32 / 2., ei.width as f32 / 2.);
    Collider::cuboid(1. , 1.)
}

In main.rs:
            .register_ldtk_entity::<PlayerChatBundle>("PlayerChat")

image
image

I have testd these collider

Collider::cuboid(1. , 1.)
Collider::cuboid(ei.width as f32 / 2., ei.height as f32 / 2)
Collider::cuboid(ei.grid.x as f32 / 2., ei.grid.y as f32 / 2.)

The first region work great after i set Collider::cuboid(8. , 8.), 16*16 in ldtk editor.
But the second region size strange again.

image
image

It seems ldtk entity will change Collider i created.
So i don't know what is right way to set a collider sensor for region which i want to active some game event. What should i do?

@Trouv
Copy link
Owner

Trouv commented Nov 24, 2023

My best guess for what's causing this is bevy_ecs_ldtk's logic for scaling entities. It basically does transform.scale = entity_instance_size / entity_definition_size..

So it looks like maybe you've tried this, but what happens if you hardcode the cuboid to be half the size of the entity definition? This value specifically:
entity_definition_size_001

@darkautism
Copy link
Author

I tested this.
image
image
Only first region is fit.
image
image

image
image

@Trouv
Copy link
Owner

Trouv commented Nov 24, 2023

I see - thanks for elaborating on that.

It's a very strange issue. Out of curiosity - does the collider shape change at all if you switch them? Like registering the PlayerChatBundle to the ToolTips identifier? .register_ldtk_entity::<PlayerChatBundle>("ToolTips")

@darkautism
Copy link
Author

image
image

It seems different size have different behavior.

@Trouv
Copy link
Owner

Trouv commented Nov 25, 2023

Could you use bevy-inspector-egui to find out the scale of those entities? Or print them in a system? It looks like the left one is supposed to be (10,12,1) and the right (1,10,1),

@darkautism
Copy link
Author

image
image

@Trouv
Copy link
Owner

Trouv commented Nov 28, 2023

I think I have an idea of what might be happening. It looks like you might be using a Tile field on the ToolTips entity? What happens if you remove that field?

@darkautism
Copy link
Author

image
image

It seems ok if i change tooltip tile to square.

@Trouv
Copy link
Owner

Trouv commented Dec 2, 2023

#271 should fix this. I'd ask you to try it but there have been a lot of breaking changes since 0.8 that could be annoying to deal with

@Trouv Trouv closed this as completed in #271 Dec 5, 2023
Trouv added a commit that referenced this issue Dec 5, 2023
…size as basis when calculating ldtk entity scale (#271)

Closes #267 

LDtk populates an `EntityInstance`'s `__tile` field if any tile is used
when visualizing that entity in-level. This is normally just the
entity's editor visual, but if that entity is given a singular tile
field instance and has no editor visual, it might use that instead.
Currently, `bevy_ecs_ldtk` uses this field to calculate the scale when
spawning `EntityInstance`s. This can lead to unexpected behavior. The
scale of entity instances with no entity visual should correlate to the
size of the instance in the level. This might not be the case for
entities that have a tile field despite having no editor visual. The
solution implemented here is to use the `tileRect` field of the entity
definition instead of the `__tile` field of the entity instance.

To be honest, I'm not totally satisfied with this solution either. The
entity spawning process is currently designed to match the visuals in
the editor when using sprites. However, it's not perfect at doing this,
and using the transform's scale to accomplish this makes the logic
affect a lot more components than just the sprite. It also affects
colliders, for example. However, for now, this should be a satisfactory
fix that does not affect many users negatively
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants