Jump to content
  • Advertisement
Sign in to follow this  

Projectile leading a target

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

I'm trying to lead a target with turret that shoots out a projectile in 3d space on a coordinate system(without acceleration, so no gravity) the problem: GIVEN: -Ppos (projectile's initial position = turret's initial position)[vector] -Pvco (projectile's velocity constant, speed of projectile)[float] -Tpos (target's current position)[vector] -Tvel (target's current velocity)[vector] FIND: -t (time until projectile and target intersect)[float] -Pdir (Projectile's direction)[vector] This is what I have so far (a script written in python): http://www.pasteall.org/11337/python The script works very good... sometimes. The target has a positive y axis velocity, and starts in Quadrant IV. Projectiles are fired every 5 seconds or so, the script runs on their creation. The projectiles fail to intersect with the target when the target starts in Quadrant IV and is moving to Quadrant I. New projectiles will then hit the target once the target crosses the y axis. I cannot figure out what is wrong with it. I think it has something to do with positions. The same happens when the target has a positive x axis velocity and starts in Quadrant III. As soon as the target crosses the y axis, the new projectiles will hit the target. Same for a positive y axis velocity in QIII to QII, and positive x axis velocity in QII to QI. I also get this problem when the velocities are negative and they start in the reverse quadrants. Here is a picture to help you visualize: http://www.pasteall.org/pic/show.php?id=1656 In the picture: -QI is top right, QII is top left, QIII is bottom left, QIV is bottom right. -The target moved from QIV to QI. (a positive y axis velocity) -The projectile is going to intersect the target, because the target was allready in QI after it was fired. -Projectiles that were fired when the target was still in QIV missed. If you need a better explanation of the problem, or you need help interpreting the python code, please ask. I found this thread and I wasn't able to gather much from of it: http://www.gamedev.net/community/forums/topic.asp?topic_id=384206 Thanks, -Mad Hatteur

Share this post

Link to post
Share on other sites
You know that the projectile's path must lie in the plane defined by the line of the target's motion and the point of the projectile's origin.

To approach this, I would transform it into a two-dimensional problem. You have a triangle defined by the target's position, the projectile's position, and the point of intersection of their paths. You know the distance between the target and projectile and the angle of the target's path relative to the projectile's current position. You also know the ratio between the projectile's velocity and the target's velocity.

This lends itself very well to using the law of cosines. If a is the distance between the target and projectile, b is the distance between the target and point of intersection, c is the distance between the projectile and point of intersection, and theta is the angle between the vector between projectile and target and the target velocity vector, then:


b is the only unknown, so you can solve this with a simple quadratic equation. You want the smallest value of b that is still positive; a solution will exist as long as the projectile is faster than the target.

cos(theta) is simply the dot product of the normalized velocity vector and the normalized difference between the two positions.

Once you know b, the rest of the problem is trivial:


I just sketched this up quick on pencil and paper; I didn't actually test it.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • 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!