MilGra
milgra
about
articles
projects

blogroll
bit-101
coding horror
blameit
failblog
beszeljukmac
sghu

navigation
posts
docs
admin

Advanced Inverse Kinematics


The inverse kinematics system is a point-based system, in which members take effect on each other according to a few formulas:

- Points have to keep their distance from each other
- Desired points have to preserve their angle or limiting angle from other desired points

Part 1 - Keeping distance

We have points A and B, the required distance between them is d. What happens if one of them moves farther?



Our task is simple in this case. If A moves farther, we can determine the directional angle of the two points, and we have to make the distance constant again.



We know every coordinate, so we can determine x and y distances, and with the use of tangent we can get the desired angle between A and B, finally we can calculate the new coordinates. Fig 2 is defined in a normal positioned coordinate system (in the first quarter of the cartesian system), don't forget about flash flipping the y axis, and the upper left corner is the origin.

It looks much more simple in actionscript, because we can use the fantastic Math.atan2 function, where only dy and dx distances are required, and its returning interval is between -¶ and +¶. But be careful: the angles are also flipped because of the flipped y-axis !

Source Code







Source File

To make things more spectacular, we should try to make an IK chain at this point. The easiest way to implement it is a recursive moving function.

Source Code

We store the neighbours in an array, so that a member can have an arbitrary number of neighbours, but there must not be a circle in the chain, because it results in a neverending loop.







Source File

Part 2 - Preserving angle

We have a quite nice chain now, but it is a little bit awkward. Let's see how we should maintain a constant angle relative to a third point.



It's quite clear that we first have to determine the angle between the two parent-points (A and B), and with the constant opening-angle (alpha) we can calculate the position of the third point. (It is much more simple in actionscript because of Math.atan2). But our code should be more complicated because we want to configure each member separately, therefore, defining an IKMember class is necessary. We also have to define the SetAngles function, which calculates the angles of the fixed angled neighbours.

Source Code

The result:







Source File

Part 3 - Maintaining limiting angles and making the junction point relative

Here comes the most difficult and most spectacular part: we have to restrict the movement interval of the points between limiting angles. We have to check, that the points fixated in Part 1 are within, above or under the limiting interval, if they aren`t within, we have to reposition them to the closest limit.



If B moves farther, the other two points have to move as well, preserving the minimal limiting angle alpha. To achieve this, we have to calculate betha between A and B, and the minimal distance mind, what minalpha can permit.

The easiest way to calculate mind is to place the CAB triangle in the origin of a coordinate system, and with the help of ar, br, minalpha and Pythagoras we can calculate the missing data.



If we have mind and epsylon, we also have the position of the other two points as shown in fig 6.



To achieve this in flash we have to further improve our source.

Source Code

As you see, i made a little craft here: we have to observe which neighbour calls our member, for if we adjusted the members to the junction point, it would be unnatural; imagine people walking with a static knee, with only the ankle and the haunch moving.

Showcase
Source File

I configured something skeleton-like to close the tutorial.







Source File

If you want to know more about the algorithm, check my IKGround prototype.

I hope my first tutorial was useful. Don't let the pay-sites gag you, don't forget what Plato said: wisdom can't and mustn't be sold for money. I also encourage you to send tutorials if you can. Oh, and also don't forget what Bill Gates said: 640K should be enough for everything, so code everything you can, and don't go over 640Kbit/8/1024=78,125 Kbyte!!! :)

Article on gotoandplay.it

MilGra


powered by kure