# Controlling how quickly a missile turns when tracking target

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey,

My missile currently does a good job of tracking the target it is launched at. This is the relevant bit of code,

 public override void nextStep(float dt) { base.nextStep(dt); if (this.target != null) { // change direction to keep track of the target Vector3 dist = Vector3.Zero - this.current_position; // this.target.Position.Translation - this.current_position; float curr_len = current_velocity.Length(); current_velocity += dist * TurnTime * dt; current_velocity.Normalize(); current_velocity *= curr_len; } this.current_position += this.current_velocity * dt; } 

I've temporarily got it "tracking" the world origin, for simplicity, but it works the same when tracking an entity. The issue is, I don't understand the relationship between TurnTime and the actual time taken to turn to face an object. It would make balancing easier if I understood this relationship. What I need is some function of the turn speed, so I can throw in a feature where the missile slows to change course in over a tighter radius, and thus control how many times it orbits an entity before either hitting it or giving up.

##### Share on other sites
Maybe it would work better if you followed roughly the real physical principles. The rocket flies at some speed, in a straight line. To turn, it will probably use some wings or change the jet draft direction. I think that should be approximated by an aditional force (thus acceleration) pushing the rocket to the side, so direction of the acceleration vector is perpendicular to the velocity vector. If this acceleration was constant and the flying speed of the rocket was constant too, the rocket would fly in a perfect circle, with the acceleration (centripetal) pointed to the centre of the circle.

Now, the magnitude of the acceleration can be variable and you can use it to change how quickly can the rocket turn. It represents just the turning force (acceleration is proportional to force), the resulting trajectory (radius of the turning circle) will depends also on the actual flying speed which IMHO is perfectly realistic.

And how to get the direction of the acceleration vector? You could for example get the actual direction (equals the velocity vector) and the target direction (from the current position to the target point which is Vector3.Zero in your case). Now use some vector operations to find out which way do you have to turn in order to reach the target and then orient the acceleration vector that way (but perpendicular to the actual velocity vector, so it will change direction slightly every frame).

You then simply update the actual velocity vector by adding the acceleration vector multiplied by dt.

##### Share on other sites

Hey,

My missile currently does a good job of tracking the target it is launched at. This is the relevant bit of code,

 public override void nextStep(float dt) { base.nextStep(dt); if (this.target != null) { // change direction to keep track of the target Vector3 dist = Vector3.Zero - this.current_position; // this.target.Position.Translation - this.current_position; float curr_len = current_velocity.Length(); current_velocity += dist * TurnTime * dt; current_velocity.Normalize(); current_velocity *= curr_len; } this.current_position += this.current_velocity * dt; } 

I've temporarily got it "tracking" the world origin, for simplicity, but it works the same when tracking an entity. The issue is, I don't understand the relationship between TurnTime and the actual time taken to turn to face an object. It would make balancing easier if I understood this relationship. What I need is some function of the turn speed, so I can throw in a feature where the missile slows to change course in over a tighter radius, and thus control how many times it orbits an entity before either hitting it or giving up.

Taking the code at face value, it does not make a lot of sense and I'm not surprised that the effect of TurnTime is confusing. Consider the units on the line where TurnTime is used. The quantity on the right is being added to velocity, which has the unit of length over time (L/T). But the quantity on the right has units of length times time squared (LT^2). Then it gets munged by normalizing it and multiplying it by a length. Physically there is little meaning behind this and that makes it difficult to reason about physically.

But we can still try to reason about what the author was trying to accomplish since we know this is intended to be pursuit code. The resulting velocity is made up of two parts: (1) the original velocity and (2) a contribution in the direction of the prey. The magnitude of the velocity is being clamped so that it is always the same and it is only the direction that changes. The second part depends upon the displacement of the prey from the pursuer, which is scaled by the TurnTime. The larger the TurnTime, the larger the contribution of this second part.

Imagine that the prey is directly in front of the pursuer -- the displacement between the prey and pursuer is parallel to the pursuers velocity so the resulting (scaled) velocity will be unchanged. Now, imagine that the prey is to the left of the pursuer so that the displacement is orthogonal to the pursuers velocity, the sum of the two components are orthogonal so you can think of it as a triangle. The length of the velocity never changes (because it is clamped) but the magnitude of the other component does change because it is proportional to the distance between the prey and pursuer. So the sum of the component vectors angles more and more to the left as the 'TurnTime' component increases. However, since the magnitude of the velocity is clamped the result will be in the same direction as the sum of the two parts, but the speed will remain constant. So the larger the TurnTime the greater the effective turning acceleration, which does not seem like the intended effect to me. Another way of putting it is that the larger the TurnTime, the smaller the turning circle, again, this seems wrong since I would expect a small turn time to imply a small turning circle.

-Josh

##### Share on other sites
There are more problems with the original code. Not only zero physical meaning, as jjd also pointed out, but it will for example completely fail if the target is directly behind the rocket at some frame. At this particular situation, you first make the velocity vector smaller (without changing its direction), then normalize it and give it back its original lenght. The result is that nothing really happens. This is an extreme situation (which may never happen), but the mere existence of such a problematic configuration should always attract your attention. And with a reason in this case (and most cases in general) - even if the rocket isn't flying absolutely directly away from the target, but just almost directly, the behaviour will be bad because the turning component will have only minor impact (and zero impact in the mentioned extreme case). Which doesn't seem right at all ;)

##### Share on other sites
i have one stupid question why are you not implementing time at all?

where is time?

and some misunderstandings: missile will turn alpha and gamma angle with specific velocity/ :PPPPP

draw a curve and think of time speed and angle dependencies

just create your own angular speed dependency , in example if missile has vel speed about 2,5 mach it will turn X degrees in one second , so it should make X/2 angle in one second with 1,75 mach speed etc,

well i have never tested it before....

##### Share on other sites
Plug your current heading vector into a rotation matrix. Compute the matrix based off of angle rotation per second. That is your new heading.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015830
×