KRB
KRB

Reputation: 5175

"Simple" Rectangle Collision Detection: Why doesn't my code work?

Hey guys, please take a look at my code and see what's wrong here. I looked at the docs and everything and it seems like this should work.

public Boolean CollisionTest (Rect one, Rect two) {
        if (one.intersect(two)) {
            collided = true;
        } else {
            collided = false;
        }
        return(collided);
    }

Should this not return if two rectangles collide? The reason I am doubting this I am having some null pointer exception inside my Main Thread (it is stopping on my finally statement for my game loop thread) errors when debugging and when I do not use this function it is fine.

Very weird, also I would appreciate if anyone could post links to useful collision detection tutorials. I want to deal with my own collision detection and not use outside libraries.

Thanks EVERYONE!

Upvotes: 2

Views: 8890

Answers (4)

Wroclai
Wroclai

Reputation: 26925

Well, I'm not familiar with the Rect API but @Donut has answered this part well. Since you're also wondering about collision detection I will leave two very good tutorials about it.

I do have experiences in making a tile map system and a basic collision detection for a game, in Java (on an Android phone). This and this were very good tutorials and pretty straightforward and they contains basic collision detection.

Upvotes: 1

Donut
Donut

Reputation: 112815

According to the Android Developers documentation for the intersect function:

If the specified rectangle intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle.

The part that I added emphasis to means that your one parameter could be changed if the rectangles do intersect -- which I'm guessing is how it's somehow getting set to null, and causing your error later on in the game loop.

The documentation also states:

To just test for intersection, use intersects().

A description of the Rect.intersects(Rect a, Rect b) method is available here.

If we modify your method to use Rect.intersects, it would look like this:

public Boolean CollisionTest (Rect one, Rect two) {
        return Rect.intersects(one, two);
    }

At this point you could probably get rid of CollisionTest altogether and just call Rect.intersects directly -- unless at some point you wanted to implement your own collision detection. In that case, you'd just have to modify this one method. It's up to you, really.

Upvotes: 12

chaitanya
chaitanya

Reputation: 1601

I see that you have not defined collided over here in the function. Is that defined in the global space or as an instance variable.

Also you can shorten your code

public Boolean CollisionTest (Rect one, Rect two) {
    if (one.intersect(two)) {
        collided = true;
    } else {
        collided = false;
    }
    return(collided);
}

as

return one.intersect(two);

Upvotes: 1

Jim Blackler
Jim Blackler

Reputation: 23169

You could write a check like this:

public boolean CollisionTest(Rect one, Rect two) {
  return one.left <= two.right && one.right >= two.left &&
      one.top <= two.bottom && one.bottom >= two.top;
}

Upvotes: 1

Related Questions