T. Grumser
T. Grumser

Reputation: 139

Rotate a quaternion / Change the axis it rotates around

Conceptually a quaternion can store the rotation around an axis by some degree. Now, what is the numerically most robust and least calculation intensive way to rotate the axis?

For example when i have a quaternion, which rotates 90 degrees around the y-axis and i want those 90 degrees to be applied to some other arbitrary axis, described by a normalized vector.

EDIT: Since this also came up i added an answer on how to correctly change the axis a quaternion rotates around with another quaternion.

Upvotes: 1

Views: 6274

Answers (2)

T. Grumser
T. Grumser

Reputation: 139

@derHugo's solution, solves the problem i initially asked, but the seconds part of his answer isn't doing what he seemed to be describing. To rotate a quaternions axis of rotation with another quaternion you would need to apply the rotations differently.

E.g. you have a quaternion yQuaternion, which rotates 90° around the y-axis and want to rotate, it's rotation axis by 90° around the x-axis (which would result in a quaternion rotating 90° around the z-axis) you'd have to do the following.

// The quaternion, we want to "rotate"
var yQuaternion = Quaternion.Euler(0f, 90f, 0f);
// The quaternion we want to rotate by.
var xQuaternion = Quaternion.Euler(90f, 0f, 0f);

var result = xQuaternion * yRotation * Quaternion.Inverse(xQuaternion);

What happens here is that we first rotate backwards to our desired axis, then apply the rotation we want to use and then revert the rotation we initally applied.

NOTE: I'm quit sure, that saying "rotate a quaternion" isn't a valid term when talking about this quaternion operations.

Upvotes: 3

derHugo
derHugo

Reputation: 90872

It is a bit unclear what your actual goal is by doing what you describe.

In order to actually keep the angle but change the axis you would use Quaternion.ToAngleAxis, alter the axis and then pass it back into Quaternion.AngleAxis

like e.g.

Quaternion someRotation;

someRotation.ToAngleAxis(out var angle, out var axis);

var newAxis = Vector3.up;
var newRotation = Quaternion.AngleAxis(angle, new axis);

Or you rotate an existing Quaternion by another one using * like

Quaternion newRotation = someRotation * Quaternion.Euler(90, 0, 0);

which would take the existing rotation and rotate it by 90° around the X axis.

Upvotes: 4

Related Questions