Jump to content

  • Log In with Google      Sign In   
  • Create Account


New question on radians


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.

  • You cannot reply to this topic
11 replies to this topic

#1 Orymus3   Crossbones+   -  Reputation: 7149

Like
1Likes
Like

Posted 14 October 2013 - 01:06 PM

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

 



Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12514

Like
3Likes
Like

Posted 14 October 2013 - 01:43 PM

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, 14 October 2013 - 01:45 PM.


#3 Orymus3   Crossbones+   -  Reputation: 7149

Like
0Likes
Like

Posted 14 October 2013 - 02:04 PM

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

All I have is angles in RAD...



#4 Kaptein   Prime Members   -  Reputation: 1971

Like
2Likes
Like

Posted 14 October 2013 - 02:05 PM

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



#5 Brother Bob   Moderators   -  Reputation: 7928

Like
3Likes
Like

Posted 14 October 2013 - 02:10 PM

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.



#6 Paradigm Shifter   Crossbones+   -  Reputation: 5236

Like
2Likes
Like

Posted 14 October 2013 - 02:22 PM

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, 14 October 2013 - 02:23 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 Brother Bob   Moderators   -  Reputation: 7928

Like
2Likes
Like

Posted 14 October 2013 - 02:25 PM

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.



#8 Paradigm Shifter   Crossbones+   -  Reputation: 5236

Like
1Likes
Like

Posted 14 October 2013 - 02:31 PM

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


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#9 Orymus3   Crossbones+   -  Reputation: 7149

Like
0Likes
Like

Posted 14 October 2013 - 02:47 PM

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!



#10 Paradigm Shifter   Crossbones+   -  Reputation: 5236

Like
0Likes
Like

Posted 14 October 2013 - 03:10 PM

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.


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#11 Orymus3   Crossbones+   -  Reputation: 7149

Like
0Likes
Like

Posted 14 October 2013 - 07:58 PM

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.



#12 Paradigm Shifter   Crossbones+   -  Reputation: 5236

Like
0Likes
Like

Posted 14 October 2013 - 08:46 PM

Yeah, definitely look into vectors. Trig and vectors really are the key to getting better at game maths,


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley




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.



PARTNERS