How to transform a Quaternion by a Matrix4x4?
Given a 4x4 matrix, and a Quaternion, what is the most efficient way to compute the matrix-transformed-Quaternion?
What I’m looking for is similar to, but not exactly the following. But I’d like to do it without all these extra operations, and no loss of data (in this example, rotation around the X axis (Vector3.right) could be lost.)
Quaternion TransformQ(Matrix4x4 matrixTransformation, Quaternion inputQ)
{
Vector3 transformedRight = matrixTransformation.MultiplyPoint3x4(inputQ * Vector3.right);
Quaternion finalQ = Quaternion.FromToRotation(Vector3.right, transformedRight);
return finalQ;
}
I would think this can be done by multiplying the Quaternion x,y,z,w components by the appropriate combination of matrix row/column values; I just don’t know the matrix math well enough.
Edit/Update: I finally found this question, which this one is similar to: http://answers.unity3d.com/questions/592099/how-to-muliply-a-quaternion-with-a-matrix4x4.html Alas, this answer yields a result that is a Matrix, and I'm looking for a quaternion.
I also found http://answers.unity3d.com/questions/402280/how-to-decompose-a-trs-matrix.html and am currently using the following base on it, but I'm not sure it's right: is it ok to just ignore the other elements of the transformation?
Quaternion matrixRotation = Quaternion.LookRotation(
matrix.GetColumn(2),
matrix.GetColumn(1) );
Quaternion outputQ = matrixRotation * inputQ;
Answer by derBroV · Jul 27, 2020 at 12:51 PM
The quaternion has the character that you can multiply another quaternion and it's rotated by that. So you can inoutQ *= matrixRotaion.rotation
. Correct me if I'm wrong.
I think this is correct, https://docs.unity3d.com/ScriptReference/$$anonymous$$atrix4x4-rotation.html was added in version 2017.2, which was not yet released when this question was asked. Will test to confirm this works as expected, and will mark as correct answer at that point.
The GetRotation method used by the rotation property most likely does something similar to what you have posted at the end of your question. Keep in $$anonymous$$d that you can not always extract a clear rotation from a matrix4x4 since it could represent a sheared space or a degenerated space. That's why rotation or the lossyscale extracted from a matrix might not be "correct".
The Transform class will most likely calculate the world space rotation seperately by just combining the quaternions of the hierarchy chain.
Finally be careful in which order you multiply quaternions since quaternion multiplication is not commutative. But I guess you know that ^^.
Your answer

Follow this Question
Related Questions
Acceleration and accelerometer calibration 0 Answers
Instantiate object in orientation of previously instantiated object 1 Answer
How to translate World <->Local position without Transform component with Matrix4x4? 0 Answers
Cant add Torque and Rotation to a rigidbody in the same frame? 2 Answers
Trying to MathF.Clamp y axis rotation results in x equal 270 and y equal max value instantly 1 Answer