# Controlled rotation on 3D object with mouse movement

I've a scene with a cube that I can rotate around yaw, pitch and roll. I do this by click the circles and drag the mouse on x/y-axis. I want similar functionality as the Unity rotation tool in editor mode.

My problem is that I can't figure out how to transform the mouse movement so the rotation doesn't become reversed if the cube is rotated 180 degrees in some axis. And how to use correct mouse direction depending on how the cube is rotated.

An image of the cube:

Here I get the mouse input

var direction = InputHandler.GetMouseDirection();
Vector3 diff = new Vector3(direction.X, direction.Y, 0);

if (selectedIntersect == Intersect.Yaw) //green
block.Rotate2(diff.X, 0, 0);

else if (selectedIntersect == Intersect.Pitch) //blue
block.Rotate2(0, 0, diff.Y);

else if (selectedIntersect == Intersect.Roll) //red
block.Rotate2(0, diff.Y, 0);


Here I rotate the cube:

public void Rotate2(float yaw, float pitch, float roll)
{
Quaternion yawrotation = Quaternion.CreateFromAxisAngle(
Quaternion pitchrotation = Quaternion.CreateFromAxisAngle(
Quaternion rollrotation = Quaternion.CreateFromAxisAngle(

Quaternion.Concatenate(ref orientation, ref yawrotation, out orientation);
Quaternion.Concatenate(ref orientation, ref pitchrotation, out orientation);
Quaternion.Concatenate(ref orientation, ref rollrotation, out orientation);
}

• As a general rule of thumb mouse up-down should rotate around camera x axis, and mouse left-right around camera y axis – d3dave May 3 '14 at 22:13
• i am having the exact same issue. When i rotate my cube 180 degree it starts doing opposite movements. Have you solved it ? If yes, then can post your solution here. thanks in advance. – Behroz Sikander Jun 21 '14 at 12:58

I study how Unity editor handles rotation with mouse movements. You can rotate by directly clicking on the object, but I didn't look too deeply to that rotation since it doesn't answer your question.

You can hold the circular handles to rotate on only one local axis.

It proceeds like this:

1. It creates a line from the projection of the point clicked on the handle (your mouse), and the projection of the center of the object (the pink dot)

When moving perpendicularly to that line, it rotates the object. Here, cyan is positive and red negative because...
2. The positive direction of the axis (here, we are rotating around Z axis) is pointing left depending of the projection of the center of the object (the pink dot).

When pointing right, the positive direction is up/right depending of the line angle, else positive is at down/left of the line.

You can easily get the direction of the axis by measuring the angle of it, which you can get by removing its Euler angles from the Euler angles of the camera.
This only works when you have the Y axis of the camera pointing up.

Another approach would be to use rays and planes (They are both your best friends !) and apply the delta rotation each time the mouse moves. This way, there's no ambiguity.