Jun 212013

In an old post I explained how to shoot an object to hit a moving target in 2D. The method in 3D is basically the same, but the code below is much cleaner and might be simpler to understand even for the 2D case.

##### Unity3D example and source code

The interesting bit of C#:

private Vector3 FindInterceptVector(Vector3 shotOrigin, float shotSpeed,

Vector3 targetOrigin, Vector3 targetVel) {

Vector3 dirToTarget = Vector3.Normalize(targetOrigin - shotOrigin);

// Decompose the target's velocity into the part parallel to the

// direction to the cannon and the part tangential to it.

// The part towards the cannon is found by projecting the target's

// velocity on dirToTarget using a dot product.

Vector3 targetVelOrth =

Vector3.Dot(targetVel, dirToTarget) * dirToTarget;

// The tangential part is then found by subtracting the

// result from the target velocity.

Vector3 targetVelTang = targetVel - targetVelOrth;

/*

* targetVelOrth

* |

* |

*

* ^...7 <-targetVel

* | /.

* | / .

* |/ .

* t---> <-targetVelTang

*

*

* s---> <-shotVelTang

*

*/

// The tangential component of the velocities should be the same

// (or there is no chance to hit)

// THIS IS THE MAIN INSIGHT!

Vector3 shotVelTang = targetVelTang;

// Now all we have to find is the orthogonal velocity of the shot

float shotVelSpeed = shotVelTang.magnitude;

if (shotVelSpeed > shotSpeed) {

// Shot is too slow to intercept target, it will never catch up.

// Do our best by aiming in the direction of the targets velocity.

return targetVel.normalized * shotSpeed;

} else {

// We know the shot speed, and the tangential velocity.

// Using pythagoras we can find the orthogonal velocity.

float shotSpeedOrth =

Mathf.Sqrt(shotSpeed * shotSpeed - shotVelSpeed * shotVelSpeed);

Vector3 shotVelOrth = dirToTarget * shotSpeedOrth;

// Finally, add the tangential and orthogonal velocities.

return shotVelOrth + shotVelTang;

}

}

Vector3 targetOrigin, Vector3 targetVel) {

Vector3 dirToTarget = Vector3.Normalize(targetOrigin - shotOrigin);

// Decompose the target's velocity into the part parallel to the

// direction to the cannon and the part tangential to it.

// The part towards the cannon is found by projecting the target's

// velocity on dirToTarget using a dot product.

Vector3 targetVelOrth =

Vector3.Dot(targetVel, dirToTarget) * dirToTarget;

// The tangential part is then found by subtracting the

// result from the target velocity.

Vector3 targetVelTang = targetVel - targetVelOrth;

/*

* targetVelOrth

* |

* |

*

* ^...7 <-targetVel

* | /.

* | / .

* |/ .

* t---> <-targetVelTang

*

*

* s---> <-shotVelTang

*

*/

// The tangential component of the velocities should be the same

// (or there is no chance to hit)

// THIS IS THE MAIN INSIGHT!

Vector3 shotVelTang = targetVelTang;

// Now all we have to find is the orthogonal velocity of the shot

float shotVelSpeed = shotVelTang.magnitude;

if (shotVelSpeed > shotSpeed) {

// Shot is too slow to intercept target, it will never catch up.

// Do our best by aiming in the direction of the targets velocity.

return targetVel.normalized * shotSpeed;

} else {

// We know the shot speed, and the tangential velocity.

// Using pythagoras we can find the orthogonal velocity.

float shotSpeedOrth =

Mathf.Sqrt(shotSpeed * shotSpeed - shotVelSpeed * shotVelSpeed);

Vector3 shotVelOrth = dirToTarget * shotSpeedOrth;

// Finally, add the tangential and orthogonal velocities.

return shotVelOrth + shotVelTang;

}

}

**Update:**

If you want to find the point where they meet, you can calculate the time it will take, and then multiply the shot velocity by that. In practice they will collide sooner since they have a certain radius, but we can take that into account when we calculate the time.

// Find the time of collision (distance / relative velocity)

float timeToCollision = ((shotOrigin - targetOrigin).magnitude - shotRadius - targetRadius)

/ (shotVelOrth.magnitude-targetVelOrth.magnitude);

// Calculate where the shot will be at the time of collision

Vector3 shotVel = shotVelOrth + shotVelTang;

Vector3 shotCollisionPoint = shotOrigin + shotVel * timeToCollision;

float timeToCollision = ((shotOrigin - targetOrigin).magnitude - shotRadius - targetRadius)

/ (shotVelOrth.magnitude-targetVelOrth.magnitude);

// Calculate where the shot will be at the time of collision

Vector3 shotVel = shotVelOrth + shotVelTang;

Vector3 shotCollisionPoint = shotOrigin + shotVel * timeToCollision;

Pingback: Studio 2: Back at it again | Caleb's Blog()