# Missile turning toward target

How would you go about calculating when to begin descending on a target when you know the angle of descent, but to reach the angle of descent the direction vector arcs like it would in real life. I'm very bad at math and terminology but I hope you get what I mean.

You know the targets position in 3 space. And the missiles position, velocity, etc.

The arcing algorithm (that changes the missiles direction over time to a desired direction) is done like this:

If the code isn't clear enough I can explain in english, just let me know. This changeDirection() method gets called each tick for the missile until it is facing the correct direction.

Code:
void Missile::changeDirection(const Point3F& newDirection, const S32 axis)
{
F32 diff = 0.0f;
mTargetDir = newDirection;
mAxis = axis;
isChangingDir = true;
F32 speed = mCurrVelocity.len();
mCurrVelocity.normalize();

F32 unitsOfTurn =  mDataBlock->precision * 0.01;  // arbitrary number

mTargetDir.z - mCurrVelocity.z

if ( axis & Z )
{
if ( (diff = newDirection.z - mCurrVelocity.z) > 0 )
{
mCurrVelocity.z += unitsOfTurn;
if ( mCurrVelocity.z > newDirection.z )
{
mCurrVelocity.z = newDirection.z;
}
}
else if ( diff < 0 )
{
mCurrVelocity.z -= unitsOfTurn;
if ( mCurrVelocity.z < newDirection.z )
{
mCurrVelocity.z = newDirection.z;
}
}
}

if ( axis & X )
{
if ( (diff = newDirection.x - mCurrVelocity.x) > 0 )
{
mCurrVelocity.x += unitsOfTurn;
if ( mCurrVelocity.x > newDirection.x )
{
mCurrVelocity.x = newDirection.x;
}
}
else if ( diff < 0 )
{
mCurrVelocity.x -= unitsOfTurn;
if ( mCurrVelocity.x < newDirection.x )
{
mCurrVelocity.x = newDirection.x;
}
}
}

if ( axis & Y )
{
if ( (diff = newDirection.y - mCurrVelocity.y) > 0 )
{
mCurrVelocity.y += unitsOfTurn;
if ( mCurrVelocity.y > newDirection.y )
{
mCurrVelocity.y = newDirection.y;
}
}
else if ( diff < 0 )
{
mCurrVelocity.y -= unitsOfTurn;
if ( mCurrVelocity.y < newDirection.y )
{
mCurrVelocity.y = newDirection.y;
}
}
}

if ( mCurrVelocity == newDirection )
{
isChangingDir = false;
}
mCurrVelocity *= speed;
Any links or tips would be appreciated, thanks.

Last edited:

Okay let me rephrase the above question.

I have a missile traveling horizontal at an altitude of 200m. The target rests directly ahead along the x,y axis, but at ground level, so at altitude of 0m. If I curve into the direction vector that sends the missile toward the target, how do I calculate when to begin the descent?

I am calculating my curve like this:

targetDirection = { tx, ty, tz } <- this is the target vector
currentDirection = { x, y, z } <- this is the current direction vector the missile is travelling on.

The objective is to make x = tx, y = tx, z = tz. However, NOT instantly. So I add or subtract a finite quantity called C each tick to each component until it is equal to the target component.

To reiterate, I do this
tx - x
If the result is negative I subtract C from x because x is larger than tx, and we want x to equal tx.

I do this for each component until currentDirection == targetDirection.

My objective is to have missiles that fly and turn in a realistic fashion so if you know of a better way than please share. Thanks!

OK. You have a couple of issues to deal with here...

1) A missile almost never fires directly at it's objective. For some reason passing understanding the people that design missiles want the launcher to stay intact so, the missile launches in the direction the laucher points, preferably close to the target as in a fighter in the six o clock behind a bogey, but not so optimal in the case of a ground launch, or non-optimal fighter launch. Result: The missile fires for X seconds to clear the launch facility so it's exhaust doesn't cook the launcher before turning to target.

2) Once clear the launcher the missile homes on it's target and corrects it's trajectory. How? Assuming the missile is not psychic, and that is FAF (Fire and Forget) as opposed to laser, radio, or other tagged targeting systems, it must adjust its position and trajectory to meet the target point. The math for that is simple.

3) Arcing is a visual phenomena. What if the missile is in space? Where are you viewing it from? Is the missile ballistic? (ie: will it meet the target before it's fuel exhausts [non-ballistic] or after it's fuel exhausts [ballistic]) Is the target stationary? (Yes, the Earth turns, so it's not exactly stationary)

Hello,
1) yes my missile has a track delay before it aligns itself to a target.
2) the missile does have a built-in guidance system, but it is too cpu expensive and I'd prefer to not use it if the target is stationary (which is this case)
3) The arcing takes place as a result of a direction vector change, so it is more than a visual phenoma I would contend. And the earth isn't spinning in my simulation so yes the target is stationary.

So I guess it is #3 that I am most worried about right now. I just need a way to calculate "the arc" i guess to determine when to begin turning toward the target. If say, I want to descend on my target at a 45 degree angle, how would I know when to begin arcing into that 45 degree direction vector so as to hit the target. If i wait till I am 45 degrees before i begin the arc it will miss because it turns.

Have I got it right:
A missile flyies in straight line, above a target in straight line. The missile wants to know the time to start turning. The turn will be with a constant direction changing?(a circle). If the missile keeps turning the same amount in the same time interval, the trajectory is circle. Let's say Theta is the angle swept by the radius in a time t. The rotation speed let be w. Theta=w*t. The arc length is R*Theta=R*w*t, where R is the radius of the circle. the arc length is(considering the missile's speed v): v*t.
So: R*w*t=v*t. Now you have the R of the circle. You also have the coordinates for the 2 lines(missile and target trajectory). The radius of the circle is always perpendicular on the speed, so this is how u know the circle center: draw a perpendicular line down from the missile trajectory line. The center is in the point where the length of the perpendicular line is equal to the radius(R which you just computed before.) squareR=square(x-x0)-square(y-y0) is the circle parametric equation. x0 and y0 are the center of the circle coordinates, you computed them when you draw down that line. u know R.
Now we can make ends meet: U have the target coordinates, (xt,yt), and missile coordinates(xm=v*t,ym). The coordinates of your circle are: x0=v*t,y0=ym-R.
In equation  replace x0 and y0. now fit it once with x and y of the target. The equation I got:
v*t=xt-squareroot(square(v/w)+square(yt-ym +v/w)) , were w is the turning angle of the missile (radians). v*t is the x coordinate of the missile when it should start turning. If u want the time, divide the equation by v.
Tell me if I didnt understand the problem.