Help with comparing rotations beyond +-Pi

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
severedskullz
Prole
Posts: 36
Joined: Thu May 30, 2013 1:55 am

Help with comparing rotations beyond +-Pi

Post by severedskullz »

For future reference '+-" means positive or negative

Hello everyone,

So I have attempted to come up with a solution on my own but no dice. I am essentially trying to create a turret that can only rotate X degrees from its original orientation. It works fine for any values between 0 and +-(X - degOfFreedom). Once I pass Pi, the signs flip and + becomes - and - becomes + when using math.atan2(). I cannot figure out how to rid of this problem.

tl/dr: At the bottom of the planet, the signs flip and screws up my math. Please help.

If anyone could help me out I would appreciate it!
Attachments
Terrae.love
Press T to create a turret at the current location

Green line indicates Centered aim vector.
White lines indicates Turret aim vectors
(19.57 KiB) Downloaded 137 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Help with comparing rotations beyond +-Pi

Post by micha »

I haven't looked into your code, so this answer is not adapted to your variable names.

Suppose you have two angles, alpha and beta, and you want to calculate the difference between the two angles. Substracting, of course, only works in limited cases as e.g. 0° and 359° are close together, while the two numbers have a large difference. Here is a line of code to determine angle difference:

Code: Select all

delta =(alpha-beta+math.pi)%(2*math.pi)-math.pi
The result now always is between -pi and pi (-180° and 180°).
User avatar
severedskullz
Prole
Posts: 36
Joined: Thu May 30, 2013 1:55 am

Re: Help with comparing rotations beyond +-Pi

Post by severedskullz »

micha wrote:The result now always is between -pi and pi (-180° and 180°).
I was already at that point by using math.atan2. What I am having trouble with is comparing additions and subtractions after determining the angle.
Say I have turret "A" at rotation 160 with 45 degrees of rotational freedom allowing me to rotate anywhere between 125 and 205 degrees. When I determine the angle of the turret beyond 180, math.atan2 will flip the signs and start to decrement. So If i was at logical angle 200, math.atan2 would return -165 which I cannot check with simple "if" statements.

Logically, -165 and 205 are equivalent, but I cannot figure out how to clamp the rotation for this specific case: minRot <= desiredRot <= maxRot
Further clarifying, once I go past +- 180, the maxRot becomes outside the bounds of (turretRot + 45].

Somehow I need to link -180 and 180 together and allow the opposite sides decrements to count as increments so I may use them in a comparison, but I cannot figure out how.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Help with comparing rotations beyond +-Pi

Post by micha »

To check if an angle alpha is in an interval, first calculate the mid-point of the interval (angle-wise). Then calculate the difference between this mid-angle and the given angle alpha (the formula in the post above takes care of the 180°-wrapping). If this difference is larger than half the span of the interval, then alpha is not in the range of the interval. Otherwise it is.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Help with comparing rotations beyond +-Pi

Post by bartbes »

severedskullz wrote: I was already at that point by using math.atan2. What I am having trouble with is comparing additions and subtractions after determining the angle.
Which was exactly what his code did, and the resulting difference would then be in that range.
User avatar
severedskullz
Prole
Posts: 36
Joined: Thu May 30, 2013 1:55 am

Re: Help with comparing rotations beyond +-Pi

Post by severedskullz »

bartbes wrote:
severedskullz wrote: I was already at that point by using math.atan2. What I am having trouble with is comparing additions and subtractions after determining the angle.
Which was exactly what his code did, and the resulting difference would then be in that range.
Then I completely misunderstood his solution. Sorry.

Edit so now 0 and Pi works fine, but now I'm having issues with anything between Pi/2 +- Pi/4 returning -Pi as the delta which is pointing in the opposite direction of where its supposed to be going... working on that now.
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests