A simple way to rotate a Vector3 with a Quaternion?

Explicitly converting to an Euler Matrix to then perform a rotation defined by a Quaternion seems kind of clunky. Am I missing something?

It would be nice, given a Quaternion quat and Vector3 vec to just be able to say

const rotatedVec = quat.rotate(vec);

ok, I missed that this works:

const rotatedVec = vec.applyRotationQuaternion(quat);

Conceptually and syntactically, the rotate function would be nice to have on the Quaternion, though I suppose that might lead to circular imports

2 Likes

I you want to still have it on your side, you can still do :

BABYLON.Quaternion.prototype.rotate = function(vec) {
    const rotatedVec = vec.applyRotationQuaternion(this);
    return rotatedVec;
};

or

BABYLON.Quaternion.prototype.rotate = function(vec) {
    vec.applyRotationQuaternionInPlace(this);
};

The first one is like on your example : const rotatedVec = quat.rotate(vec);
The second one would be for a direct usage : quat.rotate(vec); and vec end up rotated.

++
Tricotou

1 Like

Thanks for the response Tricotou,

I generally try not to mess around with the prototype prop of anything out of my control unless desperation mandates it :slight_smile: and in this case it doesn’t really feel necessary. But a very good point and handy trick indeed.

1 Like