• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Weird (But Simple?) Angle Math

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1GameCreator  Members

Posted 08 January 2014 - 05:07 PM

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!

### #2Brother Bob  Moderators

Posted 08 January 2014 - 05:28 PM

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.

### #3ferrous  Members

Posted 08 January 2014 - 06:14 PM

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?

### #4Álvaro  Members

Posted 08 January 2014 - 08:18 PM

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, 08 January 2014 - 08:46 PM.

### #5Sirisian  Members

Posted 10 January 2014 - 12:35 AM

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.

### #6ferrous  Members

Posted 10 January 2014 - 06:50 PM

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.

### #7Álvaro  Members

Posted 10 January 2014 - 08:04 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.