I am implementing a mouse rotation which works by accumulating the X-Y delta between frames to yaw and pitch rotation angles. The problem is that I wish rotations to be independent of each other. But as you can see from the picture:
I wish it behaved like this instead:
In the second picture, instead of dragging, I am releasing the mouse button and performing a second drag movement. So the local Y-axis is not rotated as in the first picture.
This is the code I am using:
Vector2 delta = current - lastPointerPosition;
totalYaw += delta.X;
totalPitch += delta.Y;
var r = Quaternion.RotationAxis(Vector3.UnitY, totalYaw / 100f);
var s = Quaternion.RotationAxis(Vector3.UnitX, totalPitch / 100f);
var qOrientation = s*r*qStart;
Please ignore for the moment the crude division by 100, I wanted to get the rotation right before optimising that part. That code causes the behaviour in the first picture.
I tried to rotate the UnitX axis used in the second quaternion by doing var q = r*new Quaternion(Vector3.UnitX, 0)*Quaternion.Invert(r);
and then using q.Axis
in place of UnitX
in s
.
I have looked all over the web but so far none of the solutions seemed to work for me. What am I doing wrong?
EDIT every time the mouse moves, I am updating lastPointerPosition with the current one (after updating the rotation matrix); when the mouse pointer is released, totalYaw/totalPitch are restored to 0.
lastPointerPosition
only changes when you press the mouse button again? \$\endgroup\$