# formula to lead a target?

This topic is 3861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Was going over this with a friend, and this is a bit harder than it seems. What is the simplest formula for leading a target given: P1: shooter's position V1: shooter's weapon velocity P2: target's position V2: target's velocity So, we want to figure out what position to shoot at so the projectile will arrive when the target does (assuming it doesn't change velocity).

##### Share on other sites
This thread has the answers you seek.

##### Share on other sites
Hmm finally got around to implementing this. Looks like there is a mistake in the solution presented there. These two functions calculate a point to lead the target:

Point intercept(Point const &shooter, double bullet_speed, Point const &target, Vector const &target_velocity){  double a = bullet_speed*bullet_speed - target_velocity.dot(target_velocity);  double b = -2*target_velocity.dot(target-shooter);  double c = -(target-shooter).dot(target-shooter);  return target+largest_root_of_quadratic_equation(a,b,c)*target_velocity;}double largest_root_of_quadratic_equation(double A, double B, double C){  return (B+std::sqrt(B*B-4*A*C))/(2*A);}

However this line looks incorrect since it dot products the target velocity with itself?

  double a = bullet_speed*bullet_speed - target_velocity.dot(target_velocity);

Tried substituting target position in both side and it doesn't work either way. Any ideas? Or anyone know of similar code that works?

##### Share on other sites
Quote:
 Original post by FippyDarkpawHowever this line looks incorrect since it dot products the target velocity with itself?  double a = bullet_speed*bullet_speed - target_velocity.dot(target_velocity);

I don't know what about that line looks incorrect to you. The dot product of a vector with itself is the square of its length, and it's a perfectly normal thing to compute.

If you have an example where the code doesn't do what it's supposed to, post it and I'll fix the code or I'll walk you through an explanation based on the example.

##### Share on other sites
It works if the target is not moving. But if the target moves it shoots way off.

Here's the C#/XNA code translation of your code. Given: shooter position, bullet speed, target position, and target velocity, where should I shoot to intercept? Let me know if you spot any mistakes. =)

public static Vector3 CalculateTargetPoint(Vector3 shooter,                                           float bulletSpeed,                                           Vector3 target,                                           Vector3 targetVelocity){          float A = bulletSpeed * bulletSpeed -              Vector3.Dot(targetVelocity, targetVelocity);   float B = -2 * Vector3.Dot(targetVelocity, (target - shooter));      float C = Vector3.Dot(-(target - shooter),                           (target - shooter));           float result = B + (float)Math.Sqrt((B*B-4*A*C)/(2*A));   return target + (result * targetVelocity);        }

##### Share on other sites
Quote:
  float result = B + (float)Math.Sqrt((B*B-4*A*C)/(2*A));

That seems completely wrong. Try this:
   float result = (B + (float)Math.Sqrt(B*B-4*A*C)) / (2*A);

##### Share on other sites
Hmm yeah that seems to have fixed it. Thanks alot. =)

BTW, I also found this 2D implementation. Works for my game which is 3D, but is top-down camera so essentially on a 2D plane.

public static Vector3 CalculateLeadTargetPoint(Vector3 shooter,                                               float bulletSpeed,                                               Vector3 target,                                               Vector3 targetVelocity){   float p1x = shooter.X;   float p1y = shooter.Z;   float p2x = target.X;   float p2y = target.Z;   float v2x = targetVelocity.X;   float v2y = targetVelocity.Z;   float a = (v2x * v2x + v2y * v2y - bulletSpeed * bulletSpeed);   float b = (2*p2x*v2x - 2*p1x*v2x+ 2*p2y*v2y  - 2*p1y*v2y);   float c = (p2x*p2x - 2*p2x*p1x + p1x*p1x + p2y*p2y - 2*p2y*p1y + p1y*p1y);   float result = (-b - (float)Math.Sqrt(b * b - 4 * a * c)) / (2 * a);   return target + (result * targetVelocity); }

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 10
• 23
• 13
• 10
• 9
• ### Forum Statistics

• Total Topics
634477
• Total Posts
3017702
×