# Quaternion LookAt for camera

I am using the following code to rotate entities to look at points.

glm::vec3 forwardVector = glm::normalize(point - position);
float dot = glm::dot(glm::vec3(0.0f, 0.0f, 1.0f), forwardVector);

float rotationAngle = (float)acos(dot);
glm::vec3 rotationAxis = glm::normalize(glm::cross(glm::vec3(0.0f, 0.0f, 1.0f), forwardVector));
rotation = glm::normalize(glm::quat(rotationAxis * rotationAngle));


This works fine for my usual entities. However, when I use this on my Camera entity, I get a black screen. If I flip the subtraction in the first line, so that I take the forward vector to be the direction from the point to my camera's position, then my camera works but naturally my entities rotate to look in the opposite direction of the point.

I compute the transformation matrix for the camera and then take the inverse to be the View Matrix, which I pass to my OpenGL shaders:

glm::mat4 viewMatrix = glm::inverse(
cameraTransform->GetTransformationMatrix()
);


The orthographic projection matrix is created using glm::ortho.

What's going wrong?

• can't you also just do quat.xyz = sqrt(0.5 - dot/2) * glm::cross(glm::vec3(0.0f, 0.0f, 1.0f), forwardVector) and combine it with quat.w = sqrt(0.5 + dot/2) to get the components: no need for the acos, (using half angle formula to get the multiplier), though you may need to negate one of the multipliers Nov 11 '13 at 1:43

float dot = glm::dot(glm::vec3(0.0f, 0.0f, -1.0f), forwardVector);