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!
Help with comparing rotations beyond +-Pi
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- severedskullz
- Prole
- Posts: 36
- Joined: Thu May 30, 2013 1:55 am
Help with comparing rotations beyond +-Pi
- 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 151 times
Re: Help with comparing rotations beyond +-Pi
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:
The result now always is between -pi and pi (-180° and 180°).
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
Check out my blog on gamedev
- severedskullz
- Prole
- Posts: 36
- Joined: Thu May 30, 2013 1:55 am
Re: Help with comparing rotations beyond +-Pi
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.micha wrote:The result now always is between -pi and pi (-180° and 180°).
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
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.
Check out my blog on gamedev
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Help with comparing rotations beyond +-Pi
Which was exactly what his code did, and the resulting difference would then be in that range.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.
- severedskullz
- Prole
- Posts: 36
- Joined: Thu May 30, 2013 1:55 am
Re: Help with comparing rotations beyond +-Pi
Then I completely misunderstood his solution. Sorry.bartbes wrote:Which was exactly what his code did, and the resulting difference would then be in that range.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.
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.
Who is online
Users browsing this forum: No registered users and 12 guests