Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#
Tip/Trick

How to Convert Right-Handed to Left-Handed Coordinates and Quaternions In Unity

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
19 Apr 2018CPOL 33.7K   1   3
Formulas to Convert Right-Handed Vectors and Quaternions to Left-Handed Equivalents

Converting Coordinates and Vectors

To convert a right-handed vector (assuming that z is up) to left-handed vector (Unity coordinate system), simply swap y and z coordinates.

The following is a method that performs this conversion:

C#
private Vector3 ConvertRightHandedToLeftHandedVector (Vector3 rightHandedVector)
{      
    return new Vector3(rightHandedVector.x, rightHandedVector.z, rightHandedVector.y);
}

Converting Quaternions

To convert a right-handed quaternion to left-handed quaternion:

  1. Negate x
  2. Swap y and z and negate both

The following is a method that performs this conversion:

C#
private Quaternion ConvertRightHandedToLeftHandedQuaternion (Quaternion rightHandedQuaternion)
{
    return new Quaternion (- rightHandedQuaternion.x,
                           - rightHandedQuaternion.z,
                           - rightHandedQuaternion.y,
                             rightHandedQuaternion.w);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Canada Canada
I develop virtual reality applications.

Comments and Discussions

 
QuestionDon't think so Pin
Member 1349605529-May-20 1:22
Member 1349605529-May-20 1:22 
QuestionAnd what about Left-Handed coordinates to Right handed oposites Pin
ronconsoda28-Sep-19 9:21
ronconsoda28-Sep-19 9:21 
QuestionLeft-handed (Unity) to Right-handed (Universal Robots) Pin
nielsson933-Jun-18 8:02
nielsson933-Jun-18 8:02 
Hello,

First of all thanks for the great the example!

I'm despairing about converting Quaternions to Axis-Angle.
Following situation:
I have developed a kinematized 3D model of a Universal Robot in Unity and would like to query the orientation of the Tool Center Point and transfer it to the real robot.
Unity uses a left-handed coordinate system (Y points upward, Z points to the right, and X toward the viewer) (positive rotation is clockwise).
Universal Robots uses a right-handed coordinate system (Z points up, y points to the right, and X points toward the viewer) (positive rotation is counterclockwise).

In Unity, I can query quaternions. The real robot can process Axis-angle representation. Definition of Universal Robots: "The length of the axis is the angle to be rotated in radians, and the vector itself gives the axis about which to rotate."

Therefore, I would like to convert the quaternions from Unity to Axis angle with the following formula:

qx = -qx_Unity
qy = -qz_Unity (switched)
qz = -qy_Unity (switched)
qw = qw_Unity

And then continue with:

angle = 2 * acos (qw)
x = qx / sqrt (1-qw * qw)
y = qy / sqrt (1-qw * qw)
z = qz / sqrt (1-qw * qw)

For the UR:
x_UR = x * angle
y_UR = y * angle
z_UR = z * angle

But I'm not sure if that's right. Especially when converting from left to right handed. Can I simply transfer your example?
Thanks for any help and comments!

Best regards
Niels

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.