Hello Guest

# viewMatrix.LookAt() returns a vector of positive NaN

• 2 Replies
• 6233 Views

#### jemhop

• 2
##### viewMatrix.LookAt() returns a vector of positive NaN
« on: May 02, 2021, 18:25:38 »
I have some code to calculate the view matrix, following code from LearnOpenGL. I'm only really using LWJGL and JOML.

This is my method to get the view matrix:
Code: [Select]
`public Matrix4f getViewMatrix() {        Matrix4f viewMatrix = new Matrix4f();        viewMatrix = viewMatrix.identity();        /*direction = new Vector3f(Math.cos(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch)), Math.sin(Math.toRadians(pitch)), Math.sin(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch)));        direction.normalize();         First do the rotation so camera rotates over its position        viewMatrix.rotate((float)pitch, new Vector3f(1, 0, 0))                .rotate((float)yaw, new Vector3f(0, 1, 0));        Then do the translation*/        viewMatrix.translate(-position.x, -position.y, -position.z);        updateCameraVectors();        viewMatrix.lookAt(position, position.add(cameraFront) , cameraUp, viewMatrix);        return viewMatrix;    }`
And my function to update the vectors
Code: [Select]
`public void updateCameraVectors() {        cameraFront.x = Math.cos(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch));        cameraFront.y = Math.sin(Math.toRadians(pitch));        cameraFront.z = Math.sin(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch));        cameraFront.normalize();        //cross product of camerafront and 0,1,0 stored in cameraRight        cameraFront.cross(new Vector3f(0, 1, 0), cameraRight);        cameraRight.normalize();        //gets the cross product of camera right with camera front and stores it in cameraUp        cameraRight.cross(cameraFront, cameraUp);        cameraUp.normalize();    }`
The problem I'm having is that when I do viewMatrix.lookAt(position, position.add(cameraFront) , cameraUp, viewMatrix), it's equal to NaN. I can't really tell what I'm doing wrong, any help would be appreciated.

#### KaiHH

• 334
##### Re: viewMatrix.LookAt() returns a vector of positive NaN
« Reply #1 on: May 02, 2021, 18:49:12 »
position.add(cameraFront) does not do what you think it does.
So, position itself is being modified and so the first and the second argument to the lookAt method will be the same then.

#### jemhop

• 2
##### Re: viewMatrix.LookAt() returns a vector of positive NaN
« Reply #2 on: May 02, 2021, 19:08:25 »
Then I'm assuming that

Code: [Select]
`        viewMatrix.translate(-position.x, -position.y, -position.z);        updateCameraVectors();        Vector3f temp = new Vector3f();        position.add(cameraFront, temp);        viewMatrix.lookAt(position, temp, cameraUp, viewMatrix);        System.out.println(viewMatrix);        return viewMatrix;`
would work? If so, I'm having some other problem that makes me unable to see my square, but whatever it is, I'm glad to have eliminated one thing.

edit: turns out my mouselook is just broken