2
\$\begingroup\$

I want to rotate an object to face a point, such as the monster look at the player.

For that I've decided to use the glm::lookAt function. However the following does not yield good results:

const glm::mat4 transformation = glm::lookAt(ghostPosition, cameraPosition, glm::vec3(0, 1, 0));
ghost.transformation = transformation;

The ghost is correctly oriented to the player but is located to the world origin. However the following works correctly (position and orientation):

const glm::mat4 transformation = glm::inverse(glm::lookAt(ghostPosition, cameraPosition, glm::vec3(0, 1, 0)))

But I don't get why I need to inverse the matrix ?

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

Because the lookAt function is to position the camera to look at an object (not for an object to look at another object) and the way 3D cameras work is that mathematically they move the entire world the opposite way and the screen stays at the origin.

So because the matrix is intended for a camera and 3d cameras work "backward" you need to inverse the matrix to get an object to look at another object.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .