# Camera Matrix

This topic is 5155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am creating my own 3D game engine and I need to know how to make a camera matrix based on a position direction and up vector.

##### Share on other sites
Quote:
 I am creating my own 3D game engine and I need to know how to make a camera matrix based on a position direction and up vector.
That isn't enough information; you'll also need something else, such as a target position or a side or forward vector.

Also, how you set up the matrix will depend on your math library conventions - specifically, whether you're using row or column vectors, and (if your matrix is a 1d array which you are accessing directly) whether the matrix is row or column major.

##### Share on other sites
First you subtract the camera position then you can use
this function
to create a rotation matrix. Just put the main camera direction as the new z axis and the up vector as the new y axis.

##### Share on other sites
Ok, if you're given the position of the camera frame, and two of the basis vectors you have enough information.

For starters, just so you know, if you have a frame (consisting of 3 basis vectors - side up and front, and a position relative to another frame), to transform a point by that frame we use the matrix:

[Sx Ux Fx Px][Sy Uy Fy Py][Sz Uz Fz Pz][0  0  0  1]

To transform vertices into that frame - as in what we need to do for a camera transformation, the inverse of this matrix is used, which is:

[Sx Sy Sz -P.S][Ux Uy Uz -P.U][Fx Fy Fz -P.F][ 0  0  0  1 ]

Since you have the up and front vectors, the side vector can be calculated using the cross product.

This assumes OpenGL. For Direct3D, the transpose of these matrices would have to be used.

##### Share on other sites
Unless you have other 4x4 matrices to pipeline you will get better performance if you subtract first and use a 3x3. Oh yeah I forgot to mention you need to do cross product to get the side vector.

##### Share on other sites
Quote:
 Original post by AlrecenkUnless you have other 4x4 matrices to pipeline you will get better performance if you subtract first and use a 3x3.

Which, unless you're writing a software engine, you will have.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 9
• 25
• 54