views:

107

answers:

4

Okay I'm working on a Space sim and as most space sims I need to work out where the opponents ship will be (the 3d position) when my bullet reaches it. How do I calculate this from the velocity that bullets travel at and the velocity of the opponents ship?

+1  A: 

Collision Detection by Kurt Miller
http://www.gamespp.com/algorithms/collisionDetection.html

Robert Harvey
+1  A: 

Add the negative velocity of the ship to the bullet, so that only the bullet moves. Then calculate the intersection of the ship's shape and the line along which the bullet travels (*pos --> pos + vel * dt*).

Josef Grahn
A: 

The question probably shouldn't be "where the ship will be when the bullet hits it," but IF the bullet hits it. Assuming linear trajectory and constant velocity, calculate the intersection of the two vectors, one representing the projectile path and another representing that of the ship. You can then determine the time that each object (ship and bullet) reach that point by dividing the distance from the original position to the intersection position by the velocity of each. If the times match, you have a collision and the location at which it occurs.

If you need more precise collision detection, you can use something like a simple BSP tree which will give you not only a fast way to determine collisions, but what surface the collision occurred on and, if handled correctly, the exact 3d location of the collision. However, it can be challenging to maintain such a tree in a dynamic environment.

David Lively
+1  A: 

Calculate the relative velocity vector between him and yourself: this could be considered his movement if you were standing still. Calculate his relative distance vector. Now you know that he is already D away and is moving V each time unit. You have V' to calculate, and you know it's length but not it's direction.

Now you are constructing a triangle with these two constraints, his V and your bullet's V'. In two dimensions it'd look like:

Dx+Vx*t = V'x*t

Dy+Vy*t = V'y*t

V'x^2 + V'y^2 = C^2

Which simplifies to:

(Dx/t+Vx)^2 + (Dy/t+Vx)^2 = C^2

And you can use the quadratic formula to solve that. You can apply this technique in three dimensions similarly. There are other ways to solve this, but this is just simple algebra instead of vector calculus.

Adam Luter
As someone pointed out, there is a matter of IF, for instance if the ship is faster than the bullet, then it's very likely your bullet will never arrive. In that case the quadractic formula will return a complex number.
Adam Luter