## Missile targeting

Member
Posts: 196
Joined: 2002.04
Post: #1
Is there a place that has some coding tutorials (hopefully OpenGL) for missile targeting for the x and y coords. Also I've tried looking on google and gamasutra but found nothing.

Thanks,
Iceman
Member
Posts: 79
Joined: 2002.08
Post: #2
I was pulling my hair about this same thing a couple of days ago and finally came up with something that works. I'm sure there is a more elegant way to do it though.

Basically I get the horizontal and vertical angles to the target and from that I calculate yaw and pitch inputs that I feed to the physics engine. It's a bit complicated but I get the vector to the target and my heading vector, construct a couple of shortest arc quaternions and get the angles from them.

KenD
Member
Posts: 196
Joined: 2002.04
Post: #3
Hmm that sounds kind of complex :eek:. What about AI pathfinding would this be easy to work into missile targeting? I don't really know what it is, but it sounds very interesting.

Thanks,
Iceman
Member
Posts: 79
Joined: 2002.08
Post: #4
I haven't dug into the path finding subject really but that's more about how to get computer controlled characters to find a way from point A to point B, avoiding obstacles and such. This may sound like what a missile is doing too, going from point A to point B but it's not really the same thing. In the case of a missile you know where the target is and it usually goes straight to it. It doesn't try to avoid obstacles.

The problem with a missile is how to make it steer towards the target in a 3D environment and I solved it as I described above. I find out how much off course I am (the angles) and then try to minimize those angles.

I haven't found a ready made solution to the problem so I had to combine several things like getting the direction to the target, my direction, getting the angles for how much I'm off and then steer in the opposite direction to minimize the difference.

KenD
Member
Posts: 196
Joined: 2002.04
Post: #5
Quote:Originally posted by KenD

The problem with a missile is how to make it steer towards the target in a 3D environment and I solved it as I described above. I find out how much off course I am (the angles) and then try to minimize those angles.
KenD

Ok I know how to make the vectors work with an angle, but what is the formula to find the minimum angle?

Thanks,
Iceman
w_reade
Unregistered

Post: #6
I think this should work:

if you have two normalised vectors: the first pointing in the direction of the target and the second pointing in the direction of the missileÖ

Öthen their vector cross product is an axis vector for the rotation that will take one onto the other, and their vector dot product is the cosine of the angle between them.

If they aren't normalised you'll get a non-unit axis vector and plain wrong inverse cosines, so normalise them.

You should be able to optimise slightly by only bothering to steer if the dot product is > 0.95 or so - this should save needless course-correction calculations when the missile is on target.

edit - gibberish
Member
Posts: 196
Joined: 2002.04
Post: #7
How do I find the angle? I know what the opposite and adjacent sides are (5,5). I've tried looking in my Algebra2 book, but none of the functions I thought would work did.

Thanks,
Iceman
Member
Posts: 196
Joined: 2002.04
Post: #8
Ok I got it finally! Been searching google all day and I finally figured out that I was forgeting to translate the radians into degrees. Here's how I got it: atan (5/5) * 180/M_PI .

Thanks,
Iceman
Member
Posts: 204
Joined: 2002.09
Post: #9
A simpler, probably faster (yet less precise) way to do this is to create a vector which represents the velocity of the missile. Then get the distance (use the fast formula by not calling sqrt, the squared distance is fine for comparison) for the missile to the target if the missile continues on the same path. Next, get the distance from the target if the missile turns to clockwise this turn, and also if the missile turns counter-clockwise this turn. The distance which is the shortest is the one which the missile should do (ie if turning left shortens the distance to the target, then do that). One side effect of this is that if the turn value is too large, the missile may orbit around the target. :-)

Another method which is better than the one proposed, yet more complicated, is to use quaternions and do linear interpolation between them. This, I believe, produces the best results.

In either case, if your game calls for having lots of missiles, you'll not be very optimized if you call any of the trig math functions (like atan). However, if you only have a couple it probably won't matter.

Cheers
Member
Posts: 196
Joined: 2002.04
Post: #10
I think I'm going to stick with what works for now. What you suggested in the first paragraph sounds almost exactly like what I'm doing now. Also I'll probably only use 2 or 3 at a time since the missiles seek out the player immediately on launch, and they go pretty fast too.

Thanks,
Iceman

quaternions = yuck :eek: