# How do I figure out which direction to rotate my player, as to have the smallest rotation.

## Recommended Posts

I have a player who's rotation is y-axis only and controled by a function which sets a new rotation variable to the new value.

What I need to figure out is which direction to turn, so the player has to move the least. What messes me up is the fact that degrees are 0-360. Anything that dosen't have to go past the 0/360 point works fine.

My variables are the current rotation, and the wanted rotation. Any ideas? Thanks

##### Share on other sites

Angles make everything messy. You can subtract the current and the desired angles. The result (in degrees) will be between -360 and +360. The ranges between -360 and -180 and between 0 and +180 correspond to situations where you should increase the angle to get where you want to get. Otherwise, decrease it

Or you can ditch angles altogether, but I am on my cell phone and that would require some lengthy explanation.

##### Share on other sites

Use "angle % 360" (instead of just angle) inside your rotation function. % is the modulus operator in most c-like languages, your language may need different syntax. Also in c++ you need fmod() instead of % if using floating point, not sure about C.

Edited by fleabay

##### Share on other sites

Thanks guys

9 hours ago, alvaro said:

Angles make everything messy. You can subtract the current and the desired angles. The result (in degrees) will be between -360 and +360. The ranges between -360 and -180 and between 0 and +180 correspond to situations where you should increase the angle to get where you want to get. Otherwise, decrease it

Or you can ditch angles altogether, but I am on my cell phone and that would require some lengthy explanation.

int jump = 5;
int sub = rot - newRot;
if ((sub < -180 && sub > -360) || (sub < 180 && sub > 0)) rot -= jump;
else rot += jump;

It works now.

Edited by LazyDev

##### Share on other sites
// Rotating from 'from_angle' to 'to_angle' (range is 0..359 for both).

// Positive / negative rotation:
pos_rot = (360 + to_angle - from_angle) % 360;
neg_rot = (360 - pos_rot) % 360;
return (pos_rot < neg_rot) ? 1 : -1;

Edit: Further optimization can be to eliminate neg_rot, and compare with 180 instead.

Edited by Alberth

##### Share on other sites
7 hours ago, Alberth said:

// Rotating from 'from_angle' to 'to_angle' (range is 0..359 for both).

// Positive / negative rotation:
pos_rot = ﻿(360 + to_angle - from_angle) % 360﻿;
neg_rot = (360 - pos_rot) % 360﻿;﻿
return (pos_rot < neg_rot) ? 1 : -1;

Edit: Further optimization can be to eliminate neg_rot, and compare with 180 instead.

Thanks,

##### Share on other sites
On 11/22/2018 at 2:29 AM, alvaro said:

Or you can ditch angles altogether, but I am on my cell phone and that would require some lengthy explanation.

I am back at my computer, so I can flesh this out a bit.

I have been advocating for years now the use of unit-length complex numbers to represent 2D rotations. If you represent a point (x,y) by the complex number (x+i*y), applying a rotation is simply complex multiplication.

The C++ code to determine which direction you should rotate is then simply this:

  return sign(std::imag(desired / current));

Edited by alvaro

##### Share on other sites
On 11/24/2018 at 2:56 AM, Alberth said:

// Rotating from 'from_angle' to 'to_angle' (range is 0..359 for both).

// Positive / negative rotation:
pos_rot = (360 + to_angle - from_angle) % 360;
neg_rot = (360 - pos_rot) % 360;
return (pos_rot < neg_rot) ? 1 : -1;

Edit: Further optimization can be to eliminate neg_rot, and compare with 180 instead.

After a while, the player can only turn left for some reason 🤔

##### Share on other sites
Quote

After a while, the player can only turn left for some reason

I'm not sure why that is, but I'll put in another vote for not working with angles directly. Alvaro offered some suggestions above, and what you want can be done directly with vector math as well (maybe that would be equivalent to what Alvaro's suggesting - I didn't examine it closely).

If you need further info on the alternate methods proposed, someone should be able to offer further details.

##### Share on other sites
5 hours ago, LazyDev said:

After a while, the player can only turn left for some reason 🤔

You shouldn't treat code as a black box that magically produces the correct answer. The computation is right there and it's not difficult, so take some time to study and understand it. Once you understand how it is intended to work, get an example where it fails, and do the computation step by step (either in a debugger or even manually with a calculator). At which point does the example deviate from the intended answer? Why?

That is the information you want to start thinking about a solution.

If you want to discuss it here, please provide a concrete example of from_angle and to_angle where the code fails, so others can verify your findings, and play with the code to find the cause. It may also clarify what you mean, eg I have no idea what "left" means, the code only has positive and negative rotations, there is no left or right in there.

As I ran an exhaustive test while writing that code, my first random guess of your problem is that you're not obeying the input conditions. Of course my tests may be flawed as well, so I am quite interested in an example where the code gives an incorrect answer.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### What is your GameDev Story?

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

(You must login to your GameDev.net account.)

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

• Total Topics
634134
• Total Posts
3015749
×