English 中文(简体)
Flipping an angle using radians
原标题:

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

最佳回答

Add/subtract pi instead.

问题回答

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);
}

object_sprite.rotation = warp_direction - 3.14;





相关问题
Find 2 points on a 3d object and get their distance

Ok. So I guess thing would to be envision new york city and beijing highlighted on google earth... I m trying to figure out how to map points onto a 3d primitive object (a sphere), get their distance ...

get cosine similarity between two documents in lucene

i have built an index in Lucene. I want without specifying a query, just to get a score (cosine similarity or another distance?) between two documents in the index. For example i am getting from ...

Get the slope from one point and an angle in degrees

In javascript, I am trying to draw a line that is at an angle that is user defined. Basically, I have a point (x,y) and an angle to create the next point at. The length of the line need to be 10px. ...

how to extrude a path in 3d?

I m trying to extrude a path in 3d. Nothing fancy yet, just following some points and using a regular polygon for tubing . I m using Processing for now to quickly prototype, but will later turn the ...

How to use the PI constant in C++

I want to use the PI constant and trigonometric functions in some C++ program. I get the trigonometric functions with include <math.h>. However, there doesn t seem to be a definition for PI in ...

热门标签