I am trying to write code with rotates an object.
I implemented it as:
Rotation about X-axis is given by the amount of change in y coordinates of a mouse and Rotation about Y-axis is given by the amount of change in x coordinates of a mouse.
This method is simple and work fine until on the the axis coincides with Z-axis, in short a gimble lock occurs.
How can I utilize the rotation arount Z-axis to avoid gimbal lock.
Answer
The simple solution is not to store the orientation of the object as angles around axes (X-, Y-, Z-axis), as for instance in euler angles.
Store the orientation of the object as a matrix or a quaternion.
This can cause gimbal lock, using euler angles:
class Object
{
float m_angleAxisX;
float m_angleAxisY;
float m_angleAxisZ;
};
No gimbal lock:
class Object
{
matrix m_orientation;
};
No gimbal lock either:
class Object
{
quaternion m_orientation;
};
Now whenever the mouse is changed, multiply m_orientation with the orientation change coming from the mouse movement each frame.
No comments:
Post a Comment