# Using Quaternions to rotate(3D)

**URL:** <https://discourse.processing.org/t/using-quaternions-to-rotate-3d/7624>\
**Category:** Project Guidance\
**Created:** [January 18, 2019, 5:15pm UTC](https://discourse.processing.org/t/using-quaternions-to-rotate-3d/7624 "2019-01-18T17:15:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aryakeyvan](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@aryakeyvan](https://discourse.processing.org/u/aryakeyvan)\
**Post date:** [January 18, 2019, 5:15pm UTC](https://discourse.processing.org/t/using-quaternions-to-rotate-3d/7624/1 "2019-01-18T17:15:48Z")

</div>

In my project I have used a complimentary filter to generate yaw, roll, and pitch angles. I used the rotateX, rotateY, and rotateZ functions to rotate a box in 3d. I ran into problems like gimbal lock when using this method.

Now I’m using a quaternion based complimentary filter to generate quaternions that represent the orientation of the box. My question is, now that I have the quaternion representing my estimated orientation, how can I implement it to generate rotations? Are there any functions that I can use to do this?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 18, 2019, 5:46pm UTC](https://discourse.processing.org/t/using-quaternions-to-rotate-3d/7624/2 "2019-01-18T17:46:38Z")

</div>

You might use the Apache Math Commons library. It has the rotation class which internally uses a quaternion implementation but provides simple constructors to create Rotations that can then be applied to vectors. It uses its own immutable 3D vector class with the x,y,z attributes stored as doubles rather than floats. The PeasyCam library uses it and in Shapes3D I created a simple port using the PVector class.

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [January 18, 2019, 6:29pm UTC](https://discourse.processing.org/t/using-quaternions-to-rotate-3d/7624/3 "2019-01-18T18:29:36Z")

</div>

Convert the quaternion to a rotation matrix (see [https://en.wikipedia.org/wiki/Quaternions\_and\_spatial\_rotation](https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation)) and use it with applyMatrix().

---

<div class="post-metadata">

**Author:** ![aryakeyvan](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@aryakeyvan](https://discourse.processing.org/u/aryakeyvan)\
**Post date:** [January 18, 2019, 6:35pm UTC](https://discourse.processing.org/t/using-quaternions-to-rotate-3d/7624/4 "2019-01-18T18:35:29Z")

</div>

Converting a quaternion to a rotation matrix would be computationally expensive. Are there any functions for quaternions?

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [January 19, 2019, 11:23am UTC](https://discourse.processing.org/t/using-quaternions-to-rotate-3d/7624/5 "2019-01-19T11:23:28Z")

</div>

For my ruby-processing projects, I have created [arc ball functionality](https://github.com/ruby-processing/ArcBall/blob/master/src/monkstone/arcball/Arcball.java) using quaternions, after code by Ariel Malka. As I understand it use of quaternions does not guard against gimbal lock (unless properly implemented), but I have never experienced with my Arcball implementation.

---

<div class="post-metadata">

**Author:** ![f42ter](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f42ter/32/16793_2.png) [@f42ter](https://discourse.processing.org/u/f42ter)\
**Post date:** [January 20, 2019, 12:03pm UTC](https://discourse.processing.org/t/using-quaternions-to-rotate-3d/7624/6 "2019-01-20T12:03:52Z")

</div>

You may find a good introduction and same sample code for quaternions here  
[http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/index.htm](http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/index.htm)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 22, 2019, 1:24am UTC](https://discourse.processing.org/t/using-quaternions-to-rotate-3d/7624/7 "2019-01-22T01:24:21Z")

</div>

> [@aryakeyvan](#):
>
> now that I have the quaternion representing my estimated orientation, how can I implement it to generate rotations?

The PeasyCam processing library for Processing is implemented using quartonions, I believe. Consulting the code might be useful.

- [peasycam](http://mrfeinberg.com/peasycam)
- [GitHub - jdf/peasycam: Dead-simple mouse-driven camera for Processing](https://github.com/jdf/peasycam)

See class Rotation

> <https://github.com/jdf/peasycam/blob/master/src/peasy/org/apache/commons/math/geometry/Rotation.java#L20>
