Collision Detection Using Java2D

My current setup is only useful after a collision has been made; obviously there must be something better than this?

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

How can I perform a preliminary collision detection?

+3
source share
1 answer

Typically, you precompute one step forward, something like this:

Inside the 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 to me. It's nice to see SO update response notifications.

0
source

Source: https://habr.com/ru/post/1742211/


All Articles