6

Ive created a game where you move a rectangle and dodge other falling rectangles from the sky. Though everytime the rectangles intersect nothing happens.

if(mSquare.intersect(jSquare)){ canvas.drawColor(Color.BLACK);
or

collision = mSquare.intersect(jSquare);
     if(collision==true){  canvas.drawColor(Color.RED);
  }  this always returns false no matter where the rectangles are....... 
Kohler Fryer
  • 764
  • 2
  • 8
  • 17

1 Answers1

5

There are a lot of ways to do this, the simplest would be to get the bounding Rect for each Bitmap and on each time step to check for a collision using Rect.intersect() method.

Something like this:

boolean collision = player.getRect().intersect(fallingObject.getRect());

Additionally there are many other (better) ways to do this, especially when dealing with objects that aren't rectangles and when you have lots of objects on the screen. Check out this post for a good discussion

Also the book "Beginning Android Games" has a great chapter about collision detection and the book is well worth a read if you are considering writing a game.

slayton
  • 20,123
  • 10
  • 60
  • 89
  • Ive tried a bunch of things but nothing seems to work..... Here is how far i got but these methods dont work... if (Rect.intersects(mSquare, mSquare)){ canvas.drawColor(Color.LTGRAY); } ................. or.......... boolean collision = mSquare.intersect(jSquare); if (collision == true){ canvas.drawColor(Color.LTGRAY); } The canvas never changes! please help im screwed. – Kohler Fryer Mar 01 '12 at 18:05
  • 1
    `Rect.intersect` is **NOT** a static method, meaning it needs to be called on an instance of an object. The correct way to check for an intersetction between two rectangles, lets call them `r1` and `r2` is to call the method like this: `r1. intersect(r2)` **NOT** `Rect.intersect(r1,r2)` see the android Rect docs for more information http://developer.android.com/reference/android/graphics/Rect.html – slayton Mar 01 '12 at 18:30
  • Alright that makes sense but ive tried that...Maybe I cant check for an intersection on a canvas...? – Kohler Fryer Mar 01 '12 at 18:49
  • can you post an update to your question with what code you've tried and describe why it isn't working? – slayton Mar 01 '12 at 19:08
  • Alright. Thanks so much for the help man it means a lot foreals. – Kohler Fryer Mar 01 '12 at 19:17
  • boolean collision = mSquare.intersect(jSquare);.................. if(collision==true){ canvas.drawColor(Color.BLACK); } – Kohler Fryer Mar 01 '12 at 19:23
  • It should be working but it doesnt give me a true or false return – Kohler Fryer Mar 01 '12 at 19:32
  • if(mSquare.intersect(jSquare)){ canvas.drawColor(Color.BLACK);} --also doesnt work – Kohler Fryer Mar 01 '12 at 19:33
  • Try using Log messages to verify the data in the Rect objects – slayton Mar 01 '12 at 20:30
  • thanks You set me in the right direction ... took me forever but i found out that my stupid rectangle didnt follow this sizing ---left <= right and top <= bottom. – Kohler Fryer Mar 12 '12 at 21:10