 Inverse Kinematics in 2D – Part 2 You can read the rest of this online course here:

Introduction

In the previous part of this series, we have discussed the problem of inverse kinematics for a robotic arm with two degrees of freedom; like the one in the diagram below. In such a scenario, the length of the robotic arms, and , is typically known. If the point we have to reach is , then the configuration becomes a triangle in which all sides are known. We have then derived the equations for the angles and , which controls the rotation of the robotic arms’ joints:

(1) (2) At a first glance, they might look rather intimidating; their geometrical interpretation, on the other hand, should be farily intuitive looking at the diagram above.

Creating the Robotic Arm

The first step to implement this solution is to create a robotic arm. The concept of “joints” is not something that Unity comes with. However, the parenting system offered by the engine can be exploited to create a hierarchy of components that will behave exactly like a robotic arm.

The idea is to use a GameObject for each joint, so that rotating its transform will cause the arm attached to it to rotate as well. Parenting the second joint to the first joint will cause them to rotate like seen in the first diagram.

The resulting hierarchy becomes:

• Root
• Joint A
• Bone A
• Joint B
• Bone B
• Hand

We can then add a script to the root object called SimpleIK, which will take care of rotating the joints to reach the desired target.

The equations derived in the previous part of this tutorial require knowing the length of the first two bones (called and , respectively). Since the length of the bones is not supposed to change, it can be calculated in the Start function. This, however, requires the arm to be in a good configuration when the game starts.

Rotating the Joints

Before showing the final version of the code, let’s start with a simplified one. If we translate equations (1) and (2) directly to code, we end up with something like this:

The mathematical functions and are called Mathf.Acos and Mathf.Atan2 in Unity. Also, the final angles are converted to degrees using Mathf.Rad2Deg, since the Transform component accepts degrees, instead of radians.

Aiming to Unreachable Targets

While the code above seems to work, there is a condition in which it fails. What happens if the target is unreachable? The current implementation does not take that into account, resulting in undesirable behaviours.

A common solution is to fully stretch the arm in the direction of the target. Such a behaviour is consistent with the reaching movement that we are trying to simulate.

The code below detects if the target is out of reach by checking if the distance from the root is greater than that the total length of the arm.

Rotating the Joints

What’s left now is to rotate the joints. This can be done accessing the localEulerAngles property of the joints’ Transform component. Unfortunately, it is not possible to change the z angle directly, so the vector needs to be copied, edited and replaced.

Conclusion

This post concludes the course on Inverse Kinematics for 2D robotics arms.

You can read the rest of this online course here:

The line art animals that have been featured in this tutorials have been inspired by the work of WithOneLine.

You can download all the assets used in this tutorial to have a fully functional robotic arm for Unity.

 Feature Standard Premium Inverse Kinematics ✅ ✅ Multiple Solutions ❌ ✅ Smooth Reaching ❌ ✅ Test Scene ❌ ✅ Test Animations ❌ ✅ Download Standard Premium A new tutorial is released every week.

💖 Support this blog

This websites exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.  1. Steven De Keninck

Hi,

Nice article. Have you ever looked into Geometric Algebra ? It’s coordinate free approach allows for a very elegant coordinate-free formulation of these types of problems. (and generalizes without changes to higher dimensions)

My geometric algebra library comes with a 2D inverse kinematics example. The IK solver is six lines and does not reference any coordinates. If you like take a look on github.com/ganja.js, then examples – 2D PGA IK. (wordpress wont let me link it 😉 )

Cheers,

Steven

• Alan Zucconi

Thank you, that’s interesting!
I’ve been wanting to add more interactive examples to my website, but is hard to find good (and simple) frameworks to use!

2. Steven De Keninck

*cough* that’s github.com/enkimute/ganja.js .. mea culpa.

3. ET

Would be great if you could provide a bare-minimum project sample and not assume that everyone has the funds to subscribe to Patreon. Nonetheless, your content is very helpful for someone to get the basic idea.

4. Dmitri

how you drawing this line in GIF in Unity ?

• Alan Zucconi

It’s a trail renderer attached to the end effector of the robotic arm!