Android Java rectangle collision detection not working
Posted
by
Charlton Santana
on Game Development
See other posts from Game Development
or by Charlton Santana
Published on 2012-04-12T22:33:59Z
Indexed on
2012/04/12
23:46 UTC
Read the original article
Hit count: 313
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 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?
© Game Development or respective owner