# Autopilot/Steering/Orientation

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

## Recommended Posts

Hi guys, I'm using some basic steering behaviors to move my objects; I'm having problems with the orientations though; in particular when the object makes a complete u-turn. In that scenario the point mass comes to a stop and essentially reverses; I really want it to actually turn and start heading the opposite direction; either that and/or have it stop, then rotate 180 deg and start heading the other direction. If I have an orientation pointing down the positive z-axis and I want it pointing down the negative z-axis it's easy to think "alright, I'll just rotate around the y-axis Pi rads" but how do I do that programatically, regardless of it's current orientation ? I think if I understand that I can solve the problem above. This is for a spaceship in full 3D space. Thanks.

##### Share on other sites
Just a hunch..
Are your orientations based on euler angles? If yes, are you clipping(ok, maybe this is not the best word here)them to an interval like [-Pi, Pi)?
If so, doing an u-turn will cause your angles to go to their corresponding values in the above mentioned interval, like 350º -> -10º.

Depending on what you mean by "basic steering behaviours", these could get your spaceship to 'think' it has the right orientation when, in fact, it doesn't.
There's a lot of assumptions, but I warned you, just a hunch

##### Share on other sites
You may already have this part figured out, but the first step in solving this problem will be to compute a new target orientation given the object's current orientation and velocity.

To find the target orientation, construct a rotation that will rotate the current forward vector onto the velocity vector over the shortest arc. This can be done as follows:

1. Compute the axis of rotation as the normalized cross product of the forward vector and the velocity vector.

2. Compute the angle of rotation (this would be the angle between the two vectors - there are a few different ways to compute this).

Now, there are two special cases you have to consider.

The first special case is when the two input vectors are already aligned or nearly aligned. When this is the case, the magnitude of the computed cross product will be small, and the dot product of the two vectors will be positive. In this case, just return identity.

The second special case is when the two input vectors are oppositely aligned or nearly oppositely aligned. When this is the case, the magnitude of the computed cross product will be small, and the dot product of the two vectors will be negative. In this case, return a rotation angle of pi, and any axis that is perpendicular to the two input vectors (the local up vector of the object would probably be a good choice).

Finally, concatenate this rotation with the current orientation to yield the target orientation.

Now, if you just assign the new orientation directly, you'll have the same problem you do now: in certain cases (such as when the object reverses direction abruptly), the object will 'snap' suddenly to a new orientation. There are various ways to get around this; for example, you could instead interpolate to the new orientation gradually (e.g. by using SLERP).

[Edit: There's one other special case to consider when building the target orientation, and that is when the speed of the object is near zero. In this case, just return identity.]

• ### What is your GameDev Story?

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

• 13
• 10
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634125
• Total Posts
3015666
×