Can someone help me with the maths?

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
Rasheed
Posts: 2008
Joined: Tue May 17, 2005 8:30 am
Location: The Netherlands

Can someone help me with the maths?

Post by Rasheed »

I want to link bones in the same bone layer that are not linked. I want to use bone animation in a layer script, to create a dynamic link. This problem occurred when I wanted to do inverse kinematics on a chain of bones that was branched. If you do IK on a child, the IK will go no further than any parent that has several child bones.

In the screenshot below, the red colored bone is selected and only that bone and the blue parent bone will react on the IK tool, but the green parents will not, because the parent of the blue bone has two child bones.

Image

For an animation of a segmented creature I'm creating, I want to be able to do IK on a chain of bones, but still have branches attached to each of the bones in the chain. Those branches represent the legs, four legs for each segment.

If I can somehow modify the bone position of the unattached branches, I will have to calculate where those branches are, relative to the position of the "parent" bone (it could be called a "virtual parent", because it behave like a parent, but it isn't attached to the branch-bone in a parent-child relationship.

I hope I'm still perfectly clear to you.

Now for the maths. And maths isn't my strong point, alas. So would someone with more recent experience in this matter review my formulas?
___________

Image

Suppose, I have the position of the virtual parent bone P, and the position of the virtual child bone Q. The position of Q will depend on three values:
  1. position of point P
  2. angle of the parent bone, alpha
  3. scale of the parent bone, s
These values may change during the animation, so they are time (or frame) dependent.

Because Q will rotate around P (due to the changing bone angle of the virtual parent bone in P), I first need to transform the coordinates of Q so, that P is at the origin (0,0). If I know where the position of P is in each frame, I can simple at its coordinates to the coordinates calculated in the transformed coordinates system.

For P = ( xp(t) , yp(t) ) and Q = ( xq(t) , yq(t) )

u(t) = xq(t) - xp(t) [1a]
v(t) = yq(t) - yp(t) [1b]

I can now express the cartesian coordinates as polar coordinates

s(t) is bone scale of parent bone

m is the distance between points P and Q, for a scale equal to one:

m = sqrt ( u(0) * u(0) + v(0) * v(0) ) / s(0)

u(t) = m * s(t) * cos( beta + alpha ) [2a]
v(t) = m * s(t) * sin( beta + alpha ) [2b]

because at t=0, alpha is zero, this applies:

u(0) = m * s(0) * cos( beta )
v(0) = m * s(0) * sin( beta )

or:

cos( beta ) = u(0) / ( m * s(0) ) [3a]
sin( beta ) = v(0) / ( m * s(0) ) [3b]

From this maths source I know that this applies:

cos( beta + alpha ) = cos( beta ) * cos( alpha ) - sin( beta ) * sin( alpha )
sin( beta + alpha ) = sin( beta ) * cos( alpha ) + cos( beta ) * sin( alpha )

If [3a] and [3b] are substituted, this applies:

cos( beta + alpha )
= u(0) * cos( alpha ) / ( m * s(0) )
- v(0) * sin( alpha ) / ( m * s(0) )

sin( beta + alpha )
= v(0) * cos( alpha ) / ( m * s(0) )
+ u(0) * sin( alpha ) / ( m * s(0) )

If [2a] and [2b] are substituted, this applies:

u(t) = ( s(t) / s(0) ) * ( u(0) * cos( alpha ) - v(0) * sin( alpha ) )
v(t) = ( s(t) / s(0) ) * ( v(0) * cos( alpha ) - u(0) * sin( alpha ) )

If [1a] and [1b] are substituted, this applies:

xq(t) = xp(t) + ( s(t) / s(0) ) * ( u(0) * cos( alpha ) - v(0) * sin( alpha ) )
yq(t) = yp(t) + ( s(t) / s(0) ) * ( v(0) * cos( alpha ) - u(0) * sin( alpha ) )

in which:
s(0), u(0) and v(0) are constants
xp(t), yp(t), s(t) and alpha are variables, depending on the values of the bone position, bone angle and bone scale of the virtual parent bone in frame number t
__________

Is this reasoning correct. I need to know, otherwise I can't really continue. Perhaps someone has found a similar solution, or can point me to a web page with a solution, so I can check if I'm correct.

Thanks in advance.
User avatar
jahnocli
Posts: 3471
Joined: Fri Oct 29, 2004 2:13 pm
Location: UK

Post by jahnocli »

GoToAndPlay may be what you are looking for -- check it out anyway...

J
You can't have everything. Where would you put it?
User avatar
Rasheed
Posts: 2008
Joined: Tue May 17, 2005 8:30 am
Location: The Netherlands

Post by Rasheed »

Thank you so much for that link. It seems that Lost Marble has made some decisions with inverse kinematics which I can't use:
  1. no IK if a parent bone has more than one child
  2. IK only flows from child to parent, but not the other way around
Perhaps I should investigate if I can modify the Bone Manipulation tool so, that IK works both ways, and also if a parent has more child bones.
Post Reply