handmade.network » Forums » Help: matrix-to-quaternion in left-hand coordinate system
Chen96
Chen
21 posts
#12725 Help: matrix-to-quaternion in left-hand coordinate system
2 months, 2 weeks ago

Hi all, I need help on converting a pure rotation matrix to quaternion for my animation system in left-hand coordinate system. I've been trying out matrix-to-quaternion equation from this gamasutra post. In the post, the author stated that the conversion code snippet that he provided has the assumption that the rotation matrix is in right-hand system. However matrices in my game are always in left-handed coordinate system. I copied the code snippet anyway, and as expected, it wasn't doing the right thing.

It didn't make sense why handedness would make a difference, so I did some testing on the code. I convert rotation matrices generated with Euler angles to quaternions using the code snippet from the gamasutra post. Then I convert the same Euler angles to quaternions using an online tool. The quaternion generated in my code and the quaternion generated by the online tool always have different signs, but euler angles they map to are the same. So it seems like their final orientation is the same, but the path the rotation takes are opposite. I have no idea how to fix this.

I later stumble across this stackoverflow post. In the post, one guy commented "Matrix-to-quaternion has to make a choice that stays always on the same half-sphere of unit quaternions". I feel like that is the key to solve my problem, but I don't know how because I have no idea how that snippet code works or how to always "stay in the same half-sphere" :(

So my question is, why was the code snippet in that gamasutra post "handedness sensitive", how it's derived, and what changes should be made to that code so that the converted quaternion always "stays in the same half-sphere" in a left-handed system? Thanks a lot!

Alex