#collide_aabb detecting constant collisions even though there is only 1 sprite in game

29 messages · Page 1 of 1 (latest)

noble stag
#

I basically ripped the collision function straight out of the breakout example, and even if I remove everything except the "ball" sprite, it spams the collision event.

fn check_bounce(mut commands: Commands,
                mut ball_query: Query<(&mut Velocity, &Transform)>,
                collider_query: Query<(Entity, &Transform)>,
                mut collision_events: EventWriter<CollisionEvent>,
){

    let (mut ball_velocity, ball_transform) = ball_query.single_mut();
    let ball_size = ball_transform.scale.truncate();

    for (collider_entity, transform, ) in &collider_query {
        let collision = collide(
            ball_transform.translation,
            ball_size,
            transform.translation,
            transform.scale.truncate(),
        );
        if let Some(collision) = collision {
            println!("Collision detected.");
            // Sends a collision event so that other systems can react to the collision
            collision_events.send_default();


        }
    }
}

Anyone experienced this before?

#

Here is the full file if you're daring enough.

bleak grail
#

Try adding a check that ball entity <> collider entity

noble stag
#

As in,make sure its not colliding with itself?

bleak grail
#

or if you prefer, without<velocity> to the collider query

#

indeed. as written, I would definitely expect it to collide with itself

noble stag
#

what gave it away

bleak grail
#

the fact that you're iterating through all transforms and comparing them to one transform, I suppose

#

and if there's only one sprite in the game, then what else is it going to be colliding with?

noble stag
#

good point

#

How can I grab the actual entity in a one liner given that the ball query should only have one result

#

i know .single_mut() exists, but it seems like it gets the components not the entity

#

for context im trying to do: rust if let Some(collision) = collision && collider_entity != ball_query.single_mut() {

bleak grail
#

probably collider_query: Query<(Entity, &Transform, Without<Velocity>)>, would be the smallest way to do it in this case

#

otherwise I'd just add entity to the ball query and literally test equality in the loop

noble stag
#

yerp that was it

#

for some reason when I do ```Rust
if (let Some(collision) = collision && ball_entity == collider_entity){

#

but

#

if I do the ball_entity check seperately it runs

#
error: expected expression, found `let` statement
   --> src/main.rs:111:13
    |
111 |         if (let Some(collision) = collision && ball_entity == collider_entity) {
    |             ^^^

#

but regardless, thank you for showing me how to figure that out! @bleak grail

bleak grail
#

No worries, I've traipsed through a few misbehaving collision detection systems before so I had some ideas what I was looking for already 🙂

noble stag
#

I've just left it as two nested if statements

#

I can't seem to find anything about combining an if statement that includes a variable definition and a comparator.

strange bough
noble stag
strange bough
#

it's an option

#

you can also use a match