views:

65

answers:

1

My current setup is only useful once collision has been made; obviously there has to be something better than this?

public boolean CollisionCheck(Rectangle rect1, Rectangle rect2) {
   if(rect1.intersects(rect2)) {
      return true;
   }
   return false;
}

How can I do preemptive collision detection?

A: 

Generally, you pre-compute one step ahead, something like this:

Inside Rectangle class:

public void move()
{
    rec.x += rec.dx
    rec.y += rec.dy
}

Then,

public boolean CollisionCheck(Rectangle rect1, Rectangle rect2) {
   rec1.move();
   rec2.move();
   if(rect1.intersects(rect2)) {
      return true;
   }
   return false;
}

Ha. Travis got in before I did. Nice to see SO has update answer notifications.

Xavier Ho
Yes however I feel that your method is better; I was oblivious on how to "predict" the movement; that's why I posted. Thanks
No worries. You can also call `move()` and `CollissionCheck()` in succession, if you need the flexibility. +1 for a useful question.
Xavier Ho