Inverse Kinematics Help: Math Behind FABRIK

I’ve been aiming to atleast start out and get into the whole IK (Inverse Kinematics) area and was recently suggested to learn from a Wiki article about FABRIK by a friend. I have been trying to understand some solving code from said article and have found myself stuck, here’s the solving code:

function chain:solve()
	local distance = (self.joints[1] - self.target).magnitude;
	if distance > self.totallength then
		-- target is out of reach
		for i = 1, self.n - 1 do
			-- create direct line to target and travel join distance across it
			local r = (self.target - self.joints[i]).magnitude;
			local l = self.lengths[i] / r;
			-- find new joint position
			self.joints[i+1] = (1 - l) * self.joints[i] + l * self.target;
		end;
	end;
end;

Here’s what I have failed to grasp (Line 6-10):

-- create direct line to target and travel join distance across it
local r = (self.target - self.joints[i]).magnitude;
local l = self.lengths[i] / r;
-- find new joint position
self.joints[i+1] = (1 - l) * self.joints[i] + l * self.target;

I’m somewhat familiar with the whole concept of FABRIK (I watched EgoMoose’s video on FABRIK) being based on anchoring one end and constructing the whole system from that point. However, I can’t understand any math that happens in this part except the literal fact that r gets the magnitude of the difference of the join from the the target, I’ve comprehended nothing else. I’ve tried looking at it practically and have asked around but I’m still stuck up here. I would heavily appreciate any attempts to even slightly describe what’s going on in that code or any links/concepts one might suggest to learn from.

Thank you,
Ankur_007

Maybe THIS will help?

To me (I have no experience with IK :joy:) it looks like the lines you’re talking about are constraining the parts to their max length.

You can see that l is a ratio between the distance and max length (so it’s getting the ratio of how far off it is basically)

Multiplying this by the target again I’m assuming will basically move the length for that connection to as close as it can get to the target but allows rotation in any direction since rotation isn’t taken into account.

Keep in mind it’s setting the next joint meaning there will be math performed on the constrained joint in the next run.

There is a segment just before which (probably where the name comes from) inverses the ratio of the length and applies it to the “previous” joint’s position, and then adds it to our “current” joint.

I’m not sure the entire effect that is happening here and can’t do any testing to see how exactly this works, but to me it looks like this is both constraining the cloth, and also making each piece react to the movement of the last piece causing the cloth to swing and react to its own movement.

The reason the ratio is reversed is probably conservation of movement since this ratio is losing some amount of movement when corrected since we know this value is further than it’s max length so we have to account for that using our inverse of that ratio which will cumulate through the entire cloth.

https://forum.scriptinghelpers.org/topic/680/roblox-single-joint-inverse-kinematics-using-law-of-cosines
I made this tutorial months ago but it covers single joint inverse kinematics, you may be interested.

1 Like