views:

107

answers:

3

I'm trying to determine whether Object1 will collide with Object2 given the following information:

1) Objects' bounding boxes (uses bounded-box collision detection)

2) Objects' speeds

3) Object's current locations (x, y coordinate)

4) Objects' directions (Up, Down, Left, or Right)

For a bit of imagery, imagine the objects traveling on a 2D grid, and they can only move on the lines of that grid.

So given the above information, I need an efficient, but readable algorithm to determine whether those objects will collide. By efficient I mean constant time with time spent on computations minimized. Psuedocode or a link is fine.

+3  A: 

First, find the time interval during which the boxes will overlap on the X axis. Second, find the time interval during which the boxes will overlap on the Y axis. Finally, check if the two time intervals overlap. If so, the earliest point in time that is in both intervals is the moment they are going to collide.

tdammers
Still not getting how to do this. I.e. how do I find the time intervals.
lotad
Find the difference in velocity, then act as if one object were moving at that velocity and the other one stood still. Find the linear equation that describes this movement over time. Solve for the two cases where left1 = right2 and where right1 = left2. These are the boundaries of your interval.
tdammers
I mean, I know how to get the time intervals, just not how to handle it with the different directions.
lotad
And if the velocities of both objects are equal?
lotad
Forget it, velocity is 1d vector so if the velocities were the same they would never collide.
lotad
+1  A: 

Your best approach is to work out:

  1. The linear time range (possibly never) in which the x co-ordinates will overlap
  2. The linear time range (possibly never) in which the y co-ordinates will overlap

And then test if the two time ranges intersect. This will as an added bonus give you the collision time.

This will be a simple constant time operation overall.

mikera