# Rotate to face a direction, by applying angular acceleration?

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

## Recommended Posts

This seems like it should be simple, but I've been struggling with it for a while.

I have a starship that has an associated ODE body. I'm trying to get the starship to follow a path. So I calculate the desired heading as a vector.

The problem is I need to find the angular force to apply (as a vector) to rotate the ship from its current heading (forward vector) to face the desired heading.

Could someone explain how to calculate that angular force?

##### Share on other sites

Besides the current heading, you probably want to take the current angular velocity into account. You want to reach the desired heading with an angular velocity of 0. The problem is similar to the "arrival" steering behavior. I would start by looking at some implementation of that and adapt it.

##### Share on other sites

Yeah, I need to do that. So I've worked out that angular rotation is represented by the axis of rotation, and the magnitude is the amount of rotation. This means that I can use the cross-product between the two vectors to work out the torque I need to apply.

This seems to work for small rotations, but the moment my ship crashes into something the whole thing goes haywire. I'm going to debug some more but if using the cross-product is a bad idea for some reason please let me know! Are there edge cases when the difference between the two vectors is large?

##### Share on other sites
No, I wouldn't say there are edge cases, but some naive implementation might leave you with a tiny torque in situations where you are facing close to the opposite of where you want. You can use the cross product to determine which axis to rotate, but then use something like the arccosine of the dot product to measure the angle.

##### Share on other sites

The exact torque is

torque = -(1 / timestep^2 * angle * stiffnes + 1/timestep * angular velocity * damping) * moment of inertia

where stiffnes and damping is an arbitrary number between 0 and 1, defining how fast you want it done. If you would like the ship to align in one loop, just set both to 1.

Cheers,

Mike

##### Share on other sites

@Mike, just a few questions about that equation:

• Is "timestep" the fractional 1/60.0 or the number of steps in a second (e.g. 60)?
• Is "angle", the result of acos(desired.dot(current)) ?
• Is "angular velocity" the current angular velocity of the ship?
• What's "moment of inertia"?

Sorry if I'm being dumb :)

##### Share on other sites

moment of inertia is a physics term, the rest I'm not sure.

(variables with descriptive names are nice, if one was called currentAngle or desiredAngle it gets more intuitive, but I digress)

##### Share on other sites

Timestep is the delta time of your game, for instance 1/60.

I supose you could use acos(desired.dot(current)), but it is not safe, since it could in some situations force the ship to rotate the longer way around, rather than the shorter. The unambigious and 100% safe way to do it would be atan2( desired.perpdot(current), desired.dot(current) ).

Perpdot is the perpendicular dot product or "2D cross product". The "regular" dot product between two normalized vectors return cosine to the angle between them, and perpdot returns sine to the angle.

http://mathworld.wolfram.com/PerpDotProduct.html

Yes, the angular velocity is the current one.

Moment of inertia is "rotational mass". Just like mass, you can in principle give it any value you want, but if you set it too high or too low, the physics will look weird.

Check out these wikipedia articles to understand the concepts of rotation. You'll need it if you want to play with physics. :-)

http://en.wikipedia.org/wiki/Moment_of_inertia

http://en.wikipedia.org/wiki/Torque

http://en.wikipedia.org/wiki/Angular_acceleration

http://en.wikipedia.org/wiki/Angular_velocity

Cheers,

Mike

Edited by h4tt3n

• ### What is your GameDev Story?

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

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

• Total Topics
634078
• Total Posts
3015370
×