Angles, radians conversion question... Gente?

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Angles, radians conversion question... Gente?

Post by heyvern »

:)

Genete I have a question once again about converting angles to radians etc.

In my aimer script I am using the code from the LM translate bone tool for frame 0. This will "flip" the angle when it hits 0 all the way around to 360. For the original purpose of this code this makes perfect sense. For my purposes this causes a problem.

Here is the code for that angle conversion based on translating the target bone:

Code: Select all

                local angle = aimer.fAngle
                angle = angle + math.atan2(boneVec.y, boneVec.x)
What happens is that the angle of the aimer bone is fine. It flips from 0 to 360 and everything is just fine. But when I use a constraint the constrained bone flips all the around to the opposite side when it crosses that 0 angle point.

Is there any "magical" equations in your bag of tricks to convert that angle before applying it that wouldn't cause a constraint to act oddly?

There was some "extra" bit of code from the original LM bone translation tool that I removed for my script:

Code: Select all

--[[                
                while angle > 2 * math.pi do
                        angle = angle - 2 * math.pi
                end
                while angle < 0 do
                        angle = angle + 2 * math.pi
                end
--]]
Removing this code stopped the AIMER bone from "flipping" rotation... but it doesn't help with constraints.

-vern
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

What happens is that the angle of the aimer bone is fine. It flips from 0 to 360 and everything is just fine. But when I use a constraint the constrained bone flips all the around to the opposite side when it crosses that 0 angle point.
I'm sorry but I cannot reproduce the problem. Or I can but don't understand it.

Please verify the problem in this anme file (it must be downloaded in a folder with aim_bone.lua file the last version you have in the first post)
http://www.darthfurby.com/genete/Scripting/test.anme

In this file the aim bone cross from 0 to 180 in two frames but as well as its length is so small we don't notice. The constrained bone is rotated suddenly from 0 to 180. But if I make a complete turn (from frames 54 to 86) then you can see a curious thing:

Aimer bone never reaches more than -180 or +180. It is due to its angle comes form atan2. atan2 always return a value between +pi -pi (-180, 180). The constrained bone follows the aimer bone with no jump when performing the 360 turn.
lua programming reference manual wrote: math.atan2 (y, x)

Returns the arc tangent of y/x (in radians), but uses the signs of both parameters to find the quadrant of the result. (It also handles correctly the case of x being zero.)
So I really don't understand you... Can you put an example, please. :roll:

Regarding the second portion of code.
2*math.pi = 360 degrees.

So this smart portion of code encloses the angle inside a 0-360 segment. As well as it is always removing 2*pi (360) it is like removing complete turns of rotation so a big positive or negative angle is converted to a 0 360 angle pointing to the same direction.

In that case the angle value comes from a previous calculation and a big +/- angle is calculated. To avoid infinite increment of that value it is reduced to its canonical situation (0-360).
Best
-G
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

You are right. I can't replicate it either.

Apparently that code change DID fix it and I was using the wrong version of my script. :oops:

Originally the problem came from the - 2 * math.pi + 2 * math.pi bit that "reversed" the angle which is only needed on frame 0 when dragging the tip of a bone with the bone translate tool. That doesn't apply here.

Sorry for the confusion.

-vern
Genete
Posts: 3483
Joined: Tue Oct 17, 2006 3:27 pm
Location: España / Spain

Post by Genete »

Apparently that code change DID fix it and I was using the wrong version of my script. :oops:
:evil:

...


:D :wink:
Post Reply