genaray
genaray

Reputation: 1180

Rotate quaternion in Libgdx

How do we rotate a quaternion in libgdx around a certain axis ?


var rotation = new Quaternion().setEulerAngles(0.0f, 0.0f, 90.0f);
// How do we rotate that quaternion around X axis 90 degrees ?

Upvotes: 3

Views: 477

Answers (1)

httpdigest
httpdigest

Reputation: 5797

Rotating a quaternion means: multiply another quaternion to the right or to the left of your original quaternion (depending on the space you want to rotate). If, for example, you want a vector to first rotate 90 degrees around the X axis, followed by 90 degrees around the Z axis, then this can be achieved with the following code:

// your quaternion (rotation of 90 degrees around Z axis)
Quaternion rotation = new Quaternion().setEulerAngles(0, 0, 90);
// quaternion expressing rotation of 90 degrees around X axis
Quaternion x90deg = new Quaternion().setEulerAngles(0, 90, 0);
// concatenation (right-multiply) of the X rotation with the Z rotation
Quaternion result = new Quaternion(rotation).mul(x90deg);
// test: rotate (0, 0, 1) around X axis 90 deg, followed by Z axis 90 deg
// the first rotation will yield (0, -1, 0), and the second rotation (1, 0, 0) as a result
Vector3 v = new Vector3(0, 0, 1);
result.transform(v);
// v = (1, 0, 0)

Upvotes: 3

Related Questions