- Home /
Need to convert to/from right-handed coordinates
I need to convert Unity's left-handed coordinates to/from right-handed for some export/import functionality. Flipping a sign is not enough, I need the rotations to be correct too. So I'm hoping for a function that converts a Transform to right-handed. I've seen some posts on the net about matrices and stuff, I'm hoping to avoid that. Just need position and euler angles to be converted. Thanks.
(Would it be enough to convert to axis/angle, flip the z-axis, then read off the eulers?)
you do realize that saying right-handed/left-handed actually doesn't say much (considering that in all reality handedness only ever talks about x). all you have said is what way the positive x values point. because the conversion from left-handed physics (x-right, y-up) to right handed physics(x-left, y-up) is different from left-handed physics(x-right, y-up) to right-handed math(x-left, z-up).
can you be more specific because there is an immediate difference even in your position transformation.
Right. So converting position is easy, just negate x or y or z. I negate z and all positions look ok. However, rotations are not so simple. When the local z-axis is aligned with world z, a positive rotation in LHC will result in the opposite rotation in RHC. But I can't simply negate an euler angle (at least, I have not found a combo that works in all cases). I tried converting the rotation to axis/angle, negating z, and reading out the eulers. This 'seems' to work, but it's still off, does not look right in all cases (unless I have another bug somewhere). I saw some posting in other forums about needing to know the rotation order (xyz vs zxy vs xzy etc) and there are 36 combinations of that. A bit daunting for me to try them all. I gotta figure there's an easier way than trial/error. There must be a single formula, or maybe a small series of lines of code, which can solve this. Can I play tricks with Quaternions? At this point, if anyone has any solution that works, even if it involves mucking with matrices, I'm game.
You face multiple problems when converting between left and right handed systems. As you said the positions are quite easy, however it also depends on how your target system looks like. There are several representations of a right handed system.
If you search with this link:
http://www.google.com?q=convert+from+left+handed+to+right+handed
You will find this document:
http://answers.unity3d.com/storage/temp/12048-lefthandedtorighthanded.pdf
Btw: The more common system is the right-handed system. OpenGL (which is more math based) uses a righthanded system by default. DirectX (which is more problem orientated) uses a left handed system. Both can use both systems. The Left handed system is more useful for game engines because you can use x-y as the screen plane and z as depth. However even in screen space there are several ways (0,0 at left bottom or at left top).
Yes I had seen that PDF. It has no Unity code in it :( and this matrix stuff is a little beyond me. It looks like the 'composite' stuff might be able to convert from left to right. I also need right to left. $$anonymous$$y target system: I don't know beyond Y up, X right, Z out (all same as Unity except the direction of Z)
Answer by DaveA · Aug 06, 2013 at 02:06 AM
I think you can convert to axis/angle and flip the z axis, but also negate the angle before putting them back. Something like (untested):
// convert to quat (make a dummy object so you can use the transform?
dummyGO.transform.eulerAngles = new Vector3(rX, rY, rZ);
var angle = 0.0;
var axis = Vector3.zero;
dummyGO.transform.rotation.ToAngleAxis(angle, axis);
axis.z = -axis.z;
dummyGO.transform.rotation = Quaternion.AngleAxis(-angle, axis);
var lX = dummyGO.transform.rotation.x; // etc.
Alternately:
dummyGO.transform.eulerAngles = Vector3.zero;
dummyGO.transform.Rotate(0,0, rZ, Space.Self);
dummyGO.transform.Rotate(-rX,0,0, Space.Self);
dummyGO.transform.Rotate(0,-rY, 0, Space.Self);
or
dummyGO.transform.eulerAngles = Vector3.zero;
dummyGO.transform.Rotate(-rX, -ry, rZ);
You may also want to read this: http://sarveshspn.blogspot.com/2012/04/3d-graphics-left-handed-vs-right-handed.html
Answer by gardian06 · Jul 29, 2013 at 09:09 PM
because Unity Answers is supposed to be a repository of useful information that any user can plug in a question they have, and get on the right track, and also in several cases it is possible for more then one solution to be given (in several cases a more simple one)
and the reason that many of results you find are matrices is because that is the fundamental definition of a coordinate system.
if you don't want to use the matrices themselves then use the abstracted result of the matrices: take the euler angle of the quat, and then use the derived result of rotating the axes (no it is not just flip the z) then you get out a right Handed quat.
Ok thanks. All I know is the docs on the format say 'right handed' so that's why I need to convert, and I know that position can be 'fixed' by inverting Z, but rotation is trickier.
I will do matrices if really needed, but will try this euler-from-quat idea. I'm fuzzy on how to 'derive the result of rotating the axes'. Can you explain more? I'm also guessing this is not rotataing about Y 180 degrees...
Your answer
Follow this Question
Related Questions
Cannot convert `UnityEngine.Transform' expression to type `UnityEngine.Vector3'(Solved) 2 Answers
Symmetry for hand 0 Answers
Quaternion, Eulers and Problems 1 Answer
Is there any way to set a Gameobject's transform matrix to be the World Space? 0 Answers
How to set euler.z to zero while keeping orientation 0 Answers