Skip to content

Sprite AABB does not seem to update with position of the sprites #14422

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

Open
alice-i-cecile opened this issue Jul 21, 2024 · 0 comments
Open
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong

Comments

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Jul 21, 2024

Bevy version

0.14

What you did

image

In this picture the two boxes represent the AABBs of the fireball and the small dude in the top-left respectively.

Normal 2D setup with DefaultPlugins and default features. I'm fetching the AABBs gizmos like so:

fn check_for_projectile_collisions(
    projectile_query: Query<(&Aabb, &Target), With<Projectile>>,
    unit_query: Query<&Aabb, With<Id<Unit>>>,
    mut gizmos: Gizmos,
) {
    for (projectile_aabb, target) in projectile_query.iter() {
        // TODO: handle case where no specific target is set
        let target_entity = target.entity.unwrap();
        let Ok(target_aabb) = unit_query.get(target_entity) else {
            // The unit may have been despawned.
            continue;
        };

        let projectile_bounding_box = Aabb2d::new(
            projectile_aabb.center.truncate(),
            projectile_aabb.half_extents.truncate(),
        );

        let target_bounding_box = Aabb2d::new(
            target_aabb.center.truncate(),
            target_aabb.half_extents.truncate(),
        );

        let gizmo_color = if projectile_bounding_box.intersects(&target_bounding_box) {
            Color::from(LinearRgba::RED)
        } else {
            Color::WHITE
        };

        gizmos.rect_2d(
            projectile_aabb.center.truncate(),
            Rot2::radians(0.),
            projectile_aabb.half_extents.truncate(),
            gizmo_color,
        );

        gizmos.rect_2d(
            target_aabb.center.truncate(),
            Rot2::radians(0.),
            target_aabb.half_extents.truncate(),
            gizmo_color,
        );
    }
}

What went wrong

The position of the Aabb doesn't move as the sprites move. This can be quickly double-checked with bevy_inspector_egui: the center of all the Aabbs in my scene is (0,0,0).

Additional information

This is related to #11892 as its a bug in the same area of code, but I don't think it's the same root cause.

As a workaround, you can fetch the correct center using GlobalTransform.

        let projectile_center = projectile_global_transform.translation().truncate();
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen S-Needs-Investigation This issue requires detective work to figure out what's going wrong labels Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Projects
None yet
Development

No branches or pull requests

1 participant