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

Are you receiving a quaternion or a euler angle?

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).

They're fully predictable, just they use complex 4D rotations. Inverting the Z of a quaternion won't necessarily invert the Z of the resulting euler angle. If you want to invert the euler angle then: var existing = rotation.eulerAngles; existing.z =-existing.z; rotation = Quaternion.Euler(existing);

5 Answers

5

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.

“The Z needs to be reversed”

Because quaternions are stupid hard to deal with, that statement may mean anything.

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.

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);

I was suggesting that you do: leftShoulderRigObj.transform.rotation = Quaternion.FromToRotation(Vector3.forward, Vector3.up) * leftShoulderOrientation;

“Is it possible to reverse 1 axis of a quaternion?” I think 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 something different, it might help to diagram what you’re trying to do.

Guess in your case the answer is " * ".
Try to do something like this:

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);
}

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. This 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, this looks like this:

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 “behind” in unity. Which 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.