• Advertisement
Sign in to follow this  

New question on radians

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

I can't help but feel like a noob once again, so I need to ask this before my head goes through the wall.

 

I'm working on a rotation algorithm. Basically, I have an angle (in rad) and a target angle (in rad) and I need to figure out whether the shortest rotation between the two is clockwise or counter-clockwise.

I've devised a bunch of algorithms, but all of them with complex exceptions to handle the wrap of the angle.

 

My circle is -Pi to Pi (where Pi and -Pi occupy the exact same position, which is the opposite to 0).

 

Anybody got a clue how to solve this 'elegantly'?

 

Thanks

 

Share this post


Link to post
Share on other sites
Advertisement

Yes: Stop using angles. If you represent orientations by unit-length complex numbers, in order to rotate from z1 to z2, you need to multiply by z2/z1. Whether that rotation is achieved going clockwise or counterclockwise depends on the sign of the imaginary part of z2/z1.

 

EDIT: Oh, since z1 has length 1, you can compute z2/z1 as z2*conj(z1).

Edited by Álvaro

Share this post


Link to post
Share on other sites

i don't understand why would need to "wrap the angle" more than once in your entire life?

if you have a function that does it, that should be the end of it

 

otherwise, there's quaternions and vectors

using vectors you can rotate around an axis without problems.. if its 2D its going to be really easy to implement

 

if you don't want to reinvent the wheel there's glm library that does all of this

not sure if it has quaternions, but either way someone on this forum has really good tutorials on basic quaternions

 

i also have basic implementations that i use myself here:

https://github.com/fwsGonzo/cppcraft/tree/master/source/library/math

just don't take everything i write at face value, since i'm the only one that has been using any of that

Share this post


Link to post
Share on other sites

I'm not sure what Z1 and Z2 are for?

All I have is angles in RAD...

Z1 and Z2 are the complex numbers at the corresponding angles, and with unit magnitude; Z1 = cos(angle1) + i*sin(angle1), and same for Z2 and angle2.

Share this post


Link to post
Share on other sites

That uses trig though but you can do it by applying the fmod (or fmodf for floats instead of doubles) function which may be faster.

 

Discussion here (starts off with degrees then various implementations for the radians case).

 

http://stackoverflow.com/questions/1878907/the-smallest-difference-between-2-angles

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

That uses trig though but you can do it by applying the fmod (or fmodf for floats instead of doubles) function which may be faster.

 

Discussion here (starts off with degrees then various implementations for the radians case).

 

http://stackoverflow.com/questions/1878907/the-smallest-difference-between-2-angles

The point is, if possible, to not use angles in the first place. The need for trigonometry may just be the result of using the wrong tool, and the correct solution may not involve any trigonometry at all if done correctly to begin with. That's why Alvaro gave the advice to stop using angles.

Share this post


Link to post
Share on other sites

Yes, if you start with a vector representation. But OP dude sounds like he has angles to start with (let's hope he didn't extract them from the vectors via atan2).

Share this post


Link to post
Share on other sites

I'm currently using Dart (Web Language) implementing StageXL.

StageXL works in such a way that my sprite has an innate field that contains its 'rotation' which it applies to the sprite.

I can GET and modify that rotation field which is represented by a number between -Pi to +Pi.

This rotation hence gives me my point of origin to perform the rotation.

 

The 'target' of my rotation is the result of an atan2(y, x) function sadly (possibly because I'm unfamiliar with the suggested approach above).

 


http://stackoverflow.com/questions/1878907/the-smallest-difference-between-2-angles

That's the exact article that got me confused in the first place :)

 

Seems like I've got much to learn from vectors and/or quaternions.

I'll start with the above info and will 'ping' if I'm still at a loss.

 

Thanks all!

Share this post


Link to post
Share on other sites

You can do it with some if statements (and probably some abs()) if you can't fmod or fmodf in your language.

 

Write down some cases (try with angles from all 4 quadrants), work out the expected answer for each combination, and work out the pattern. You need to work out how to handle the "shortest angle crosses the discontinuity" case.

Share this post


Link to post
Share on other sites

Ended up using the below statement:

 

_a+= (_a>math.PI) ?-2*math.PI : (_a<-math.PI) ? 2*math.PI : 0;

 

and it works wonderfully.

I'll still look into vectors though. If there's a better way, I want to be able to do it too (aka get better at problem solving).

 

Thanks again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement