<p>What you have here is an underconstrained problem*. To solve this problem you do what you do to any other underconstrained system. This is also why you mostly only see the 2 joint version as it is not underconstrained in the planar solution, and in any case the preregistration is trivial.</p>

<p>Basically you have 2 options:</p>

<ul>
<li>Add more constraints</li>
<li>Deal with the freedom</li>
</ul>

<p>Adding more constraints to the system reduces the degrees of freedom. For example if you only permit one of the joints to rotate about its z axis and the another around its x axis then y then you have reduced the problem to the same 2 member problem. Another way to constrain it is to force the orientation of the last link. This also reduces it to the same case as 2 links. But there are other meaningful ways to reduce complexity that depend on your application.</p>

<p><a href="https://i.sstatic.net/OoFDT.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OoFDT.png" alt="enter image description here"></a></p>

<p><strong>Image 1</strong>: Constraining one member reduces the problem to a 2 member problem. In the planar case that means 2 solutions (other in blue).</p>

<p>Dealing with the freedom just means that your answer is going to be a family of answers. Then you develop a way to choose the solution from that family. Letting the user adjust the parameter is equivalent to adding one more constraint. But there are other methods such as least squares, or energy methods that yield different results. Sky is the limit here. For example I have worked with robot controllers that decided the parameter based on what was the least amount of rotation on joints. The problem was that this wasn't entirely deterministic so over time the robot ended up doing things differently...</p>

<p>* This is more suited to Engineering.SE but since IK is a common graphics problem lets deal with it here.</p>

CC BY-SA 3.0
2017-03-01T12:25:08.923Z
Do you mean underconstrained?
StinkySkunk
CC BY-SA 3.0
2017-03-01T12:46:01.710Z
@StinkySkunk oh yes
joojaa
CC BY-SA 3.0
2017-03-01T18:12:40.767Z
To be honest, I'm not sure which option I'm searching for. What I do know is that I'm after a planar solution that moves like this: 

https://youtu.be/3sqd_CZNq7s?t=6s

It's short, but I'm looking at the part where the color chain is red-orange-yellow-blue.
Greg Gunn
CC BY-SA 3.0
2017-03-01T19:00:52.033Z
That does not seem very useful @GregGunn It does not even seem to solve any ik. Atleast we do not know if it is because we cant see the target.  It looks more like a system that moves the links based on proxility which is more like hbrid fk/ik. the only reason we even suspect its ik is because the title says so... Titles lie.
joojaa
CC BY-SA 3.0
2017-03-01T19:30:16.597Z
@joojaa If that reference is my goal—regardless of it's IK or not—how would you suggest I build something that moves like that?
Greg Gunn
CC BY-SA 3.0
2017-03-02T06:43:18.940Z
@GregGunn There is a inifinite mumber of solutions that does something. _You_ need to decide what is useful, i cant help you if there is no aim other than a abstract "something". You need to have a clear vision of what problem your ik is trying to solve. Onnce you can describe what the vide does with its 4th link and why thats useful then i can help. The trick to underconstrained problems is that you need to do the reasoning because the computer or anybody else who does not k.ow the purpose and specification can not do it for you.
joojaa
CC BY-SA 3.0
2017-03-01T09:30:44.637Z
2017-03-01T19:47:55.513Z
2017-03-01T19:47:55.513Z
joojaa
<p>I'm building a 2D inverse kinematics system and can't figure out how to find all the joint positions. I have the math to make a typical IK set up with a single bend (e.g. arm, leg), but I get stuck when adding another bend.</p>

<p><strong>How do I find the position of all joints for an inverse kinematic chain that is longer than 2 segments?</strong></p>

<p>Here's an example of the planar solution I'm looking for: <a href="https://youtu.be/3sqd_CZNq7s?t=6s" rel="nofollow noreferrer">https://youtu.be/3sqd_CZNq7s?t=6s</a></p>

<p>I'm building this in Adobe After Effects so using an existing IK solver isn't going to work.</p>

<p>Here's my visual setup for reference:</p>

<p><img src="https://i.sstatic.net/eQknI.png" alt="ik reference"></p>

<p>What I'm aiming for is that when the blue box is moved (target) joints 2 and 3 bend appropriately to maintain their length. Joint 1 is static and doesn't move.</p>

<p>To make this work, I need x,y coordinates to be calculated in real time. No need to render anything on screen—just the position values for each joint.</p>

<p>Here's my javascript so far:</p>

<pre><code>var angle1, angle2, angle3, theta1, theta2, theta3, targetSqrDist;

// joints
var joint1 = thisComp.layer("joint 1");
var joint2 = thisComp.layer("joint 2");
var joint3 = thisComp.layer("joint 3");
var joint4 = thisComp.layer("joint 4");

// segment lengths
var segment1Len = length(joint2.transform.position, joint1.transform.position);
var segment2Len = length(joint3.transform.position, joint2.transform.position);
var segment3Len = length(joint4.transform.position, joint3.transform.position);

// target position
var target = thisComp.layer("target");
var ix = target.transform.position[0] - joint1.transform.position[0];
var iy = target.transform.position[1] - joint1.transform.position[1];

// target square distance
targetSqrDist = ix * ix + iy * iy;


// Segment 1
angle1 = Math.max(-1, Math.min( 1, 
    (targetSqrDist + (segment1Len*segment1Len) - (segment2Len*segment2Len)) / (2 * segment1Len * Math.sqrt(targetSqrDist))
));

theta1 = Math.atan2(iy, ix) - Math.acos(angle1);

var s1X = joint1.transform.position[0] + segment1Len * Math.cos(theta1);
var s1Y = joint1.transform.position[1] + segment1Len * Math.sin(theta1);


// Segment 2
angle2 = Math.max(-1, Math.min( 1, 
    (targetSqrDist - (segment1Len*segment1Len) - (segment2Len*segment2Len)) / (2 * segment1Len * segment2Len)
));

theta2 = Math.acos(angle2);

var s2X = s1X + segment2Len * Math.cos(theta2 + theta1);
var s2Y = s1Y + segment2Len * Math.sin(theta2 + theta1);


// Segment 3
angle3 = Math.max(-1, Math.min( 1, 
    (targetSqrDist - (segment2Len*segment2Len) - (segment3Len*segment3Len)) / (2 * segment2Len * segment3Len)
));

theta3 = Math.acos(angle3);

var s3X = s2X + segment3Len * Math.cos(theta3 + theta2);
var s3Y = s2Y + segment3Len * Math.sin(theta3 + theta2);
</code></pre>

<p>This code would be applied to each joint with a unique position value assigned to it, depending on the joint's location.</p>

CC BY-SA 3.0
2017-02-28T11:49:07.603Z
To be honest, I didn't read the code. Can you first clarify your question?
StinkySkunk
CC BY-SA 3.0
2017-02-28T18:14:40.730Z
Sorry, things got muddled while writing. How do I find the position of all joints for an inverse kinematic chain that is longer than 2 segments?
Greg Gunn
CC BY-SA 3.0
2017-02-28T19:41:17.920Z
I think you can perhaps find explicit formulas here http://courses.csail.mit.edu/6.141/spring2011/pub/lectures/Lec14-Manipulation-II.pdf starting on slide 32. Tell me if it's not what you're looking for.
StinkySkunk
CC BY-SA 3.0
2017-03-01T02:18:30.270Z
It's close, but not exactly it. These formulas illustrate a typical 3 joint IK solve (e.g. arm, leg) with a single bend. I'm looking for the formula that factors in additional joints and bends in the IK solve (e.g. 4 joints with 2 bends).
Greg Gunn
<animation><javascript>
Inverse Kinematics with more than 2 segments
Greg Gunn
# Inverse Kinematics with more than 2 segments

(Asked by: username_0 on 2017-02-24)

I'm building a 2D inverse kinematics system and can't figure out how to find all the joint positions. I have the math to make a typical IK set up with a single bend (e.g. arm, leg), but I get stuck when adding another bend.

**How do I find the position of all joints for an inverse kinematic chain that is longer than 2 segments?**

Here's an example of the planar solution I'm looking for: <https://youtu.be/3sqd_CZNq7s?t=6s>

I'm building this in Adobe After Effects so using an existing IK solver isn't going to work.

Here's my visual setup for reference:

![ik reference](https://i.sstatic.net/eQknI.png)

What I'm aiming for is that when the blue box is moved (target) joints 2 and 3 bend appropriately to maintain their length. Joint 1 is static and doesn't move.

To make this work, I need x,y coordinates to be calculated in real time. No need to render anything on screen—just the position values for each joint.

Here's my javascript so far:

```
var angle1, angle2, angle3, theta1, theta2, theta3, targetSqrDist;

// joints
var joint1 = thisComp.layer("joint 1");
var joint2 = thisComp.layer("joint 2");
var joint3 = thisComp.layer("joint 3");
var joint4 = thisComp.layer("joint 4");

// segment lengths
var segment1Len = length(joint2.transform.position, joint1.transform.position);
var segment2Len = length(joint3.transform.position, joint2.transform.position);
var segment3Len = length(joint4.transform.position, joint3.transform.position);

// target position
var target = thisComp.layer("target");
var ix = target.transform.position[0] - joint1.transform.position[0];
var iy = target.transform.position[1] - joint1.transform.position[1];

// target square distance
targetSqrDist = ix * ix + iy * iy;

// Segment 1
angle1 = Math.max(-1, Math.min( 1, 
    (targetSqrDist + (segment1Len*segment1Len) - (segment2Len*segment2Len)) / (2 * segment1Len * Math.sqrt(targetSqrDist))
));

theta1 = Math.atan2(iy, ix) - Math.acos(angle1);

var s1X = joint1.transform.position[0] + segment1Len * Math.cos(theta1);
var s1Y = joint1.transform.position[1] + segment1Len * Math.sin(theta1);

// Segment 2
angle2 = Math.max(-1, Math.min( 1, 
    (targetSqrDist - (segment1Len*segment1Len) - (segment2Len*segment2Len)) / (2 * segment1Len * segment2Len)
));

theta2 = Math.acos(angle2);

var s2X = s1X + segment2Len * Math.cos(theta2 + theta1);
var s2Y = s1Y + segment2Len * Math.sin(theta2 + theta1);

// Segment 3
angle3 = Math.max(-1, Math.min( 1, 
    (targetSqrDist - (segment2Len*segment2Len) - (segment3Len*segment3Len)) / (2 * segment2Len * segment3Len)
));

theta3 = Math.acos(angle3);

var s3X = s2X + segment3Len * Math.cos(theta3 + theta2);
var s3Y = s2Y + segment3Len * Math.sin(theta3 + theta2);

```

This code would be applied to each joint with a unique position value assigned to it, depending on the joint's location.

**Tags:** animation, javascript

> **Comments:**
> **username_1**: To be honest, I didn't read the code. Can you first clarify your question?  
> **username_0**: Sorry, things got muddled while writing. How do I find the position of all joints for an inverse kinematic chain that is longer than 2 segments?  
> **username_1**: I think you can perhaps find explicit formulas here http://courses.csail.mit.edu/6.141/spring2011/pub/lectures/Lec14-Manipulation-II.pdf starting on slide 32. Tell me if it's not what you're looking for.  
> **username_0**: It's close, but not exactly it. These formulas illustrate a typical 3 joint IK solve (e.g. arm, leg) with a single bend. I'm looking for the formula that factors in additional joints and bends in the IK solve (e.g. 4 joints with 2 bends).  

---

## 1 answers

### Answer by username_2 on 2017-03-01. Score: 2

What you have here is an underconstrained problem\*. To solve this problem you do what you do to any other underconstrained system. This is also why you mostly only see the 2 joint version as it is not underconstrained in the planar solution, and in any case the preregistration is trivial.

Basically you have 2 options:

* Add more constraints
* Deal with the freedom

Adding more constraints to the system reduces the degrees of freedom. For example if you only permit one of the joints to rotate about its z axis and the another around its x axis then y then you have reduced the problem to the same 2 member problem. Another way to constrain it is to force the orientation of the last link. This also reduces it to the same case as 2 links. But there are other meaningful ways to reduce complexity that depend on your application.

[![enter image description here](https://i.sstatic.net/OoFDT.png)](https://i.sstatic.net/OoFDT.png)

**Image 1**: Constraining one member reduces the problem to a 2 member problem. In the planar case that means 2 solutions (other in blue).

Dealing with the freedom just means that your answer is going to be a family of answers. Then you develop a way to choose the solution from that family. Letting the user adjust the parameter is equivalent to adding one more constraint. But there are other methods such as least squares, or energy methods that yield different results. Sky is the limit here. For example I have worked with robot controllers that decided the parameter based on what was the least amount of rotation on joints. The problem was that this wasn't entirely deterministic so over time the robot ended up doing things differently...

\* This is more suited to Engineering.SE but since IK is a common graphics problem lets deal with it here.

> **Comments:**
> **username_1**: Do you mean underconstrained?  
> **username_2**: @username_1 oh yes  
> **username_0**: To be honest, I'm not sure which option I'm searching for. What I do know is that I'm after a planar solution that moves like this:   https://youtu.be/3sqd_CZNq7s?t=6s  It's short, but I'm looking at the part where the color chain is red-orange-yellow-blue.  
> **username_2**: That does not seem very useful @username_0 It does not even seem to solve any ik. Atleast we do not know if it is because we cant see the target.  It looks more like a system that moves the links based on proxility which is more like hbrid fk/ik. the only reason we even suspect its ik is because the title says so... Titles lie.  
> **username_0**: @username_2 If that reference is my goal—regardless of it's IK or not—how would you suggest I build something that moves like that?  
> **username_2**: @username_0 There is a inifinite mumber of solutions that does something. _You_ need to decide what is useful, i cant help you if there is no aim other than a abstract "something". You need to have a clear vision of what problem your ik is trying to solve. Onnce you can describe what the vide does with its 4th link and why thats useful then i can help. The trick to underconstrained problems is that you need to do the reasoning because the computer or anybody else who does not k.ow the purpose and specification can not do it for you.  

---

