0
\$\begingroup\$

I had been hard coding a collision detection system which was buggy. Then I came across using rectangles for collision 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 Block.java:

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

Code for Sprite.java:

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

Code for MainGame.java:

for(Block block : BLOCKS) {
    block.draw(canvas);
    block.rigidbody();

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

    if(spriter.intersect(blockr)){
        showgameover = 1;
        Log.d(TAG, "Game Over");
    }

}

Is anyone able to help?

\$\endgroup\$
3
  • \$\begingroup\$ What is block.rigidbody() doing? Is there a difference between the rigidbody's scale and the canvas scale (pixel/meter)? \$\endgroup\$
    – Chuck D
    Commented Apr 13, 2012 at 1:17
  • \$\begingroup\$ oh that was to do with my old collision detection.. \$\endgroup\$ Commented Apr 13, 2012 at 13:19
  • \$\begingroup\$ 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. \$\endgroup\$
    – Tigger
    Commented Apr 2, 2014 at 3:00

2 Answers 2

2
\$\begingroup\$

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.

EDIT

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.

\$\endgroup\$
6
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Apr 12, 2012 at 22:56
  • \$\begingroup\$ See my edit for more info \$\endgroup\$
    – House
    Commented Apr 12, 2012 at 23:04
  • \$\begingroup\$ for(Block block : BLOCKS) is defitnely running otherwize it would not draw the blocks to the screen. \$\endgroup\$ Commented Apr 12, 2012 at 23:08
  • \$\begingroup\$ 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. \$\endgroup\$
    – House
    Commented Apr 12, 2012 at 23:12
  • \$\begingroup\$ you able to give me an exaple of the function ... :) \$\endgroup\$ Commented Apr 12, 2012 at 23:14
0
\$\begingroup\$

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
            && this.top < 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
        && rect.top <= otherRect.bottom && otherRect.top <= 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.

\$\endgroup\$
2
  • \$\begingroup\$ where should i put these .. in the sprite.java and block.java? \$\endgroup\$ Commented Apr 12, 2012 at 23:29
  • \$\begingroup\$ 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 MainGame.java and call it as if (intersects(spriter, blocker)) { ... \$\endgroup\$ Commented Apr 12, 2012 at 23:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .