Set orientation from 3x3 rotation matrix
I have been trying to reproduce scenes on Unity from another software called MuJoCo. I have been stuck on setting the camera angle for a week now. Any help and feedback are very appreciated!
In MuJoCo, I have obtained the frame orientations of the camera that are stored as 3-by-3 matrices. I want to use that matrix and apply to the camera in Unity. So I tried to calculate the quaternion and apply to Unity. The results are not good.
What I did: I followed the equations in this page link text and created a function. Then I created a new quaternion and assigned the quaternion to localRotation of the camera.
thecamera = root.transform.GetChild(i).gameObject.GetComponent<Camera>();
thecamera.transform.localRotation = Mat2Quat(fcamort);
Quaternion Mat2Quat(float[] mat)
{
float qw = Mathf.Sqrt(1.0f + mat[0] + mat[4] + mat[8]) / 2.0f;
float qx = (mat[7] - mat[5]) / (4.0f * qw);
float qy = (mat[2] - mat[6]) / (4.0f * qw);
float qz= (mat[3] - mat[1]) / (4.0f * qw);
Quaternion quat = new Quaternion(qx, qz, qy, qw);
return quat;
}
I suspect what I did wrong was the convention on both softwares are different. In MuJoCo, they used right-handed coordinate system, with x pointing to right, y pointing forward and z upward. The camera is looking along the -Z axis of its frame. I have attached a photo to explain this.
Your answer
Follow this Question
Related Questions
[Solved] Quaternion from IMU sensor to GameObject Orientation problem 3 Answers
Quaternion from transition matrix 0 Answers
Maintain fixed vision while the player turn 0 Answers
Rotating a forward vector 0 Answers
Rotating 3d person character 0 Answers