Johannes
Johannes

Reputation: 417

Movement using quaternion

I'm creating a first-person camera that rotates using Euler angles. I created the view matrix for the camera like this:

Quaternionf rotation = new Quaternionf().rotateXYZ(
  orientation.x,
  orientation.y,
  orientation.z
);

return new Matrix4f().rotate(rotation).translate(
  -this.position.x,
  -this.position.y,
  -this.position.z);

where orientation is a Vector3f of the Euler angles. How do I use the same rotation quaternion to move the camera position? And how do I get the view direction vector? I want to move the position of the camera from the view direction.

The math library I'm using is JOML, but it doesn't have a tag yet on StackOverflow.

Upvotes: 0

Views: 696

Answers (1)

httpdigest
httpdigest

Reputation: 5797

In order to get the direction of the -Z axis when using the given quaternion (or likewise the given matrix with matrix.rotate(quaternion)) as the view transformation, then you can call either:

Vector3f forward = quaternion.positiveZ(new Vector3f()).negate();

or

Vector3f forward = matrix.positiveZ(new Vector3f()).negate();

Both of which will multiply the vector (0, 0, 1) to the right of the inverted quaternion/matrix and store the result into the supplied Vector3f instance.

This will give you the "forward" direction according to this view transformation in standard OpenGL right-handed view coordinate system.

Upvotes: 1

Related Questions