# Weird (But Simple?) Angle Math

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

## Recommended Posts

I'm not quite sure why but the engine I'm using is returning odd angles for which direction an entity is facing.  It looks like this:

Now, I'd like to turn the entity from its current angle, X, to a given angle, Y but only using the fastest way possible.  If it's facing 90 degrees and the target is 100 degrees, it should use the smaller 10 degree turn, not the 350 one.

Does anyone have any C code or pseudo code or even just general tips they can give me to easily achieve this using the above angle system?

Thanks!

##### Share on other sites

Calculate the difference between the start and the end angle to get the angle of rotation in the counter-clockwise direction. If the angle is less than -180 or greater than 180, add or subtract 360, respectively. The final angle is the shortest distance from the start to the end angle.

##### Share on other sites

Heh, that system reminds me of euler angles, for example, in unity, that would be the euler Y angle.

Can't you just take the acos of the dot product of the vectors?

##### Share on other sites

Can't you just take the acos of the dot product of the vectors?

That would tell you the angle between them without a sign. You also need to know which way to rotate.

If the vectors are represented by complex numbers and rotations are represented by unit-length complex numbers, the code to compute how to turn one into the other is simply this:

Complex compute_rotation(Complex initial, Complex target) {
return target * conj(initial);
}

EDIT: If initial' and target' aren't guaranteed to be unit-length complex numbers, you have to normalize the result (i.e., divide it by its length).

Edited by Álvaro

##### Share on other sites

Bzroom actually corrected me on this a month ago. I was using the dot product, but you can simplify some things with the cross product. Core code:

function Update()
{
var rotationRate = 5 * Math.PI / 180;

var direction = mousePosition.Subtract(playerPosition).Normalize();
// Cross product of playerDirection with direction
var angleError = Math.asin(Vector.Cross(playerDirection, direction));
// Clamp(angleError, -rotationRate, rotationRate)
if (angleError < -rotationRate) angleError = -rotationRate;
if (angleError > rotationRate) angleError = rotationRate;
// Rotate by angleError
var temp = Vector.Create();
temp.X = Math.cos(angleError) * playerDirection.X - Math.sin(angleError) * playerDirection.Y;
temp.Y = Math.cos(angleError) * playerDirection.Y + Math.sin(angleError) * playerDirection.X;
playerDirection.X = temp.X;
playerDirection.Y = temp.Y;
Render();
}


Javascript demo. (Right click to view source). Should give you the basic idea.

##### Share on other sites

Can't you just take the acos of the dot product of the vectors?

That would tell you the angle between them without a sign. You also need to know which way to rotate.

If the vectors are represented by complex numbers and rotations are represented by unit-length complex numbers, the code to compute how to turn one into the other is simply this:

Complex compute_rotation(Complex initial, Complex target) {
return target * conj(initial);
}

EDIT: If initial' and target' aren't guaranteed to be unit-length complex numbers, you have to normalize the result (i.e., divide it by its length).

This is somewhat offtopic, but do you have an article on your complex number scheme?  Might be nice to have it all in one spot to be perused, with some pseudo sample code, etc.

##### Share on other sites

This is somewhat offtopic, but do you have an article on your complex number scheme?  Might be nice to have it all in one spot to be perused, with some pseudo sample code, etc.

It's a good idea, but I haven't had much time lately. I'll see what I can do.

• ### What is your GameDev Story?

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

• 13
• 9
• 9
• 15
• 14
• ### Forum Statistics

• Total Topics
634068
• Total Posts
3015327
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!