Can i switch X Y Z in a quaternion?
Try : Quaternion rotation = new Quaternion(X,Z,Y, -W); //i had to swap Z and Y due to
I'm adapting my answer from this post since the one here was the older and likely more generic one.
It's probably best to consider this in the context of how you convert angle and axis to a quaternion. In Wikipedia you can read that you describe a rotation by an angle θ around an axis with unit direction vector (x,y,z) using
q = cos(θ/2) + sin(θ/2)(xi + yj + zk)
Your post only tells us Y ↦ Z, i.e. the old Y direction is the new Z direction. What about the other directions? You probably want to keep X ↦ X, but that still leaves us with two alternatives.
- Either you use Z ↦ Y. In that case you change between left-handed and right-handed coordinate system, and the conversion is essentially a reflection.
- Or you use Z ↦ −Y, then it's just a 90° rotation about the X axis. The handedness of the coordinate system remains the same.
Change of chirality
Considering the first case first. What does changing the coordinate system do to your angle and axis? Well, the axis coordinates experience the same coordinate swapping as your points, and the angle changes its sign. So you have
cos(−θ/2) + sin(−θ/2)(xi + zj + yk)
Compared to the above, the real part does not change (since cos(x)=cos(−x)) but the imaginary parts change their sign, in addition to the change in order. Generalizing from this, a quaternion a + bi + cj + dk
describing a rotation in the old coordinate system would be turned into a − bi − dj − ck
in the new coordinate system. Or into −a + bi + dj + ck
which is a different description of the same rotation (since it changes θ by 360° but θ/2 by 180°).
Preserved chirality
Compared to this, the second case of Z ↦ −Y maintains the sign of θ, so you only have to adjust the axis. The new Z coordinate is the old Y coordinate, and the new Y coordinate is the negated old Z coordinate. So a + bi + cj + dk
gets converted to a + bi − dj + ck
(or its negative). Note that this is just a multiplication of the quaternion by i
or −i
, depending on which side you multiply it. If you want to write this as a conjugation, you have θ=±45° so you get square roots in the quaternion that expresses the change of coordinate system.
Just swpping two axes in a quaternions? No this doesn't work because this flips the chirality. However if you flip the chirality and negate the quaternion's real part then you're back in the original chirality. In general form you can write this as
Q'(Q, i'j'k') = εi'j'k' Qw_w + Qi_i + Qj_j + Qk_k
where
is the totally antisymmetric tensor, known as the Levi-Cevita symbol.
This shouldn't be a surprise, as the i², j², k² rules of quaternions are defined also by the same totally antisymmetric tensor.
No, you cannot exchange y and z -- it will turn into a Left-Handed Coordinate system, if it was Right-Handed (and vice-versa).
You can, however, do the following substitution:
newX = oldZ
newY = oldX
newZ = oldY
I suspect that what you really want is a simple rotation about the x axis. If that's why you want to switch y and z, then you should instead apply a rotation of -90 degrees about the +x axis (assuming you have a Right-Handed coordinate system).