Reputation: 7640
I am using CesiumJs
I have a Quaternion (x,y,z,w) I have a Vector (x,y,z)
I want to multiply that Quaternion by a Vector, basically at the moment I hjave a rotation, and I want to multiply that rotation with a Vector forward (0,0,1) in order to get a point in a direction, but CesiumJS do not have those function at the moment.
I know there is some way to multiply Vector with Quaternion, I was doing it in Unity, but I do not know exactly how
I found this formula v' = q * v * (q^-1)
but what exactly is (q^-1)
?
Upvotes: 3
Views: 10207
Reputation: 12448
Marco13's comment is correct. in Cesium, Quaternions are typically converted to matrices prior to multiplication with vectors, and built-in functions exist for this purpose.
Here's a Sandcastle demo.
var position = new Cesium.Cartesian3(0, 0, 1); // x, y, z
// Could instead use:
// var position = Cesium.Cartesian3.UNIT_Z.clone();
var quaternion = new Cesium.Quaternion(0.382683, 0, 0, 0.92388); // x, y, z, w
// Could alternately use: 45 degrees (pi/4 radians) around X
//var quaternion = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_X, Math.PI / 4);
var matrix = Cesium.Matrix3.fromQuaternion(quaternion);
var scratch1 = new Cesium.Cartesian3();
var result = Cesium.Matrix3.multiplyByVector(matrix, position, scratch1);
console.log(result);
// Result will be: (0, -0.7071063400800001, 0.7071079759110002)
// The +Z vector was rotated 45 degrees towards -Y, in a counter-clockwise
// rotation as seen from the +X axis.
Upvotes: 1
Reputation: 1507
You can "vectorize" the quaternion product i.e., writing it in terms of vector operations such as the dot product and the cross product. Doing that you will get a formula equivalent to the Euler-Rodrigues Formula [1]:
v' = v + 2 * r x (s * v + r x v) / m
where x represents the cross product, s and r are the scalar and vector parts of the quaternion, respectively, and m is the sum of the squares of the components of the quaternion.
[1] https://en.m.wikipedia.org/wiki/Euler%E2%80%93Rodrigues_formula
Upvotes: 4