Sign in to follow this  
MattCa

gluLookAt and it's target vector

Recommended Posts

MattCa    109
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:

[code] // 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,
rotatedForwardVector.z);

Vector3f.add(playerPosition, finalTarget, cameraLookAt);[/code]

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?

Thanks,
Matt.

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this