# Simplest way to rotate vector by Euler angles

**URL:** <https://forum.babylonjs.com/t/simplest-way-to-rotate-vector-by-euler-angles/9389>\
**Category:** Questions\
**Created:** [March 23, 2020, 12:01am UTC](https://forum.babylonjs.com/t/simplest-way-to-rotate-vector-by-euler-angles/9389 "2020-03-23T00:01:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![earthendev](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/earthendev/32/34897_2.png) [@earthendev](https://forum.babylonjs.com/u/earthendev)\
**Post date:** [March 23, 2020, 12:01am UTC](https://forum.babylonjs.com/t/simplest-way-to-rotate-vector-by-euler-angles/9389/1 "2020-03-23T00:01:08Z")

</div>

Hi again!

So I’ve always wondered if there was a simpler way to rotate a vector by Euler angles (for example to make moveWithCollisions behave like Space.LOCAL) than the Rube Goldberg contraption I like to use:

```
const vec = new Vector3(0, 0, velocity);
const quat = Quaternion.FromEulerAngles(0, mesh.rotation.y, 0);
let mat = Matrix.Zero();
quat.toRotationMatrix(mat);
const newVec = Vector3.TransformCoordinates(vec, mat);
mesh.moveWithCollisions(newVec);

```

I do this because neither Vector3.rotateByEulerAngles nor Matrix.FromEulerAngles are a thing.

It works and is still pretty fast, but is there any simpler way I’ve simply overlooked? Of course you can tuck it in a function, but it still bothers me somehow. There must be a better way to go about it, right?

edit: and of course you can build the rotation matrix yourself, with sines and cosines (IIRC), but that’s not really simpler!

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [March 23, 2020, 1:00am UTC](https://forum.babylonjs.com/t/simplest-way-to-rotate-vector-by-euler-angles/9389/2 "2020-03-23T01:00:25Z")

</div>

A little shorter to use

```auto
const vec = new Vector3(0, 0, velocity);
const quat = Quaternion.FromEulerAngles(0, mesh.rotation.y, 0);
let vec2 = Vector3.Zero();
vec.rotateByQuaternionToRef(quat, vec2);

```

---

<div class="post-metadata">

**Author:** ![earthendev](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/earthendev/32/34897_2.png) [@earthendev](https://forum.babylonjs.com/u/earthendev)\
**Post date:** [March 23, 2020, 9:03am UTC](https://forum.babylonjs.com/t/simplest-way-to-rotate-vector-by-euler-angles/9389/3 "2020-03-23T09:03:25Z")

</div>

Thank you! I was so focused on matrices I didn’t realize the ~Quaternion did have the coveted FromEulerAngles method ;)~ edit: of course I meant the  
rotateByQuaternionToRef of course.

They really want us to use quaternions, huh? Time to stop procrastinating and finally learn the math behind them then.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [March 23, 2020, 1:24pm UTC](https://forum.babylonjs.com/t/simplest-way-to-rotate-vector-by-euler-angles/9389/4 "2020-03-23T13:24:23Z")

</div>

This may give you a start [Euler Angles and Quaternions - Babylon.js Documentation](https://doc.babylonjs.com/resources/rotation_conventions)

---

<div class="post-metadata">

**Author:** ![earthendev](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/earthendev/32/34897_2.png) [@earthendev](https://forum.babylonjs.com/u/earthendev)\
**Post date:** [March 23, 2020, 1:40pm UTC](https://forum.babylonjs.com/t/simplest-way-to-rotate-vector-by-euler-angles/9389/5 "2020-03-23T13:40:03Z")

</div>

Thank you! But I meant working with quaternions directly, like [this](https://www.youtube.com/watch?v=zjMuIxRvygQ). Maybe for a space game where you can move in all three dimensions that could be very useful.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [March 23, 2020, 1:55pm UTC](https://forum.babylonjs.com/t/simplest-way-to-rotate-vector-by-euler-angles/9389/6 "2020-03-23T13:55:42Z")

</div>

You can manipulate quaternions in BJS. Have you seen [Quaternion - Babylon.js Documentation](https://doc.babylonjs.com/api/classes/babylon.quaternion)

Just to emphasise something a little different between terminolgy in the video you gave the link to and BJS

In BJS (and [Wikipedia](https://en.wikipedia.org/wiki/Quaternion)) quaternion has the form a + x _i_ + y _j_ +z _k_ and not all quaternions are rotation quaternions.

A rotation quaternion should be a unit quaternion In the video it appears the term quaternion is restricted to one having a unit vector for its axis.

Here is an example with the quaternion randomized and normalized to produce a unit quaternion

[https://www.babylonjs-playground.com/#1ST43U#163](https://www.babylonjs-playground.com/#1ST43U#163)
