Hello.
I have an issue with my quternion based camera: when I move it only on X and Y axis fast it is also begin rotate around Z axis, but it shouldn`t.
I use sharpDX lib and my code is following:
public void RotateX(float _degree_angle)
{
if (cameraType == CamType.ThirdPerson)
{
_degree_angle = -_degree_angle;
}
qRotation = Quaternion.Multiply(Quaternion.RotationAxis(Vector3.UnitX, MathUtil.DegreesToRadians(_degree_angle)), qRotation);
UpdateViewMatrix();
}
public void RotateY(float _degree_angle)
{
if (cameraType == CamType.ThirdPerson)
{
_degree_angle = -_degree_angle;
}
qRotation = Quaternion.Multiply(Quaternion.RotationAxis(Vector3.UnitY, MathUtil.DegreesToRadians(_degree_angle)), qRotation);
UpdateViewMatrix();
}
private void UpdateViewMatrix()
{
qRotation = Quaternion.Normalize(qRotation);
if (CameraType == CamType.ThirdPerson)
{
ViewMatrix = Matrix.Translation(Vector3.Negate(position)) * Matrix.RotationQuaternion(qRotation);
position -= new Vector3(ViewMatrix.M13, ViewMatrix.M23, ViewMatrix.M33) * radius;
ViewMatrix = Matrix.LookAtLH(position, lookAt, new Vector3(ViewMatrix.M12, ViewMatrix.M22, ViewMatrix.M32));
}
else
{
ViewMatrix = Matrix.Translation(Vector3.Negate(position)) * Matrix.RotationQuaternion(qRotation);
}
}
I tried to change order of multiplying for x and y quaternion rotations like this:
qRotation = Quaternion.Multiply(Quaternion.RotationAxis(Vector3.UnitY, MathUtil.DegreesToRadians(_degree_angle)), qRotation);
qRotation = Quaternion.Multiply(qRotation, Quaternion.RotationAxis(Vector3.UnitX, MathUtil.DegreesToRadians(_degree_angle2)));
This helps - after that camera no longer rotate on Z-axis during fast moving on x-y axis, but if I move it on diagonal, if begins to rotate around all 3 axis, describing virtual 8 digit.
Could someone please point me the right way working with quaternions to stop camera rotation around Z-axis when I don`t need this?
This issue is driving me crazy.