Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Leading target

This topic is 5967 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

This is for a 2d, overhead space shooter game. Right now, I have turrets that fire at their target, but by the time the bullets get there, the target is already gone. I’m trying to make turrets that shoot ahead of their target (“leading” the target). The bullet’s velocity is constant, so there is no need to consider acceleration. Basically I’m looking for the angle that the turret must be aimed at in order to hit the target. I found this topic mentioned a few times in other threads but none of them really came to any conclusion on how to do this (or I just couldn’t get the methods they mentioned to work). So far, these are the equations that I have figured out… I’m not sure if they’re correct, or what the best way is to solve for them: targetXPos + targetXVel * time = turretXPos + bulletVel * cos(theta) * time targetYPos + targetYVel * time = turretYPos + bulletVel * sin(theta) * time the unknowns are time and theta…. Two unknowns and two equations, seems easy right? But I can’t get it to work, so I’m wondering if these equations are correct, or what the best was to solve for theta is. Thanks to any one who can help out here.

Share this post

Link to post
Share on other sites
Basically, you should just break the whole thing down into triangle:

. (space holder)
0 (the target)
- (target's path)
T (your turret)
/ (turret's path)
X (meeting point)


From 0 to X, you have (targetVelocity * time)
From T to X, you have (gunVelocity * time)
To find your angle, inverse sin((0 to X) / (T to X)). This gets the angle between (T and 0) and (T and X), just add or subtract to make it into the coordinate system you're using.

As for time being an unknown, that's not exactly true. The time from (0 to X) and the time from (T to X) equals each other. You just gotta calcuate it in your targetting algorithm (ie. that's another equation).

Hope this helps.

[edited by - LyLFox on March 17, 2002 7:10:08 PM]

Share this post

Link to post
Share on other sites
Ok, you''re essentially trying to solve simultaneous equations I think. You''re looking for 2 lines that intersect - the trajectory of the target and the trajectory of the bullet - and any example where the lines cross is potentially a way of shooting the target. So given any pair of lines, you have to take the length of each line multiplied by the speed of whatever will be travelling along it, and you want these two values to be equal. This seems easy to achieve with a brute force method. (Start at the beginning point on the ''known'' line, and plot the bullet line to that point, see if the distances X speeds are equal. If so, there''s your angle to shoot at. If not, move a little bit along that line and try again until it is, or until the line intersection is offscreen.) Performing this as some sort of binary search would speed it up a little.

But maybe some better mathematical knowledge would let you solve this instantly though. For instance, the required angle will change logarithmically, varying from the exact angle between the 2 objects at zero velocity, changing quickly and levelling off to the exact angle of travel at infinite velocity. Maybe there''s some sort of curved interpolation you can do here. (Sorry for any incorrect mathematical terms. Or indeed any incorrect mathematical assumptions.)

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post

Link to post
Share on other sites
Yeh! Finally got it to work. Thanks Kylotan. The speed isn''t a problem at all at this point - the frame rate stays at 140 fps with or without the AI turned on. But now the computers targeting is way too good... well that shouldn''t be too hard to fix. Thanks.

Share this post

Link to post
Share on other sites
This problem can be solved with fairly simple trig and vector mathematics. However, it would be more appropriate that this question be asked in the Maths/Physics forum. Before doing so though, I highly recommend you check out the archives for that forum, as this question has been answered inumerable times (it comes up about once per month).



Share this post

Link to post
Share on other sites

  • Advertisement

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!