开发者

rectangle collision detections on a path

I have two rectangles, one is moving and one is stationary. I have the size of both rectangles and both the current coordinates of the moving rectangle and the coordinates of where it wants to go. It will move in a straight line. With this information, what is the most effective way to find if the two boxes will collide on the path. Is it more efficient to do this or to move the box a small amount each fram开发者_C百科e and do a collision detection each time?


Since one of the rectangles is stationary, one way to do this is to create a polygon of the path travelled by the moving rectangle, which can be done using only its initial and final positions:

(initial position)
+--------+
|\       |\
| \      | \
+--\-----+  \
 \  \     \  \
  \  +-----\--+
   \ |      \ |
    \|       \|
     +--------+
      (final position)

Path polygon:

+--------+
|         \
|          \
+           \
 \           \
  \           +
   \          |
    \         |
     +--------+

Then use a normal polygon collision detection algorithm between the stationary rectangle and the path polygon.


If it's moving at a constant speed the most efficient way is to calculate when they will collide (Time = Speed/Distance) and if they will collide (Derive a linear equation of motion, and see if it will pass through the stationary rectangle). If it's moving at a variable speed, perform these calculations each time the speed changes. These are both far more efficient than comparing for each frame.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜