# How To Multiply A Quaternion With A Vector3

Yo @Deltakosh and @sebavan … In Unity… I guess you can just multiply a quaternion and a vector3

Example Unity Code:

``````Vector3 wantedPosition = target.position;
wantedPosition.y = currentHeight;

wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
``````

How the heck would port this line of code when wantedPosition is a Vector3 and its trying to add the current rotation angle expressed as a Quaternion plus the new usedDistance as a Vector3

``````wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
``````

Hey,
Maybe you can convert the Quaternion to Vector3 first, and after this 2 vectors are easy to multiply.
https://doc.babylonjs.com/api/classes/babylon.quaternion#toeulerangles

You can also look at other implementations:

You can turn the quaternion to a matrix Matrix - Babylon.js Documentation and the apply the matrix to the vector with Vector3 - Babylon.js Documentation

1 Like

Yo @Raggar … Thank a bunch… You too @JohnK

I think i am going to try and use this in my Utilities

``````        /** Multiply the quaternion by a vector */
public static MultiplyQuaternionByVector(quaternion:BABYLON.Quaternion, vector:BABYLON.Vector3): BABYLON.Vector3 {
const result:BABYLON.Vector3 = new BABYLON.Vector3(0,0,0);
BABYLON.Utilities.MultiplyQuaternionByVectorToRef(quaternion, vector, result);
return result;
}
/** Multiply the quaternion by a vector to result */
public static MultiplyQuaternionByVectorToRef(quaternion:BABYLON.Quaternion, vector:BABYLON.Vector3, result:BABYLON.Vector3) : void {
// Vector
const x:number = vector.x;
const y:number = vector.y;
const z:number = vector.z;
// Quaternion
const qx:number = quaternion.x;
const qy:number = quaternion.y;
const qz:number = quaternion.z;
const qw:number = quaternion.w;
// Quaternion * Vector
const ix:number =  qw * x + qy * z - qz * y;
const iy:number =  qw * y + qz * x - qx * z;
const iz:number =  qw * z + qx * y - qy * x;
const iw:number = -qx * x - qy * y - qz * z;
// Final Quaternion * Vector = Result
result.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
result.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
result.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
}

``````

We shall see how that works… Ill let you know

1 Like

That worked

1 Like