3
\$\begingroup\$

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:A Rubik's 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(
        Vector3.Normalize(this.WorldRotation.Up), MathHelper.ToRadians(yaw));
    Quaternion pitchrotation = Quaternion.CreateFromAxisAngle(
        Vector3.Normalize(this.WorldRotation.Right), MathHelper.ToRadians(pitch));
    Quaternion rollrotation = Quaternion.CreateFromAxisAngle(
        Vector3.Normalize(this.WorldRotation.Forward), MathHelper.ToRadians(roll));

    Quaternion.Concatenate(ref orientation, ref yawrotation, out orientation);
    Quaternion.Concatenate(ref orientation, ref pitchrotation, out orientation);
    Quaternion.Concatenate(ref orientation, ref rollrotation, out orientation);
}
\$\endgroup\$
  • \$\begingroup\$ As a general rule of thumb mouse up-down should rotate around camera x axis, and mouse left-right around camera y axis \$\endgroup\$ – d3dave May 3 '14 at 22:13
  • \$\begingroup\$ 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. \$\endgroup\$ – Behroz Sikander Jun 21 '14 at 12:58
1
\$\begingroup\$

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)
    handle rotation
    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).
    determine direction
    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. enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.