Jump to content
• Advertisement

# formula to lead a target?

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

If you intended to correct an error in the post then please contact us.

## 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 this post

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

#### Share this post

##### 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 this post

##### 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 this post

##### 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 this post

##### 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 this post

##### 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); }

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
frob
12
4. 4
5. 5
• Advertisement

• 13
• 14
• 65
• 14
• 15
• ### Forum Statistics

• Total Topics
632130
• Total Posts
3004286

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!