Weird (But Simple?) Angle Math

Started by
5 comments, last by alvaro 10 years, 3 months ago

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:

circle.jpg

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!

Advertisement

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.

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?

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).

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.

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.


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.

This topic is closed to new replies.

Advertisement