Problem with homing missiles

I'm trying to make homing missiles for my game but there is a little problem. I don't want them to instantly turn towards their target by just getting the angle and giving them that. I want them to turn towards their target until they are pointed at it while moving. The problem is because it can't figure out what to do about 0 and 360. Like if the angle it should be at is 340 and its angle is 10 it'll go the long way to turn instead of subtracting to get to 0 then going to 340. My angles are in radians or whatever. So to the left is pi and -pi up is 1.something and down is -1.something and right is 0.
This is a little hard to understand what your trying to do.. but it sounds like you need to add some decision making.. You need to give more details on how your figuring out the angle/heading

0 to 359

Well after doing a little research this might be a little over my head but maybe someone else can help you out with this.. here is a link you might find useful

http://www.codeproject.com/Articles/190833/Circular-Values-Math-and-Statistics-with-Cplusplus
Ya'll are overthinking it.

The missiles shouldn't care except that they want to turn toward their target before moving. The trick is to limit the angle by which they may turn.

The limit depends entirely on the 'speed' which the missiles are moving. Try some values, like 20 or 30 degrees and see how quickly they turn (and how wide an arc they make).

As for the 0,360 problem -- you need to clamp your angles into a range such that they can never have an overlap, say [0,360). You can do this with the std::fmod() function (in <cmath>).

angle = fmod( angle, 2*pi );

Use it whenever you add anything to your angle:

angle = fmod( angle + deg2rad( 20 ), 2*pi );

Hope this helps.
Like if the angle it should be at is 340 and its angle is 10 it'll go the long way to turn instead of subtracting to get to 0 then going to 340.


The simplest solution for this is to use the [perpendicular] dot product.

The perpdot product is linear algebra magic which can be used here to determine which "side" of the missile your target is on. All you need are 3 vectors:

1) The position of the missile
2) The position of the target
3) The velocity of the missile (ie: X and Y speed)

You can use these 3 points to form a triangle, with the missile position being the key angle. The tricky bit here is that the sine of that angle will be positive if the target is on the right, or negative if the target is on the left. And the sine of the angle is exactly what the perpdot product gives you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Assume you have a 'Vector' or 'Point' or somesuch class, which has x and y properties:


// Linear Algebra magic!
//   Once you understand how to use these functions, you will use them ALL THE
//   FREAKING TIME

// returns cos(theta)*length(a)*length(b), where theta = the angle between vectors a,b
inline double dot(const Vector& a, const Vector& b)
{
   return (a.x*b.x) + (a.y*b.y);
}

// returns sin(theta)*length(a)*length(b), where theta = the angle between vectors a,b
inline double perpDot(const Vector& a, const Vector& b)
{
   return (a.y*b.x) - (a.x*b.y);
}



Again, noting that the sign of sin(theta) is all we really care about, checking to see which way we need to rotate is easy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// our 3 points of interest:
Vector missile_pos = ...;
Vector target_pos = ...;
Vector missile_velocity = ...;

double d = perpDot( target_pos - missile_pos, missile_velocity );
if( d < 0 )
{
    // rotate your missile clockwise
}
else if(d > 0)
{
    // rotate your missile counter-clockwise
}
else
{
    // missile is headed either directly towards or directly away from the target.  Find out which:
    d = dot( target_pos - missile_pos, missile_velocity );
    if( d < 0 )
    {
        // headed directly away.  Rotate either CW or CCW -- doesn't matter
    }
    else
        ; // headed directly towards -- no need to rotate.
}



NOTE: if you try this and your missile starts flying away from the target instead of towards, I might have gotten the sign backwards. Just flip the CW/CCW rotation around.
Last edited on
Nice, Thanks that makes things simple!
What do I have to include to have vectors? I use an enemy direction and speed and move it that way. All I can find anywhere is the other kind of vector...
Bingocat4 wrote:
What do I have to include to have vectors?
Disch wrote:
// Assume you have a 'Vector' or 'Point' or somesuch class, which has x and y properties:
You are expected to write it yourself. And it is not strictly nessesary anyway. It is just here to show general approach.
Ohhh ok. I was confused because I see people sometimes talking about Vectors, and never found anything on google about including it or anything.
C++ is weird because it calls its resizable array class a "vector", so if you saw people talking about vectors on this forum, they were probably talking about that.

However, that is different from a mathematical vector, which is very similar to a point in space. In this thread, I am referring to a mathematical vector.



EDIT: FWIW, I just found this pic on my HD from when I explained this to someone else a long time ago. Maybe it's useful?

http://imgur.com/yYcaZut
Last edited on
Ok! I got it working after a little while. Thanks for the help.
Last edited on
Topic archived. No new replies allowed.