# How does lookAt matrix math work?

Can someone provide me an explanation of how the lookAt matrix works?

+----+----+----+----+
| Xx | Xy | Xz |  0 |  <- x axis
+----+----+----+----+
| Yx | Yy | Yz |  0 |  <- y axis
+----+----+----+----+
| Zx | Zy | Zz |  0 |  <- z axis
+----+----+----+----+
| Tx | Ty | Tz |  1 |  <- camera position
+----+----+----+----+


From my current understanding using the diagram above from this tutorial, the zAxis is created by subtracting camera position from the target position. Then you create two more perpendicular vectors calculated by: xAxis = zAxis x up, yAxis = xAxis x zAxis.

However, I don't understand how these exact operations result in the camera facing towards something? Like why are these exact calculations needed? Why could it not have been zAxis = camPosition - targetPosition? Or yAxis = zAxis x down instead of zAxis x up? Or have xAxis = targetPosition - camPosition instead of the zAxis?

The positive zAxis actually points away from the target. Thus what I wrote previously was incorrect, it should instead be: zAxis = norm(camPosition - targetPosition). This will cause the positive zAxis to point out of the screen from the camera's perspective, and thus the negative zAxis to go into the screen towards the target. This means then means the target can be rendered as it is located on the negative zAxis of the camera (remember that the camera only renders vertices on negative zAxis).
Because of the above, that is why zAxis = norm(camPosition - targetPosition) is needed.
• "that moves objects from world space into view space", this is technically not correct. The matrix created from the operations is a camToWorld matrix which, given an object's position relative to the created camera coordinate system, gives its position in world coordinates. In order to bring an object from world to camera coordinates you need the inverse of that matrix or worldToCam Dec 6, 2021 at 11:50