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:

- Part 3. Inverse Kinematics in 3D

#### 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 QWOP, GIRP 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, and (both in black), can be rotated by (in blue) and (in green) degrees, respectively. This cause the end effector to move to a position .

### 📰 Ad Break

#### Internal Angles

We can use the three points , and to construct a triangle with internal angles , and , like the one below.

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

- The segment represents the
**arm**and has length ; - The segment represents the
**forearm**and has length ; - The segment represents the distance between the
**shoulder joint**and the hand, and has length .

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 and . Let’s start with , which can be calculated using the law of cosines on :

(1)

We can refector (1) to extract :

What is now needed is to use the inverse cosine function (also known as **arcosine**) to find the :

(2)

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

(3)

❓ Show me the derivation for the **law of cosines**!

The segment divides the triangle into two right triangles: and . This proof is a natural consequence of applying Pythagora’s theorem to both triangles:

(4)

(5)

Both (4) and (5) are now expressed in terms of . We can equate them to obtain:

[[+preamble]

\usepackage[makeroom]{cancel}

[/preamble]

\begin{split}

\boxed{a^2 -d^2} & = \boxed{c^2 – \left(b-d\right)^2} \

a^2 – d^2 &= c^2 – \left(b^2 + d^2 – 2 bd\right) \

a^2 – d^2 & = c^2 – b^2 – d^2 + 2 bd \

a^2 \xcancel{- d^2} & = c^2 – b^2 \xcancel{- d^2} + 2 bd\

a^2 & = c^2 – b^2 + 2 bd \

\end{split}

]

which can be rearrenged as:

(6)

Equation (6) is how the Law of cosines was transcribed in Euclid’s Proposition 12 from Book 2 of the Elements.

To get to the modern form of the equation, we need to apply some trigonometry. Since (5) is a right triangle, we can express the segment as:

(7)

where can be found by remembering that the angles of a triangle sums up to (or, equivalently, radians):

(8)

Substituting into (7) we obtain:

*** QuickLaTeX cannot compile formula: \begin{equation*} \begin{split}d & = a \sin{\delta}\d & = a \boxed{\sin{\left(\frac{\pi}{2} - \gamma\right)}}\d & = a \boxed{\cos{\gamma}}\end{split}\end{equation*} *** Error message: Argument of \OT1\d has an extra }. leading text: ...)}}\d & = a \boxed{\cos{\gamma}}\end{split} Paragraph ended before \OT1\d was complete. leading text: ...)}}\d & = a \boxed{\cos{\gamma}}\end{split} Missing $ inserted. leading text: ...)}}\d & = a \boxed{\cos{\gamma}}\end{split} Missing } inserted. leading text: ...)}}\d & = a \boxed{\cos{\gamma}}\end{split} Missing \cr inserted. leading text: ...)}}\d & = a \boxed{\cos{\gamma}}\end{split} Misplaced \cr. leading text: ...)}}\d & = a \boxed{\cos{\gamma}}\end{split} Missing \cr inserted. leading text: ...)}}\d & = a \boxed{\cos{\gamma}}\end{split} Misplaced \cr. leading text: ...)}}\d & = a \boxed{\cos{\gamma}}\end{split} Missing \cr inserted. leading text: ...)}}\d & = a \boxed{\cos{\gamma}}\end{split}

The last step is a consequence of the well-known **co-function identity** which states that:

\sin\left(\frac{\pi}{2} – \gamma\right) = \cos{\gamma}

]

We can now use (9) to replace in (6), which gives us the modern form of the **law of cosines**:

*** QuickLaTeX cannot compile formula: \begin{equation*} \begin{split}c^2&=a^2+b^2-2b\boxed{d} \c^2&=a^2+b^2-2b \boxed{a \cos{\gamma}} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split}\end{equation*} *** Error message: Missing $ inserted. leading text: ...} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split} Missing { inserted. leading text: ...} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split} Missing } inserted. leading text: ...} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split} Extra }, or forgotten $. leading text: ...} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split} Missing } inserted. leading text: ...} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split} Extra }, or forgotten $. leading text: ...} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split} Missing } inserted. leading text: ...} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split} Extra }, or forgotten $. leading text: ...} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split} Missing } inserted. leading text: ...} \c^2&=a^2+b^2-2ab \cos{\gamma}\end{split} Extra }, or forgotten $.

#### Joint Angles

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

Let’s start by calculating , which is easier. It is obvious from the diagram above that and sum up to (equal to radians). This means that:

(11)

Calculating is not much different. The only different here is that we need to take into account (in purple), which is the angle of the segment . This can be calculated using the **arcotangent** function :

(12)

Which leads to:

(13)

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

❓ Multiple solutions?

### ⭐ Recommended Unity Assets

Unity is free, but you can upgrade to **Unity Pro** or **Unity Plus** subscription plans to get more functionalities and training resources for your games.

#### 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:

- Part 3. Inverse Kinematics in 3D

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.

## Leave a Reply