# Changing orientation by applying torques

Suppose you've got an object floating freely in space. You have a vector you want this object to point towards, and a vector representing the direction it's currently facing. From these two, you can get the rotation (matrix, quaternion, whatever) that represents the change in orientation to bring the two vectors into alignment.

If you only have the ability to apply torque (derivative of angular velocity) to your object, what's a good algorithm for applying torque over time that won't over/undershoot the destination?

(In this case, it's a space ship that wants to automatically orient itself in the direction of travel using thrusters. Roll is irrelevant.)

• Are you trying to optimize fuel use (total torque exerted) or speed? Also, is the ship starting off with zero angular velocity at the beginning of this operation? Jul 14 '10 at 21:01
• Well, to optimize for speed would be to have neigh-infinite acceleration, and to optimize for fuel use would be to have nearly zero acceleration. So, in practice, I'll use a trade-off that looks good. As for starting angular velocity, hopefully I'll be able to include that in the calculation naturally. Jul 14 '10 at 21:34

This can be handled like the similar case for linear acceleration.

First fact to note: Because the ship starts off with an angular velocity of zero, an you want it to end up with an angular velocity of zero, this means that the total change in velocity must equal zero.

From this we can see that the integral of the acceleration over time must equal zero -- there must be exactly as "much" positive acceleration as there is negative acceleration.

Therefore, your solution, whatever it is, must be constrained to this property: Equal "total" acceleration forwards and backwards.

Here is what the shape of your acceleration over time graph should follow: Looking at this, there are so many possible forms and shapes your acceleration could be in! Let us make some assumptions for the shape of acceleration that you want, in order to give an easy/concise answer.

For the sake of a simple answer, I will have acceleration be in one of three states: forwards, backwards, or zero. Forwards and backwards will be at equal magnitude, and the states can be switched instantaneously. (there is no gradual ramping up of acceleration)

You can find the change in distance for a given acceleration over a given period of time with this equation:

s = 0.5*a*t^2


The simplest solution here would be to accelerate until you reach the halfway point, then decelerate the rest of the way.

We will take P as the total distance you want to move:

s = P/2
P/2 = 0.5*a*t^2
P = a*t^2
t^2 = P/a
t = sqrt(P/a)


So basically:

1. Accelerate at a for sqrt(P/a) units of time (units based off your units for acceleration)
2. Decelerate at the same magnitude for the same amount of time

This is not the only solution. It will get you there in the fastest amount of time (2*sqrt(P/a)). But what if you want a more relaxed version?

In this case, you can accelerate 1/3rd of the way, coast for the 1/3rd, and decelerate the rest of the third. Or 1/4th, coast for 1/2, decelerate 1/4th as well.

Or maybe you could accelerate for a fixed amount of time, and then decelerate for a fixed amount of time, but wait until you arrive at the exact position before you start decelerating.

• You're very correct about this being the case for linear motion. My main concern was using an algorithm like this with a physics engine that only provides me with yaw, pitch, and roll control for torque. Those axes aren't orthogonal once you start rotating, so I was hoping that there was a better solution. Your approach to timing, however, will still apply regardless. Jul 14 '10 at 21:37
• Ah; sorry, I must not have understood your question. I'm not sure I can answer more thoroughly without seeing the specs of your engine. But surely there is a way to apply constant torque in a constant direction? Jul 14 '10 at 21:42
• Yeah, your answer is probably as close as possible. I solved my particular problem by hacking at the quaternions until I got something to which the linear algorithm would apply. Thanks! Jul 15 '10 at 7:12