开发者

Java2D Distance Collision Detection

开发者 https://www.devze.com 2022-12-28 05:48 出处:网络
My current setup is only useful once collision has been made; obviously there has to be something better than this?

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

public boolean CollisionCheck(Rectan开发者_开发知识库gle rect1, Rectangle rect2) {
   if(rect1.intersects(rect2)) {
      return true;
   }
   return false;
}

How can I do preemptive collision detection?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消