How to lead a target?
Hi all.
I''ve been looking all over the net for what I would have thought would have been something common. I''m looking for a code snippet (using 3D/2D Vectors) or explanation on how to lead a target.
When you shoot a gun or throw a ball at a moving target, you need to lead that target so the bullet/ball hits the target in the future. I know the speed and vector of my target, I know my position relative to the target and my distance from it. I also know how fast my projectile can travel. What I don''t know is how far ahead I need to lead my target so I hit it.
I''m using 2D vectors (adds/scales/subs) to move around my world.
Any suggestions to websites that have this formula wold be appreciated. With all the shooting games out there I would have thought this would have been all over the net by now. Thanks in advance.
ok my physics is rusty but try this (answer at absolute bottom the body of this response is the derivation of the equation):
ok so there's object O1 and object O2
each have respective equations of position:
O1: s1 = a1t^2 + v1t + 0
O2: s2 = a2t^2 + v2t + 0
if we are assuming that there is no acceleration in there particles we then get:
O1: s1 = v1t
O2: s2 = v2t
we'll say that time zero is when object O2 is directly in the position normal to the path of O1 (i.e when O1 passes the vertical line in the above diagram.
at that time, t in both equations is equal.
because there is independance of X and Y velocities, we can say that the horizonal (X axis) component of O2's velocity is as such:
vx2 = v2sin(theta) (where theta is the angle we are trying to solve for. i.e the angle at which we must shoot to hit the target.0
this x component of O2's position has the following equation:
sx2 = vx2 * t
now sub in the equation for vx2:
sx2 = v2 * sin (theta) * t
we also know that if these particles are to collide at some time t, then the X component of O2's position equation must be equal to the the position equation of O1:
v2 * sin(theta) * t = v1 * t
the t's cancel out:
v2 * sin(theta) = v1
so:
theta = sin-1(v1/v2)
which is your answer assuming that you fire O2 exactly when the origin of O2 is perpendicular to the mostion of O1.
to genericize it for all origins of firing:
so now you can use the same equation to calculate theta, but theta gives you the angle z2 in the above diagram (which is the angle from the normal to the movement vector of O1). so you can calculate z1 (the angle at which you are looking at O1 relative to its movement vector) from position info and then modify your firing angle like:
firing angle = theta + (180 - (90 + z1))
where theta still equals sin-1(v1/v2)
so your final equation:
firing angle = sin-1(v1/v2) + (180 - (90 + z1));
oops forgot this:
the firing angle is the amount that you must turn towards the direction of motion of O1 if you were directly facing O1 at the moment of calculation. if turning takes some time, you're going to have to factor that in.
-me
[edited by - Palidine on April 1, 2002 2:09:22 PM]
O1 |-----------------> | ^ | / | / | / | / |/ / O2
ok so there's object O1 and object O2
each have respective equations of position:
O1: s1 = a1t^2 + v1t + 0
O2: s2 = a2t^2 + v2t + 0
if we are assuming that there is no acceleration in there particles we then get:
O1: s1 = v1t
O2: s2 = v2t
we'll say that time zero is when object O2 is directly in the position normal to the path of O1 (i.e when O1 passes the vertical line in the above diagram.
at that time, t in both equations is equal.
because there is independance of X and Y velocities, we can say that the horizonal (X axis) component of O2's velocity is as such:
vx2 = v2sin(theta) (where theta is the angle we are trying to solve for. i.e the angle at which we must shoot to hit the target.0
this x component of O2's position has the following equation:
sx2 = vx2 * t
now sub in the equation for vx2:
sx2 = v2 * sin (theta) * t
we also know that if these particles are to collide at some time t, then the X component of O2's position equation must be equal to the the position equation of O1:
v2 * sin(theta) * t = v1 * t
the t's cancel out:
v2 * sin(theta) = v1
so:
theta = sin-1(v1/v2)
which is your answer assuming that you fire O2 exactly when the origin of O2 is perpendicular to the mostion of O1.
to genericize it for all origins of firing:
t0 |--------------------> \ z1 / \ / \ / \ z2/
so now you can use the same equation to calculate theta, but theta gives you the angle z2 in the above diagram (which is the angle from the normal to the movement vector of O1). so you can calculate z1 (the angle at which you are looking at O1 relative to its movement vector) from position info and then modify your firing angle like:
firing angle = theta + (180 - (90 + z1))
where theta still equals sin-1(v1/v2)
so your final equation:
firing angle = sin-1(v1/v2) + (180 - (90 + z1));
oops forgot this:
the firing angle is the amount that you must turn towards the direction of motion of O1 if you were directly facing O1 at the moment of calculation. if turning takes some time, you're going to have to factor that in.
-me
[edited by - Palidine on April 1, 2002 2:09:22 PM]
Thanks, that is a good start..
"which is your answer assuming that you fire O2 exactly when the origin of O2 is perpendicular to the mostion of O1."
That is actually something I didn''t mention, O1 can be moving at any angle, event directly toward O2 (making interception easy I would think ) . The other issue I failed to mention is that both O1 and O2 can have, but not required to have, velocities.
"which is your answer assuming that you fire O2 exactly when the origin of O2 is perpendicular to the mostion of O1."
That is actually something I didn''t mention, O1 can be moving at any angle, event directly toward O2 (making interception easy I would think ) . The other issue I failed to mention is that both O1 and O2 can have, but not required to have, velocities.
read the "oops i forgot" part which generalizes to all angles.
firing angle = sin-1(v1/v2) + (180 - (90 + z1));
but actually i just realized that won't work if the lead angle should be less than the perpendicular....well actually it might work, i have to look at it more.
gah
i'll figure that out soon. i have to eat lunch.
anyway, as for zero velocity conditions, btw O1 is the target O2 is your bullet. i don't think that you need to account for O2 (the shooter) moving as long as the velocity of their bullets is much greater than the speed of the person shooting:
1) O1 is stationary, just point at it and shoot
2) O2 is stationary. as per above, you don't really need to consider the velocity of the person doing the shooting unless they are shooting really really slow bullets. the only way it WILL effect the equations is if you actually increase or decrease the velocity of the projectile being shot based on the movement of the shooter. in real life if you are moving at 5m/s and shoot a gun directly in the direction you are running, the bullet will travel at normal bullet speed plus the velocity at which you are moving. assuming that you are not taking this into account you definitely don't need to worry about whether the shooter is moving or not
-me
[edited by - Palidine on April 1, 2002 3:43:11 PM]
firing angle = sin-1(v1/v2) + (180 - (90 + z1));
but actually i just realized that won't work if the lead angle should be less than the perpendicular....well actually it might work, i have to look at it more.
gah
i'll figure that out soon. i have to eat lunch.
anyway, as for zero velocity conditions, btw O1 is the target O2 is your bullet. i don't think that you need to account for O2 (the shooter) moving as long as the velocity of their bullets is much greater than the speed of the person shooting:
1) O1 is stationary, just point at it and shoot
2) O2 is stationary. as per above, you don't really need to consider the velocity of the person doing the shooting unless they are shooting really really slow bullets. the only way it WILL effect the equations is if you actually increase or decrease the velocity of the projectile being shot based on the movement of the shooter. in real life if you are moving at 5m/s and shoot a gun directly in the direction you are running, the bullet will travel at normal bullet speed plus the velocity at which you are moving. assuming that you are not taking this into account you definitely don't need to worry about whether the shooter is moving or not
-me
[edited by - Palidine on April 1, 2002 3:43:11 PM]
so to clarify again cause i didn''t make it clear. in everything i''ve been talking about O1 is the target object, O2 is the bullet you are shooting at it.
you don''t need to worry about the movement of O2''s shooter.
all equations are calculated as if you were looking directly at the target at the time of shooting. the firing angle returned by the equation is the amount you would have to turn if you were looking directly at the target. if you are not looking directly at the target, modify the answer by adding or subtracting the angle you are facing away from the target.
-me
you don''t need to worry about the movement of O2''s shooter.
all equations are calculated as if you were looking directly at the target at the time of shooting. the firing angle returned by the equation is the amount you would have to turn if you were looking directly at the target. if you are not looking directly at the target, modify the answer by adding or subtracting the angle you are facing away from the target.
-me
Thanks.. you've been very helpful
Let me write this out in long hand to verify I understand (not having any formal background in math (outside of highschool anyway) I tend to need to fully qualify things)
FA= FiringAngle
v1 = Velocity of O1
v2 = Velocity of O2
z1 = Theta of O1 relative to O2
FA = Sin((-1*(v1/v2)) + (180 - (90+z1)))
Again, please forgive my rehashing, just trying to put it in the simplest terms.
Thanks again.
[edited by - DiverDave on April 1, 2002 4:04:15 PM]
Let me write this out in long hand to verify I understand (not having any formal background in math (outside of highschool anyway) I tend to need to fully qualify things)
FA= FiringAngle
v1 = Velocity of O1
v2 = Velocity of O2
z1 = Theta of O1 relative to O2
FA = Sin((-1*(v1/v2)) + (180 - (90+z1)))
Again, please forgive my rehashing, just trying to put it in the simplest terms.
Thanks again.
[edited by - DiverDave on April 1, 2002 4:04:15 PM]
yeah looks good. couple problems with my solution though. the biggest problem is that is assumes that O1 is traveling exactly along the X axis of whatever coordinate system you are using. i imagine that you could do a matrix transform to make that the case. and it looks like that will keep this equation very simple.
i''m working out a generic 2D case but it gets retardedly complicated in the derivation. you have to solve a quadratic in the end which isn''t a big deal b/c you can throw out one of the numbers all together b/c it''ll be an imaginary root. but the solving of the equations to get down to something you can plug into a computer is taking a while. in the end the generic equation will be better b/c you won''t have to do a matrix transform on everything in the world. it''s just hassling me to derive it.
i forgot how much multi-dimensional physics hurt my brain
but on the other hand, thanks for getting me back into being productive
-me
i''m working out a generic 2D case but it gets retardedly complicated in the derivation. you have to solve a quadratic in the end which isn''t a big deal b/c you can throw out one of the numbers all together b/c it''ll be an imaginary root. but the solving of the equations to get down to something you can plug into a computer is taking a while. in the end the generic equation will be better b/c you won''t have to do a matrix transform on everything in the world. it''s just hassling me to derive it.
i forgot how much multi-dimensional physics hurt my brain
but on the other hand, thanks for getting me back into being productive
-me
I had a hard time finding a solution for this too! I filled pages and pages with math, only to end up with tons of sines and cosines. I looked up bot AI, and all their solutions were hacks.
I finally did it recursively:
First aim directly at the target, disregarding it''s movement..
Then, find how long it would take to get to that point
use that time as an approximation for t, and aim at where the target would be at t
See how long it would take to get there..
etc..
It worked fine, but only if the speed of my gun was faster than the speed of the projectile.
I would post the code, but I lost it. The good part is that it is a solution that works in 3D, and it converges quickly( as long as the bullet is fast)
However, I wish I could work out the vector geometry to solve it in 3 dimensions..
I guess that your 2d solution is actually a 3d solution, you just have to rotate so that the gun, the starting point of the target and the direction of the target all lie in a plane..
I finally did it recursively:
First aim directly at the target, disregarding it''s movement..
Then, find how long it would take to get to that point
use that time as an approximation for t, and aim at where the target would be at t
See how long it would take to get there..
etc..
It worked fine, but only if the speed of my gun was faster than the speed of the projectile.
I would post the code, but I lost it. The good part is that it is a solution that works in 3D, and it converges quickly( as long as the bullet is fast)
However, I wish I could work out the vector geometry to solve it in 3 dimensions..
I guess that your 2d solution is actually a 3d solution, you just have to rotate so that the gun, the starting point of the target and the direction of the target all lie in a plane..
alright i think i have it worked out. i''ll put up the detailed derevation on my website. it''s gonna take at least 30min to type it all up.
as a warning it''s been at least a year since i''ve derived anything in physics so don''t kill me if it doesn''t work
i''ll post the link here when it''s done.
-me
as a warning it''s been at least a year since i''ve derived anything in physics so don''t kill me if it doesn''t work
i''ll post the link here when it''s done.
-me
here:
http://www.circlelogic.com/gamestuff/Targeting.html
again, i''m not 100% confident in this, but it looks pretty good to me.
also be gentle on that server, it''s just a little guy
-me
http://www.circlelogic.com/gamestuff/Targeting.html
again, i''m not 100% confident in this, but it looks pretty good to me.
also be gentle on that server, it''s just a little guy
-me
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement