A barebones implementation of the FABRIK algorithm ("Forward And Backward Reaching IK"). Move the mouse around to control the IK target point.

I think there's something kind of cute and coincidental about the way that it actually sorta looks like fabric.

FABRIK is a clever trick that's surprisingly straightforward to implement, and it gives some really natural and performant results. The general principle is that instead of doing gradient descent or other fancy math for IK, you just imagine that your IK chain is a string (for 2D strings, imagine that they're resting on a table, and you're viewing from above).

At each tick, you perform two loops through the chain: First, you "pull the string" by the end point to the target position (and some amount of the rest of the string will get pulled along with it). After you've done this, you do the same thing, but this time, you pull the root of the string to the anchor point. (If the IK chain is a person's arm, then the anchor point is the shoulder.)

The source code also includes a 2D-distance check that tries to avoid number-overflow (by sacrificing some precision) when you give it a large enough vector. Might not be tuned perfectly (I really just guessed about the "safe range"), but maybe some motivated party can math out a more rigorous version of the function!

[Please log in to post a comment]