Sign up ×

I had been hard coding a collision detection system which was buggy. Then I came across using rectangles for collsion detection. So I put it all in and it does not work, I put a log in and it never logged.

Note to Java programmers who are not Android programers: Android uses the word Rect instead of Rectangle.

Code for

public Rect getBounds(){
    return new Rect (this.x, this.y, 10, 20);

Code for

public Rect getBounds(){
    return new Rect (this.x, this.y, 20, 20);

Code for

for(Block block : BLOCKS) {

    Rect spriter = sprite.getBounds();
    Rect blockr = block.getBounds();

        showgameover = 1;
        Log.d(TAG, "Game Over");


Is anyone able to help?

share|improve this question
What is block.rigidbody() doing? Is there a difference between the rigidbody's scale and the canvas scale (pixel/meter)? –  Chuck D Apr 13 '12 at 1:17
oh that was to do with my old collision detection.. –  Charlton Santana Apr 13 '12 at 13:19
Old question, but new Rect() calls in the OP are wrong. They should be new Rect(this.x, this.y, (this.x + 10), (this.y + 20)). This could have lead to many issues for the original poster. –  Tigger Apr 2 '14 at 3:00

2 Answers 2

Have you tried the debugger? The code looks like it should work, if the sprite and a block are indeed colliding.

Here's what you do, create a sprite and a block at the same position. Have a breakpoint on your if(spriter.intersect(blockr)) statement. When it breaks inspect the elements rectangle values to ensure they do intersect.

I imagine that if these are currently intersecting you're just not set up to see the log properly and/or not doing anything with the showgameover variable.


According to the documentation you're using the intersect method correctly.

One of the few remaining things to check is to ensure that your for(Block block : BLOCKS) is actually getting run. What I told you to do with the debugger should help answer that. Otherwise you can stick a Log.d() inside the loop and ensure that it's actually being run. But I recommend the debugger method, as that's more useful in the long run.

share|improve this answer
The showgameover does things which worked when i had my old collision detection system but i changed because it was buggy. the log.. is the same as the other logs in this java file. the only thing i could think could be different is that in java twithout android instead if intesect you put intersects but i got error and said it should be intersect (il have a look in the android jar) :) This is why im confused as well because it looks like it should work. –  Charlton Santana Apr 12 '12 at 22:56
See my edit for more info –  Byte56 Apr 12 '12 at 23:04
for(Block block : BLOCKS) is defitnely running otherwize it would not draw the blocks to the screen. –  Charlton Santana Apr 12 '12 at 23:08
Then your rectangles are not intersecting. If you're not using the debugger make a function that takes a Rect and draws it on screen. It depends on what you're using but, something like this may work. –  Byte56 Apr 12 '12 at 23:12
you able to give me an exaple of the function ... :) –  Charlton Santana Apr 12 '12 at 23:14

I am not sure if this is your problem, but I struggled with a subtle intersection bug for a long time. I traced it to Android's implementation of intersects:

public boolean intersects(int left, int top, int right, int bottom) {
     return this.left < right && left < this.right
            && < bottom && top < this.bottom;

Whereas the algorithm I was using assumed that 'touching' rects are intersecting, using the following implementation:

public static boolean intersects(Rect rect, Rect otherRect) {
    return  rect.left <= otherRect.right && otherRect.left <= rect.right
        && <= otherRect.bottom && <= rect.bottom;

Additionally, you should probably be using the intersects method instead of intersect as the latter modifies the rectangle (of the sprite rect in your case) to match the intersection area.

share|improve this answer
where should i put these .. in the and –  Charlton Santana Apr 12 '12 at 23:29
I'd ideally put it in one place only (utility class, base class of sprite and block, etc.) but if you just want to see if that's the problem just place the second piece of code in and call it as if (intersects(spriter, blocker)) { ... –  Firas Assaad Apr 12 '12 at 23:35

Your Answer


