in C#, Maths, Tutorial, Unity

Inverse Kinematics in 2D – Part 2

You can read the rest of this online course here:

A follow-up that focuses on 3D is also available:

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, c and a, is typically known. If the point we have to reach is C, then the configuration becomes a triangle in which all sides are known.

We have then derived the equations for the angles A and B, which controls the rotation of the robotic arms’ joints:

(1)   \begin{equation*}  A = \underset{\alpha}{\underbrace{\cos^{-1}{\left(\frac{b^2+c^2-a^2}{2bc}\right)}}} + \underset{A'}{\underbrace{\tan^{-1}{\left(\frac{C_Y-A_Y}{C_X-A_X}\right)}}} \end{equation*}

(2)   \begin{equation*}  B = \pi - \underset{\beta}{\underbrace{\cos^{-1}{\left(\frac{a^2 + c^2 -b^2}{2ac}\right)}}} \end{equation*}

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 c and a, 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 cos^{-1} and tan^{-1} 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:

A follow-up that focuses on 3D is also available:

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

Download

Become a Patron!
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

💖 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.

Twitter_logo

YouTube_logo
📧 Stay updated

You will be notified when a new tutorial is relesed!

📝 Licensing

You are free to use, adapt and build upon this tutorial for your own projects (even commercially) as long as you credit me.

You are not allowed to redistribute the content of this tutorial on other platforms. Especially the parts that are only available on Patreon.

If the knowledge you have gained had a significant impact on your project, a mention in the credit would be very appreciated. ❤️🧔🏻

Write a Comment

Comment

  1. 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

    • 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. 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.

  3. I think I get the Mathematic part, but I can’t get the code to work.
    I don’t get any errors but the script won’t work in Unity. Also the structure of the downloaded script is totally different then in the tutorial. This makes it very hard for me to bugfix the code.
    Previous tutorials struggled with the same problem. Maybe it’s my lack of C#, but even with the assets I can’t figure these tutorials out.

Webmentions

  • Inverse Kinematics in 2D - Part 1 - Alan Zucconi October 15, 2020

    I think I get the Mathematic part, but I can’t get the code to work.
    I don’t get any errors but the script won’t work in Unity. Also the structure of the downloaded script is totally different then in the tutorial. This makes it very hard for me to bugfix the code.
    Previous tutorials struggled with the same problem. Maybe it’s my lack of C#, but even with the assets I can’t figure these tutorials out.