0
\$\begingroup\$

I have a game where I know the location and velocity of my target. I know my own location and the speed of my projectile. I want to determine either

the location of the intersection between my projectile fired and the target, or

the time taken for my projectile to intersect the target

All entities are travelling with constant speed and direction which makes life easier. I have been trying to work out the maths for some time but cannot quite get it right. I know that the predicted location of the target intersecting the projectile will be

Pp = Tp + Tv * t

Where Pp is the predicted position, Tp is the current target position, Tv is the target velocity, and t is time.

I also know that

t = |Pp - Sp| / Bs

where Sp is my (source) position and Fs is the projectile (bullet) speed. But I cannot figure out a way to solve this to get either Pp or t.

I know that all speeds are constant. I am free to aim in any direction and I know that the projectile is faster than the target therefore a hit is guaranteed

Can anyone enlighten me? Thanks

I found a website detailing one method but this doesnt seem to work (http://howlingmoonsoftware.com/wordpress/leading-a-target/). My verification method to determine if it worked was to use the predicted position to determine the time for the target to reach it, and for the projectile to reach it. These should be very similar but in my game they are out by almost a factor of 2.

\$\endgroup\$
1
  • \$\begingroup\$ This is indeed a duplicate. thanks. Just to add, the link I supplied was slightly wrong as they were using the delta velocity which is not needed. The link supplied by bummzack is correct. \$\endgroup\$
    – allanmb
    May 1, 2014 at 11:36

1 Answer 1

2
\$\begingroup\$

Similar to what you've got, start with the target's position/velocity:

Ptarget impact = Ptarget now + t × vtarget

And we've got your projectile's values:

Pshot impact = Pplayer + t × vshot

Since both are supposed to collide:

Ptarget impact = Pshot impact

Due to this, you'll get this:

Ptarget now + t × vtarget = Pplayer + t × vshot

To solve this, you'll want to bring the time dependent stuff on one side:

Ptarget now - Pplayer = t × vshot - t × vtarget

t × (vshot - vtarget) = Ptarget now - Pplayer

Now you just have to transform the term one last time:

t = (Ptarget now - Pplayer) / (vshot - vtarget)

t = Δposition / Δvelocity

This enables you to determine the time it takes your projectile to reach the enemy based on his currenct velocity (and orientation) as well as your distance to the target.

Once you've got this, you can once again use the very first formula to determine the point you'll have to aim for:

Ptarget impact = Ptarget now + (Δposition / Δvelocity) × vtarget

\$\endgroup\$
3
  • \$\begingroup\$ I think this answer is incomplete. The positions and velocities will be vectors, not scalars. How do you get a scalar value? Is it the magnitudes? \$\endgroup\$ Jan 21, 2022 at 19:27
  • \$\begingroup\$ @DanielJohnson Hm? These formulas work no matter how many components/dimensions you've got. P is a position, Δ a distance, and v a velocity. Whether these consist of single called, x and y, possibly z, too - it won't change. And no, don't use magnitudes, just calculate with vectors. \$\endgroup\$
    – Mario
    Jan 22, 2022 at 6:18
  • \$\begingroup\$ It's not how many dimensions I'm concerned with. You're dividing vectors by vectors and expecting a scalar value. I don't understand how to make that transition and search results suggest that dividing vectors by vectors is an unusual operation. \$\endgroup\$ Jan 23, 2022 at 15:40

Not the answer you're looking for? Browse other questions tagged .