Inverse Kinematics in 2D – Part 1

If you have been following this blog for a while, you might have noticed some recurring themes. Inverse Kinematics is definitely one them, and I have dedicated an entire series on how to apply it to robotic arms and tentacles. If you have not read them, do not fear: this new series will be self-contained, as it reviews the problem of Inverse Kinematics from a new perspective.

You can read the rest of this online course here:

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

Introduction

We are so used to interact with the world around us that is easy to underestimate how complex moving our hands and arms really is. In the academic literature, the task of controlling a robotic arm is known as inverse kinematics. Kinematics stands for movements, and inverse refers to the fact that we don’t usually control the arm itself. What we control are the motors that rotate each individual joint. Inverse kinematics is the task of deciding how to drive these motors to move the arm to a certain point of position. And in its general form, it is an exceptionally challenging task. To give a feeling for how challenging it is, you can think about games such as QWOPGIRP or even Lunar Lander, where you do not decide where to go, but which muscles (or thrusters) to activate.

The task of controlling moving actuators predates even the field of robotics. It should not come as a surprise that, throughout the centuries, mathematicians and engineers have been developing countless solutions. Most 3D modelling softwares and game engines (including Unity) come with a set of tools that allow the rigging of human-like and dog-like creatures. For all different setups (such as robotic arms, tails, tentacles, wings, …) no built-in solution is usually offered.

This is why in the previous series on Procedural Animations and Inverse Kinematics, I have introduced a very general and effective solution that works on potentially any setup. But such a power comes at a cost: efficiency. One of the main criticism that the series has received is that it was too time-consuming and expensive to be used on hundreds of characters at the same time. This is why I have decided to start a new series that is specifically focused on Inverse Kinematics for arms with two degrees of freedom. The technique you will discover in this tutorial is exceptionally efficient and can truly be run on dozens (if not hundreds!) of characters at the same time.

Inverse Kinematics

Let’s imagine a robotic arm with two segments and two joints, like the one seen in the diagram below. At the end of the arm, there is the end effector that we want to control. We do not have direct control on the position of the end effector; we can only rotate the joints. The problem of inverse kinematics is to find the best way to rotate the joints to move the end effector to the desired position.

The solution that is proposing in this new series will only work on robotic arms with two joints. In the academic literature is often said that these robotic arms have two degrees of freedom. The reason for this will be very clear by looking at the diagram below. A robotic arm with two degrees of freedom can be modelled as a triangle, which is one of the most studied geometrical figures in geometry.

Let’s start by formalising the problem a little bit more. The two joints, A and B (both in black), can be rotated by A (in blue) and B (in green) degrees, respectively. This cause the end effector to move to a position C.

📰 Ad Break

Internal Angles

We can use the three points A, B and C to construct a triangle with internal angles \alpha, \beta and \gamma, like the one below.

While these three angles are unknown, we know the length of all edges.

  • The segment \overline{AB} represents the arm and has length c;
  • The segment \overline{BC} represents the forearm and has length a;
  • The segment \overline{CA} represents the distance between the shoulder joint and the hand, and has length b.

Knowing the three sides of a triangle is enough to find all of its angles. This is possible thanks to the law of cosines, a generalisation of Pythagora’s theorem for triangles that are not necessarily at right angles.

The two angles that are needed to control the robotic arm are \alpha and \beta. Let’s start with \alpha, which can be calculated using the law of cosines on \alpha:

(1)   \begin{equation*}  a^2  = b^2 + c^2 - 2 bc \cos{\alpha} \end{equation*}

We can refector (1) to extract \cos\alpha:

    \[ \begin{split} \cos{\alpha} & =\frac{a^2-b^2-c^2}{-2bc} = \\ & =\boxed{\frac{b^2+c^2-a^2}{2bc}} \end{split} \]

What is now needed is to use the inverse cosine function \cos^{-1} (also known as arcosine) to find the \alpha:

(2)   \begin{equation*}  \alpha = \cos^{-1}{\left(\boxed{\frac{b^2+c^2-a^2}{2bc}}\right)} \end{equation*}

With the same procedure, we can apply the law of cosine once again to find \beta:

    \[b^2 = a^2 + c^2 - 2ac \cos{\beta}\]

    \[\cos{\beta}=\frac{a^2 + c^2 -b^2}{2ac}\]

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

❓ Show me the derivation for the law of cosines!

Joint Angles

Using the law of cosines we have calculated the values for \alpha and \beta, which are the internal angles of the triangle created by the robotic arm. However, the angles we really need are A (in blue) and B (in green).

 

Let’s start by calculating B, which is easier. It is obvious from the diagram above that \beta and B sum up to 180^{\circ} (equal to \pi radians). This means that:

(11)   \begin{equation*}  \begin{split} \beta + B = \pi \\ B = \pi - \beta \end{split} \end{equation*}

Calculating A is not much different. The only different here is that we need to take into account A' (in purple), which is the angle of the segment \overline{AC}. This can be calculated using the arcotangent function \tan^{-1}:

(12)   \begin{equation*}  A' = \tan^{-1}{\left(\frac{C_Y-A_Y}{C_X-A_X}\right)} \end{equation*}

Which leads to:

(13)   \begin{equation*}  A = \alpha + A' \end{equation*}

The sign of the angles A, A' and B is mostly arbitrary and depends on the way each joint move.

❓ Multiple solutions?

What’s Next…

This post concludes the introduction on the Mathematics of Inverse Kinematics for robotic arms with two degrees of freedom.

The next post will explore how we can use the equations derived to efficiently move a robotic arm in Unity.

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

FeatureStandardPremium
Inverse Kinematics
Multiple Solutions
Smooth Reaching
Test Scene
Test Animations
DownloadStandardPremium

Comments

17 responses to “Inverse Kinematics in 2D – Part 1”

  1. forofor avatar

    if only the shoulder point and hand point are known, how are you getting the elbow?

    1. That would be the point “B” in the diagrams. You can find it by rotating the segment “c” around “A” by “alpha” degrees!

  2. Jenny avatar

    Hi, I dont understand why A = α + A’

    1. Look at the diagram below “Joint angles”.
      It shows You can literally see that α = A – A’.
      (A’ is negative, so it is subtracted to get the positive angle).
      So: A = α + A’

  3. Christopher Groleau avatar
    Christopher Groleau

    Hello! I have a background mainly in art and animation and wanted to know where I could find more information on “procedural animation”? It seems like there is very little documentation on how this is done and this is one of maybe 3 in depth documents I can find on the concept. I don’t have a huge coding background and wanted to know if you would have any recommendations on where to start learning more about this from someone who seems to know what they are talking about? thanks!

    1. Hi!
      Not sure if you have seen it, but I have a much more gentle introduction to the topic!
      I hope it helps!
      https://www.alanzucconi.com/2017/04/17/procedural-animations/

  4. Hello, I have a question. What software did you use to create these nice geometry diagrams?

    1. Hi! I made them with Google Drawings!

  5. hello, I have a question. At the start you mention “The segment \overline{CA} represents the distance between the shoulder joint and the hand, and has length b.
    Knowing the three sides of a triangle is enough to find all of its angles”

    How do you know the length of b?

    1. Because both A (start point) and C (end point) are known, b is simply the distance between them!

      While that may vary, a and c do not, since the segments of the robotic arm are rigid!

  6. […] Part 1. Inverse Kinematics in 2D: The Mathematics […]

  7. Help!
    At calculation 12 you introduce 4 new terms (Ax, Ay, Cx, Cy) with no explanation.

    What are they?

    1. Hi!

      Both A and C are points, and so they both have X and Y coordinates.
      A = (Ax, Ay), and C = (Cx, Cy).

      I hope this makes it clear!

  8. […] Part 1. Inverse Kinematics in 2D: The Mathematics […]

  9. […] my blog, I tend to cover rather advanced topics, from the mathematics of inverse kinematics to atmospheric Rayleigh scattering. Making such complex subjects understandable to a larger […]

  10. Jonathan avatar
    Jonathan

    “the law of cosines, a generalisation of Pythagora’s theorem for triangles that are not necessarily right.” should read “not necessarily *at right angles*.”

Leave a Reply

Your email address will not be published. Required fields are marked *