- Home /

# Reverse Axis of Quaternion

Is it possible to reverse 1 axis of a Quaternion? Quaternion.Inverse reverses all the data.

Would you have to convert to Euler then somehow back to Quaternion?

I'm getting quaternion orientation data from an external source but the Z data needs to be reversed and I read you're not supposed to modify Quaternion data directly. (very difficult)

I dont want to use EulerAngles because of Gimbal Locking, would reprojecting it somehow (using Slerp?) fix that?

Thanks for any help in advance

Would it require the conversion of a matrix to a quaternion? Am I onto something?

I'm receiving quaternion data over a protocol, X,Y,Z,W

Then just invert the Z coordinate and it will be fine.

Presuming you mean that the Z of the quaternion is inverted (not the Z of the resulting euler angle).

**Answer** by bderooms
·
Aug 12, 2013 at 06:19 PM

I believe (having experience with Kinect & Unity3d) that what he wants is the same Rotation in a coordinate space where Z is inverted. That comes down to mirroring around the XY plane. To mirror a quaternion around the XY plane, you need to negate the Z value and the W value.

Thank you. This is exactly what I was trying to do, and your simple explanation is everything I needed.

**Answer** by Loius
·
Apr 28, 2013 at 09:59 PM

"The Z needs to be reversed"

Because quaternions are stupid hard to deal with, that statement may mean anyt$$anonymous$$ng.

D'you want the character to face the other way? Rotate by 180 around the Y axis.

You can rotate a quaternion by multiplying it by another quaternion. Say A is your facing you want to change, and B is Quaternion.AngleAxis(Vector3.up,180) (or whatever the syntax is). If you want to rotate A by 180 around Y, then: A = B * A.

edit: As noted in the comments, you're perfectly free to alter the data in a quaternion. You just don't know what that will do, unless you do know what that will do. If you're getting bad z values, go ahead and fix 'em.

The data I'm getting comes from a system where Z is up, in Unity Y is up, thats why I need to invert them.

Directly modifying the Z as "whydoidoit" creates unexpected results.

Created another quaternion and multiplying it with mine also creates unexpected results... perhaps I'm just not understanding it correctly.

I hope I'm making some sense, this is a Kinect project, so his left arm is moving correctly other than the fact that he's moving his arm up when I move my arm down. (Due to the Z not being flippeD)

That's not an inversion. It's always best to say what you want to do :D

You need to multiply it by something like:

```
Quaternion.FromToRotation(Vector3.forward, Vector3.up);
```

Not working, how am I supposed to use it exactly? This is a code sample for my right arm:

```
private var leftShoulder : GameObject;
var leftShoulderRigObj : GameObject;
private var leftShoulderOrientation : Quaternion;
function OSCMessageReceived(message : OSC.NET.OSCMessage ){
var address : String = message.Address;
var args : ArrayList = message.Values;
if(address == "/Left_Shoulder") {
var first_Left_Shoulder : boolean = true;
var second_Left_Shoulder : boolean = false;
var third_Left_Shoulder : boolean = false;
var fourth_Left_Shoulder : boolean = false;
for( var item in args) {
if (first_Left_Shoulder) {
//Quaternion W
leftShoulderOrientation.w = item;
first_Left_Shoulder = false;
second_Left_Shoulder = true;
} else if (second_Left_Shoulder) {
//Quaternion X
leftShoulderOrientation.x = item;
second_Left_Shoulder = false;
third_Left_Shoulder = true;
} else if (third_Left_Shoulder) {
//Quaternion Y
leftShoulderOrientation.y = item;
third_Left_Shoulder = false;
fourth_Left_Shoulder = true;
} else if (fourth_Left_Shoulder) {
//Quaternion Z
leftShoulderOrientation.z = item;
fourth_Left_Shoulder = false;
}
leftShoulderRigObj.transform.rotation = Quaternion.Inverse(leftShoulderOrientation);
```

Every time I get an OSC message it runs this code and goes through a foreach loop ans saves XYZW in a quaternion, I then take that quaternion, inverse it and set it to the rotation of my gameobject in the scene.

I hope this gives some context, sorry I've been awake for 48 hours and been dealing with this problem for 7 days.. its ridiculous

I was suggesting that you do:

```
leftShoulderRigObj.transform.rotation = Quaternion.FromToRotation(Vector3.forward, Vector3.up) * leftShoulderOrientation;
```

**Answer** by s_guy
·
Apr 29, 2013 at 12:48 AM

"Is it possible to reverse 1 axis of a quaternion?" I t$$anonymous$$nk you might be confusing the XYZ of a quaternion as being 3 axes, as with Euler angles. Instead, with quaternions, the XYZ describes a single axis that the rotation W is applied to.

wikipedia overview

It sounds like you mean to ask "Suppose someone had converted Euler angles to a quaternion, and that's all I have now. How do I get the quaternion for the same Euler angles with an inverted Z axis?"

If that's the case, you might try :

```
Vector3 myEulerAngles = myQuaternion.EulerAngles();
Quaternion myFixedQuaternion = Quaternion.Euler(myEulerAngles.x, myEulerAngles.y, -myEulerAngles.z);
```

Keep in mind that Quaternion.Euler "Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order)."

If you're after somet$$anonymous$$ng different, it might help to diagram what you're trying to do.

**Answer** by TravelLIN
·
Aug 22, 2017 at 07:40 AM

Guess in your case the answer is " * ". Try to do somet$$anonymous$$ng like t$$anonymous$$s:

```
private Transform target;
private void Update()
{
// get your target's rotation
Quaternion targetRotation = target.transform.rotation;
// choose an object and multiply its axis to reverse
transform.rotation = targetRotation * Quaternion.Euler(0, 180f, 0);
}
```

**Answer** by emthele
·
Jun 09, 2016 at 12:04 PM

His question is actually not that bad, and is fully understandable if you have ever worked with sensor data on your on. The point here is, IMU sensor data usually is calculated into quaternion data. T$$anonymous$$s data can directly be applied to the GameObject Transform via euler angles:

```
transform.eulerAngles = bodyQuaternion.eulerAngles;
```

where bodyQuaternion is the quaternion from the sensor data.

Now I create a simple Cube in unity and want it to behave like my sensor. Therefore, I have to exchange the quaternion values X and Y before. In my code, t$$anonymous$$s looks like t$$anonymous$$s:

```
bodyQuaternion.w = imuSensor.Quaternion[0];
bodyQuaternion.x = imuSensor.Quaternion[1];
bodyQuaternion.z = imuSensor.Quaternion[2];
bodyQuaternion.y = imuSensor.Quaternion[3];
```

Note the order: w, x, z, y.

With that, it works perfectly fine, except the fact that my view towards the sensor would be from "be$$anonymous$$nd" in unity. W$$anonymous$$ch means, if I turn it clockwise parallel to earth gravity axis (yaw), it turns counter-clockwise in Unity. I simply see it from the wrong side in my program. Therefore, I want to invert the view axis Z in unity.

As I'm also new to Unity, I have no idea how to do best: Local axis of my cube? Another rotation with another quaternion for 180 degrees? There were some good ideas above, and I will try them.

**Update:** I managed to resolve my issue. Unfortunately, I can't give a detailed description about what worked and what not, because I struggled around with that a lot in the last hours and tried things forwards and backwards.

But the wrongly turning Yaw was actually turning right, but the code was wrong in general, because the method with

```
transform.eulerAngles = bodyQuaternion.eulerAngles;
```

with bodyQuaternion = sensor Quaternion used the world's axes for rotation, not the cube's axes.

Anyway, I managed to move the cube exactly as my sensor. Here's the topic for it: http://answers.unity3d.com/questions/1201108/quaternion-from-imu-sensor-to-gameobject-orientati.html

@emthele Could you please spare some time to talk with me. I am doing something similar and get a lot of problems and I am time pressured for my dissertation. I believe you can help me out in a matter of seconds. If you can spare some time please post here and I can send you my email (I already posted a question on this but not got any response). Thank you very much in advance. Have a nice day.

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

Maintain direction regardless of orientation (2) 1 Answer

Rotating on a plane 2 Answers

Combining rotations around X and Y axes gives unusual results 1 Answer

finding pitch/roll/yaw from Quaternions 4 Answers

Rotate object on one axis with slerp 1 Answer