In one of my previous blog posts, I showed you how to measure joint angles using Kinect and C#. Today, we’ll dive into a more complex topic: in this article, you are going to learn how to measure the orientation of a joint around each axis (X, Y, Z).

Measuring the orientation values isÂ not trivial because it requires some good knowledge of Mathematics.Â Do not be afraid, though! After reading this article, you’ll be able to calculate the orientation of each joint using one line of C# code!

Sounds good? Let’s get started.

## Prerequisites

To run the code and samples provided in this guide, you’ll need the following:

## Let’s do the Math…

Kinect is reading the joint orientation values as a quaternion. A quaternion is a set of 4 values: X, Y, Z, and W.

The Kinect SDK is encapsulating the quaternion into a structure calledÂ **Vector4**. We need to transform this quaternion (Vector4) into a set of 3 numeric values.

Using the Orientation quaternion, we canÂ calculate the rotation of the joint around the X, Y, and Z axis.

### Pitch: rotating around the X-axis

The rotation around the X axis is called **Pitch**. Here is how to measure it:

<code class="language-csharp">public static double Pitch(this Vector4 quaternion) { double value1 = 2.0 * (quaternion.W * quaternion.X + quaternion.Y * quaternion.Z); double value2 = 1.0 - 2.0 * (quaternion.X * quaternion.X + quaternion.Y * quaternion.Y); double roll = Math.Atan2(value1, value2); return roll * (180.0 / Math.PI); }</code>

### Yaw: rotating around the Y-axis

The rotation around the Y axis is called **Yaw**. Here is how to measure it:

<code class="language-csharp">public static double Yaw(this Vector4 quaternion) { double value = 2.0 * (quaternion.W * quaternion.Y - quaternion.Z * quaternion.X); value = value > 1.0 ? 1.0 : value; value = value < -1.0 ? -1.0 : value; double pitch = Math.Asin(value); return pitch * (180.0 / Math.PI); }</code>

### Roll: rotating around the Z-axis

The rotation around the Z axis is called **Roll**. Here is how to measure it:

<code class="language-csharp">public static double Roll(this Vector4 quaternion) { double value1 = 2.0 * (quaternion.W * quaternion.Z + quaternion.X * quaternion.Y); double value2 = 1.0 - 2.0 * (quaternion.Y * quaternion.Y + quaternion.Z * quaternion.Z); double yaw = Math.Atan2(value1, value2); return yaw * (180.0 / Math.PI); }</code>

## Using the code

Here is the complete code. All you have to do is import the following C# file into your Kinect project.

<code class="language-csharp">using System; using Microsoft.Kinect; namespace LightBuzz.Vitruvius { public static class JointOrientationExtensions { public static double Pitch(this Vector4 quaternion) { double value1 = 2.0 * (quaternion.W * quaternion.X + quaternion.Y * quaternion.Z); double value2 = 1.0 - 2.0 * (quaternion.X * quaternion.X + quaternion.Y * quaternion.Y); double roll = Math.Atan2(value1, value2); return roll * (180.0 / Math.PI); } public static double Yaw(this Vector4 quaternion) { double value = 2.0 * (quaternion.W * quaternion.Y - quaternion.Z * quaternion.X); value = value > 1.0 ? 1.0 : value; value = value < -1.0 ? -1.0 : value; double pitch = Math.Asin(value); return pitch * (180.0 / Math.PI); } public static double Roll(this Vector4 quaternion) { double value1 = 2.0 * (quaternion.W * quaternion.Z + quaternion.X * quaternion.Y); double value2 = 1.0 - 2.0 * (quaternion.Y * quaternion.Y + quaternion.Z * quaternion.Z); double yaw = Math.Atan2(value1, value2); return yaw * (180.0 / Math.PI); } } }</code>

Then, in your main C# file, import the following namespace:

<code class="language-csharp">using LightBuzz.Vitruvius;</code>

And, finally, specify the joint you would like to measure the orientation for and call the Roll, Pitch, and Yaw methods:

<code class="language-csharp">var orientation = body.JointOrientations[JointType.ElbowLeft].Orientation; var rotationX = orientation.Pitch(); var rotationY = orientation.Yaw(); var rotationZ = orientation.Roll();</code>

## Supported joints

Measuring the rotation of the human body joints is not a trivial job. Unfortunately, Kinect does not provide us with Orientation values for the Head, Hands, and Feet. These joints do not have a “parent” joint, so it’s extremely difficult to accurately measure their orientation. The orientation accuracy regarding the rest of the joints is pretty good. The supported joints are the following:

- Neck
- SpineShoulder
- SpineBase
- ShoulderLeft/ShoulderRight
- ElbowLeft/ElbowRight
- WristLeft/WristRight
- HipLeft/HipRight
- KneeLeft/KneeRight

*The method described in this post will help you in most use-case scenarios, such as simple games. In case you would like to have increased accuracy (e.g. healthcare apps), you’ll need to use more complex algorithms, which involve a lot of custom coding.Â LightBuzz has a lot of experience with this kind of algorithms and could help you with your project.*

## Summary

In this blog post, you’ve learnt how to easily measure the rotation of the human body joints around the X, Y, and Z axis.