44

I would like to change a 4x4 matrix from a right handed system where:
x is left and right, y is front and back and z is up and down

to a left-handed system where:
x is left and right, z is front and back and y is up and down.

For a vector it's easy, just swap the y and z values, but how do you do it for a matrix? enter image description here

5
  • 1
    I may be confused here, why can't you just swap the y and z values? And I assume you mean a 4x4x4 matrix, since a 4x4 wouldn't have z.
    – derobert
    Commented Aug 11, 2009 at 21:41
  • Just a 4x4 matrix. Just flipping the y and z translation values doesn't seem to work and a matrix also contains an operation for rotation so I'm asuming the problem is there.
    – cmann
    Commented Aug 11, 2009 at 21:44
  • I think you'll need to clarify exactly what you want before anybody can answer your question. For a vector (x,y,z,w), you've explained that to "change from right-handed to left-handed" means that you change it to the vector (x,z,y,w), but it is not at all clear what that phrase means for a matrix. For example, suppose a matrix takes the vector (1,2,3,4) to the vector (5,6,7,8), then when you "change from right-handed to left-handed", should it take (1,2,3,4) to (5,7,6,8), or should it take (1,3,2,4) to (5,7,6,8), or did you mean for it to do something else? Commented Aug 11, 2009 at 23:52
  • 6
    Isn't your question formulated incorrect? You're not trying to switch from right-handed > left-handed coordinate system, you're just changing which axis that is up. Because i imagine in both instances that the positive axis related to front and back points towards the viewer from origo?
    – Tistatos
    Commented Jun 18, 2014 at 8:34
  • For those as confused as I was, I'm pretty sure Anton's comment is referring to the Quaternion representation of a rotation, whereas cmann's / Mark's question is asking about a rotation matrix in homogeneous coordinates. Further; Tistatos is, unfortunately, underestimating what a mess coordinate systems are: there are coordinate systems in which a view/camera/projection axis is toward the viewer, and others in which it is away.
    – WillC
    Commented Oct 25, 2018 at 13:22

9 Answers 9

34

Let me try to explain it a little better. I need to export a model from Blender, in which the z axis faces up, into OpenGL, where the y axis faces up.

For every coordinate (x, y, z) it's simple; just swap the y and z values: (x, z, y).
Because I have swapped the all the y and z values, any matrix that I use also needs to be flipped so that it has the same effect.

After a lot of searching I've eventually found a solution at gamedev:

If your matrix looks like this:

{ rx, ry, rz, 0 }  
{ ux, uy, uz, 0 }  
{ lx, ly, lz, 0 }  
{ px, py, pz, 1 }

To change it from left to right or right to left, flip it like this:

{ rx, rz, ry, 0 }  
{ lx, lz, ly, 0 }  
{ ux, uz, uy, 0 }  
{ px, pz, py, 1 }
3
  • 6
    Your answer seems to be change from left-hand coordinate system to right-hand coordinate system,but your problem description is to change the direction of up vector.
    – zionpi
    Commented Sep 22, 2014 at 1:26
  • 3
    opengl is a right handed system. if you choose y axis up, and just swap y and z axis, you got a coordinate system not subjected to any of left/right handed coordinated system. I don't think this is a right answer.
    – flankechen
    Commented Aug 12, 2016 at 12:05
  • I thought both Blender and OpenGL use right-hand-system?
    – vicky Lin
    Commented Feb 16, 2022 at 8:49
16

I think I understand your problem because I am currently facing a similar one.

  • You start with a world matrix which transforms a vector in a space where Z is up (e.g. a world matrix).

  • Now you have a space where Y is up and you want to know what to do with your old matrix.

Try this:

There is a given world matrix

Matrix world = ...  //space where Z is up

This Matrix changes the Y and Z components of a Vector

Matrix mToggle_YZ = new Matrix(
{1, 0, 0, 0}
{0, 0, 1, 0}
{0, 1, 0, 0}
{0, 0, 0, 1})

You are searching for this:

//same world transformation in a space where Y is up
Matrix world2 = mToggle_YZ * world * mToggle_YZ;

The result is the same matrix cmann posted below. But I think this is more understandable as it combines the following calculation:

1) Switch Y and Z

2) Do the old transformation

3) Switch back Z and Y

3
  • 5
    If there is need to mirror an axis, you can do this by adding this matrix in your world matrix multiplication: Matrix mMirror_X = new Matrix( {-1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
    – Gerrit
    Commented Sep 18, 2012 at 15:20
  • Can you help me explain why the formula is Matrix world2 = mToggle_YZ * world * mToggle_YZ;, but not Matrix world2 = world * mToggle_YZ;? That is, why switch back Z and Y? Commented Aug 26, 2019 at 9:34
  • you do the "which is axis is 'up'" calculation twice. First appearance of mToggle_YZ changes Y to Z, so that it fits to your existing world matrix. The second appearance of mToggle_YZ switches again and toggles Z back to Y -- other explanation -- mToggle_YZ (go from Y is-up to a land where Z is up) * world (your matrix in Z-is-Up land) * mToggle_YZ (go back where Y is up)
    – Gerrit
    Commented Aug 26, 2019 at 13:08
16

It is often the case that you want to change a matrix from one set of forward/right/up conventions to another set of forward/right/up conventions. For example, ROS uses z-up, and Unreal uses y-up. The process works whether or not you need to do a handedness-flip.

Note that the phrase "switch from right-handed to left-handed" is ambiguous. There are many left-handed forward/right/up conventions. For example: forward=z, right=x, up=y; and forward=x, right=y, up=z. You should really think of it as "how do I convert ROS' notion of forward/right/up to Unreal's notion of forward/right/up".

So, it's a straightforward job to create a matrix that converts between conventions. Let's assume we've done that and we now have

mat4x4 unrealFromRos = /* construct this by hand */;
mat4x4 rosFromUnreal = unrealFromRos.inverse();

Let's say the OP has a matrix that comes from ROS, and she wants to use it in Unreal. Her original matrix takes a ROS-style vector, does some stuff to it, and emits a ROS-style vector. She needs a matrix that takes an Unreal-style vector, does the same stuff, and emits an Unreal-style vector. That looks like this:

mat4x4 turnLeft10Degrees_ROS = ...;
mat4x4 turnLeft10Degrees_Unreal = unrealFromRos * turnLeft10Degrees_ROS * rosFromUnreal;

It should be pretty clear why this works. You take a Unreal vector, convert it to ROS-style, and now you can use the ROS-style matrix on it. That gives you a ROS vector, which you convert back to Unreal style.

Gerrit's answer is not quite fully general, because in the general case, rosFromUnreal != unrealFromRos. It's true if you're just inverting a single axis, but not true if you're doing something like converting X→Y, Y→Z, Z→X. I've found that it's less error-prone to always use a matrix and its inverse to do these convention switches, rather than to try to write special functions that flip just the right members.

This kind of matrix operation M * X * inverse(M) comes up a lot. You can think of it as a "change of basis" operation; to learn more about it, see https://en.wikipedia.org/wiki/Matrix_similarity.

3
  • 3
    wow, it's very userful!Thanks so much. I'm confused when read Gerrit's answer.*unrealFromRos * is just the same as rosFromUnreal in his answer. Commented Aug 26, 2019 at 9:54
  • unrealFromRos is "swap Y and Z". rosFromUnreal is unrealFromRos.inverse -- which also works out to "swap Y and Z". It's actually quite common for these matrices to be equal to their inverses. As another example, Unity -> glTF is "flip the X axis", which is equal to its inverse. But watch out for those other cases! Commented Aug 26, 2019 at 20:55
  • This answer is gold and most complete among other answers. You can add more pointers on how to generate the unrealFromRos matrix, like say from the composition of Euler angles or any other methods. Also, the concept that instead of converting a matrix to another coordinate frame, just convert the vector from that particular frame to the matrix frame, apply matrix operation, and convert vector back to the original coordinate frame, is the crux.
    – warriorUSP
    Commented Jul 12, 2020 at 15:03
8

After 12 years, the question is still misleading because of the lack of description of axis direction.

What question asked for should probably be how to convert to . goal The answer by @cmann is correct for the above question and @Gerrit explains the reason. And I will explain how to graphically get that conversion on the transform matrix.

We should be clear that orthogonal matrix contains both rotation matrix and point reflection(only point reflection will change the coordinate system between left-handed and right-handed). Thus they can be expressed as a 4x4 matrix and obey to transform matrix multiplying order. "The matrix of a composite transformation is obtained by multiplying the matrices of individual transformations."

to contains both rotation matrix and point reflection. But we can get the composite transformation graphically.

According to above image, after transformation, in RhC(Right-handedCorrdinate) will be in LfC as below

where is a transform bring points expressed in above RhC to points expressed in LhC. Now We are able to convert () to () accroding to transform matrix multiplying order as below image. enter image description here The result is the same as @cmann's.

Result:

6

I have been working on converting the Unity SteamVR_Utils.RigidTransform to ROS geometry_msgs/Pose and needed to convert Unity left handed coordinate system to the ROS right handed coordinate system.

This was the code I ended up writing to convert coordinate systems.

var device = SteamVR_Controller.Input(index);
// Modify the unity controller to be in the same coordinate system as ROS.
Vector3 ros_position = new Vector3(
    device.transform.pos.z,
    -1 * device.transform.pos.x,
    device.transform.pos.y);
Quaternion ros_orientation = new Quaternion(
    -1 * device.transform.rot.z,
    device.transform.rot.x,
    -1 * device.transform.rot.y,
    device.transform.rot.w);

Originally I tried using the matrix example from @bleater, but I couldn't seem to get it to work. Would love to know if I made a mistake somewhere.

HmdMatrix44_t m = device.transform.ToHmdMatrix44();
HmdMatrix44_t m2 = new HmdMatrix44_t();
m2.m = new float[16];
// left -> right
m2.m[0] = m.m[0]; m2.m[1] = m.m[2]; m2.m[2] = m.m[1]; m2.m[3] = m.m[3];
m2.m[4] = m.m[8]; m2.m[5] = m.m[10]; m2.m[6] = m.m[9]; m2.m[7] = m.m[7];
m2.m[8] = m.m[4]; m2.m[9] = m.m[6]; m2.m[10] = m.m[5]; m2.m[11] = m.m[11];
m2.m[12] = m.m[12]; m2.m[13] = m.m[14]; m2.m[14] = m.m[13]; m2.m[15] = m.m[15];

SteamVR_Utils.RigidTransform rt = new SteamVR_Utils.RigidTransform(m2);

Vector3 ros_position = new Vector3(
    rt.pos.x,
    rt.pos.y,
    rt.pos.z);
Quaternion ros_orientation = new Quaternion(
    rt.rot.x,
    rt.rot.y,
    rt.rot.z,
    rt.rot.w);
1
  • I see the Steam_Utils.RigidTransform convertion is difference from here. Would you like to complete your answer here?
    – NTj
    Commented Apr 14, 2016 at 7:25
1

It depends if you transform your points by multiplying the matrix from the left or from the right.

If you multiply from the left (e.g: Ax = x', where A is a matrix and x' the transformed point), you just need to swap the second and third column. If you multiply from the right (e.g: xA = x'), you need to swap the second and third row.

If your points are column vectors then you're in the first scenario.

1
  • Good point: just to be clear - because I fell over this - if the system you use writes a vector as a column, you multiply a chain of rotation matrices from right to left ("start at x, do A, then do B, then do C, then you'll be facing y" is y = CBAx) . _Whereas_ if your system writes a vector as a row, you multiply in a left-to-right order --> y = xABC
    – WillC
    Commented Oct 25, 2018 at 13:30
0

Change sin factor to -sin for swaping coordinate spaces between right and left handed

0

There are several things to consider here. I will analyze each case and then choose which fits the problem brought by the original poster. For simplicity's sake, I will assume the representation and storage convention are the same.

I) EQUIVALENT SYSTEMS: Different labels for the axes, same intended meaning (East/Right, North/Front/Onward and Up) and same representation/storage convention order.

In this case, there is nothing to do, they are just labels. We can choose any letters or symbols. Both systems represent East with x, one represents North with y and the other with z. We are going to assume that x is the 1st axis and the other, y or z, is the second. If the systems use (x,y,z) and (x,z,y) respectively to represent a triplet, there is nothing to do. For example, 3 East, 1 North and 7 Up is represented in the same way in both systems: (3,1,7).

But, as the problem state that the second system is left-handed, we can discard this case. The only way the second system can be a left-handed system with the same graphic representation (assuming x as the 1st axis), is by adopting the y axis as the second axis, which produces a "vertical" R2.

II) NON-EQUIVALENT SYSTEMS: Different labels and same intended meaning but different representation/storage convention.

Above it was stated that the second system has y as the second axis and it points up. That means that the triplet is written as (x, y, z). In other words, our example will be represented as (3,7,1). That means that when converting from the first system to the second, it is necessary to swap the 2nd. and 3rd. columns. The case brought by the OP fits this case.

III) OTHER NON-EQUIVALENT SYSTEMS:

There are lots of ways to do this. We have not analyzed cases when positive x points West or maybe downwards. What if matrices contain column-vectors instead of row-vectors?

Matrix with row-vectors

X1 Y1 Z1

X2 Y2 Z2

X3 Y3 Z3

Matrix with column-vectors

X1 X2 X3

Y1 Y2 Y3

Z1 Z2 Z3

FINAL WORDS If you are confused about this topic is completely understandable. To really grasp complete understanding of this topic I had to research for a month and gather information from a huge amount of web sites, blogs and software product documentations.

I answered a similar question here: https://math.stackexchange.com/questions/3431461/different-representations-of-3d-cartesian-axes/4657893#4657893

-21

Since this seems like a homework answer; i'll give you a start at a hint: What can you do to make the determinant of the matrix negative?

Further (better hint): Since you already know how to do that transformation with individual vectors, don't you think you'd be able to do it with the basis vectors that span the transformation the matrix represents? (Remember that a matrix can be viewed as a linear transormation performed on a tuple of unit vectors)

2
  • 4
    Thanks but actually it isn't homework and I don't really know any of the math behind matrices so I don't actually understand any of that.
    – cmann
    Commented Aug 11, 2009 at 21:47
  • Okay, to be more pragmatic and less mathematical, start with the fact you can treat the rotation and translation seperately..
    – jagdit
    Commented Aug 11, 2009 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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