Sunday, September 4, 2011

A Stretchy IK

I have been helping a co-worker learn some rigging techniques, and since I'm essentially making tutorials, I figured I might as well post them for others to benefit from as well!  Today's topic is how to make an IK limb stretchy.

The basic idea is that we will measure the original length of the IK from start joint to handle, and any time that distance increases beyond its original value, the bones of the IK limb will scale to make up the extra distance rather than rotating beyond what makes sense (the cause of popping).


Let's dive right in!

Since we need to track the distance as it updates, we make a distance tool, with the locators parented appropriately to the start joint and IK handle.


Then, to do our math we will need a multiply/divide node and either a condition node or clamp node.  These can be found under utilities in the hypershade.  People new to rigging often overlook utility nodes and go straight for expressions when they need a little math inserted.  Utility nodes are extremely quick to calculate in maya, however, while expressions are quite slow.  If you're serious about rigging, utility nodes have to be a tool in your arsenal.  If you've never worked with them before, take some time to go through them all and see what they do.


For the multiply/divide node set the operation to division.  We will divide the actual distance by the original or ideal distance.  Therefore we connect the actual value of the distance node into the top value of the divide node.  We type in the original value of the distance tool into the bottom value.  When the IK is at its original position the result of the division is 1.  If it moves out past that, the division will result in a value greater than 1.


If we stopped now and hooked our result up to the scale, the limb would always stretch to the IK's length.  The joints would never bend at all.  We don't want that, obviously, so we'll need to cap it off at a minimum of 1.  You can do this in either of 2 ways, with a condition node or with a clamp node.

The above shows a condition node so I'll start with that.  Set the condition node's operation to "less than."  The first term will be the connected in result from the division, and the second term will be 1.  The value if true should be 1, and the value if false should be the result from the division.  In the end it reads as: if the division's result is less than 1, it will return 1 instead.  But if not, it will return the actual number.

For a clamp node its even easier.  We connect the division result to the clamp's input, and then set the minimum value to 1.  The max value can be any number larger than anything we'll need (10, 100, whatever unrealistically high stretch this character is unlikely to hit).


Now that we have a good value to work with, connect the output x into the x scale of all the IK joints.  Provided your joints are properly oriented, the x direction points towards the next joint in the chain and the x scale, therefore, will only stretch the joints lengthwise towards one another.

So now if the IK handle moves in, our computed value stays locked at 1 and the IK functions normally, reaching its end point by rotation.  But if the IK handle goes out too far, instead of rotating into a popped position, the joints reach their end point by scaling proportional to the extra distance.

You can adjust the point at which the stretching starts by changing the bottom value in the division node.  Sometimes you will want to tweak it for better results. Sometimes it works wonderfully with the original value.  That part is up to your better judgement.

Have fun rigging!

No comments:

Post a Comment