• 10
• 12
• 12
• 14
• 15

# Rotation tool user experience like in unity/maya

## Recommended Posts

Hi,

I am trying to implement rotation tool in my own engine, which will have same user experience as rotation tool in unity or maya.

When user rotate object by tool, it also rotates, but its axis visually does not rotate on the back side of the tool - they are always near camera view +/-90 on the X/Y/Z axes.

I used this code to achieve rotation to face for each of the axes:

// objectPosition is your object's position
// objectToFacePosition is the position of the object to face
// upVector is the nominal "up" vector (typically Vector3.Y)
// Note: this does not work when objectPosition is straight below or straight above objectToFacePosition
QuaternionF RotateToFace(Vector3F objectPosition, Vector3F objectToFacePosition, Vector3F upVector)
{
Vector3F D = (objectPosition - objectToFacePosition);
Vector3F right = Vector3F.Normalize(Vector3F.Cross(upVector, D));
Vector3F backward = Vector3F.Normalize(Vector3F.Cross(right, upVector));
Vector3F up = Vector3F.Cross(backward, right);
Matrix4x4F rotationMatrix = new Matrix4x4F(right.X, right.Y, right.Z, 0, up.X, up.Y, up.Z, 0, backward.X, backward.Y, backward.Z, 0, 0, 0, 0, 1);
QuaternionF orientation;
QuaternionF.RotationMatrix(ref rotationMatrix, out orientation);
return orientation;
}

And then I just apply resulting rotateToFaceQuaternion for each axis.

If I apply rotateToFaceQuaternion, axis always move towards to the camera (which is correct), but when I try to rotate the whole tool by some quaternion, it will not rotate correctly: current_rotation * rotateToFaceQuaternion = incorrect result, because after applying this rotation to the axis of the tool, they will rotate to the other side of the tool (which is not correct).

My question is: how to rotate axis of the tool in such way, that they always rotates towards to the camera, but also could be rotated +/- 90 degrees and if current axis reaches edge of the tool from one side, it will appear on the other side immediately (like in unity or maya) i. e. not to allow axis to appear on the other side of the tool.