# Rotating a Vector

## Recommended Posts

What would be the most straightforward way to rotate a Vector3, by either an euler angle or a quaternion?

I feel like there must be a simpler way than my ludicrous hack: Two invisible cubes, with the vector assigned to the child, the rotation assigned to the parent, and the difference taken between their positions in world space using .getAbsolutePosition().

##### Share on other sites

Hello,

Here is the key code

`/** ROTATION */var m = new BABYLON.Matrix();q.toRotationMatrix(m);	var v2 = BABYLON.Vector3.TransformCoordinates(sphere.position, m);`
##### Share on other sites

even shorter, say we want to turn around Y axis for 90°

`var matrix = BABYLON.Matrix.RotationAxis(BABYLON.Axis.Y, Math.PI / 2);var v2 = BABYLON.Vector3.TransformCoordinates(sphere.position, matrix);`
##### Share on other sites

Thank you both. So to broaden Temechon's suggestion for the next newbie like myself, a simple function taking a vector and a quaternion and returning the vector rotated would be as follows:

`function rotateVector(vect, quat) {    var matr = new BABYLON.Matrix();    quat.toRotationMatrix(matr);	    var rotatedvect = BABYLON.Vector3.TransformCoordinates(vect, matr);    return rotatedvect;}`

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.