• Advertisement
Sign in to follow this  

gluLookAt and it's target vector

This topic is 2155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys! I'm sure this will have been answered before, but I've not found a solution to my specific problem (either that or my searching skills aren't that good).

I'm using the following code to create a lookAt (target) vector:

// Create a matrix to calculate the cameras new rotation.
Matrix4f cameraTransformMatrix = new Matrix4f();
Matrix4f.rotate((float)Math.toRadians(xRotation), new Vector3f(1, 0, 0), cameraTransformMatrix, cameraTransformMatrix);
Matrix4f.rotate((float)Math.toRadians(yRotation), new Vector3f(0, 1, 0), cameraTransformMatrix, cameraTransformMatrix);
Matrix4f.rotate((float)Math.toRadians(zRotation), new Vector3f(0, 0, 1), cameraTransformMatrix, cameraTransformMatrix);

// Multiply forward vector with a Matrix4x4
Vector4f rotatedForwardVector = Matrix4f.transform(cameraTransformMatrix, new Vector4f(0.0f, 0.0f, 1.0f, 1.0f), null);

Vector3f finalTarget = new Vector3f(rotatedForwardVector.x, rotatedForwardVector.y,

Vector3f.add(playerPosition, finalTarget, cameraLookAt);

This works when I'm facing forwards, but when I'm looking from behind, the up/down is swapped, and when looking from the side, the up/down causes the camera to rotate in a circle.

This code worked perfectly fine in XNA, but I'm wondering whether or not I'm producing the same output. Do I need to apply any changes to the 'Up' vector?


Edit: Forgot to mention, I'm using LWJGL.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement