Page 1 of 1
Help with comparing rotations beyond +-Pi
Posted: Sat Jun 15, 2013 4:43 am
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!
Re: Help with comparing rotations beyond +-Pi
Posted: Sun Jun 16, 2013 1:32 pm
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°).
Re: Help with comparing rotations beyond +-Pi
Posted: Sun Jun 16, 2013 2:32 pm
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.
Re: Help with comparing rotations beyond +-Pi
Posted: Sun Jun 16, 2013 3:27 pm
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.
Re: Help with comparing rotations beyond +-Pi
Posted: Sun Jun 16, 2013 4:01 pm
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.
Re: Help with comparing rotations beyond +-Pi
Posted: Sun Jun 16, 2013 4:18 pm
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.