Flipping an angle using radians
Asked Answered
S

3

8

Hello all you math whizzes out there!

I am struggling with a math problem I am hoping you can help me with. I have calculated an angle of direction using radians. Within OpenGL ES I move my guy by changing my point value as such:



spriteLocation.x -= playerSpeed * cosf(playerRadAngle);
spriteLocation.y -= playerSpeed * sinf(playerRadAngle);

// playerRadAgnle is my angle of direction using radians

This works very well to move my sprite in the correct direction. However, I have decided to keep my sprite "locked" in the middle of the screen and move the background instead. This requires me to Reverse my calculated angle. If my sprite's direction in radians is equivalent to 90 degrees, I want to convert it to 270 degrees. Again, keeping everything in radians.

I will admit that my knowledge of Trig is poor at best. Is there a way to figure out the opposite angle using radians? I know I could convert my radians into degrees, then add/subtract 180 degrees, then convert back to radians, but I'm looking for something more efficient.

Thanks in advance....

-Scott

Shall answered 15/2, 2010 at 2:31 Comment(0)
C
19

Add/subtract pi instead.

Corpulence answered 15/2, 2010 at 2:37 Comment(3)
Subtracting playerRadAngle from 2*PI works for the Y axis, but has no effect on the X axis. Since I am doing cosf() on the X and sinf() on the Y, what calculation can I make to playerRadAngle to get this done? Again, my Trig skills suck! Thanks for the quick reply!Shall
He didn't say subtract from 2*pi, he said add (or subtract) 1*pi. Did you try that?Emie
Ahhhh, he changed his post after I read it. Yes, this worked perfectly!!! Thank you both for the quick posts!Shall
P
4

You need to add Pi and then use the remainder after division by 2 Pi (to make it restricted within [0; 2 Pi] range).

JavaScript:

function invertAngle(angle) {
  return (angle + Math.PI) % (2 * Math.PI);
}
Pentheas answered 7/12, 2018 at 15:40 Comment(0)
R
0

object_sprite.rotation = warp_direction - 3.14;

Recapitulation answered 4/6, 2017 at 3:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.