# World, View, and Projection Matrices

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

## Recommended Posts

I have quite the trouble with the world, view, and projection matrices. I understand how the world matrix works, but I don't know how to implement it can you give an example? also for the perspective projection I use the following method:
[source lang="csharp"] public static Matrix3D ProjectionMatrix(double angle, double aspect, double near, double far)
{
double size = near * Math.Tan(MathUtils.DegreeToRadian(angle) / 2.0);
double left = -size, right = size, bottom = -size / aspect, top = size / aspect;
Matrix3D m = new Matrix3D(new double[,] {
{2*near/(right-left),0,(right + left)/(right - left),0},
{0,2*near/(top-bottom),(top+bottom)/(top-bottom),0},
{0,0,-(far+near)/(far-near),-(2 * far * near) / (far - near)},
{0,0,-1,0}
});
return m;
}
[/source]is there anything wrong here?
and the last question how do I use the camera matrix? Edited by Belos

##### Share on other sites
I don't know if it will help you, but you might read my Matrix tutorial. It's written for XNA and XNA does a lot of work for setting up you matrices for you. I haven't had to think about manually loading a matrix in a long time.

Here's the tutorial:

[url="http://xna-3d-101.com/Fundamentals/Matrices.html"]http://xna-3d-101.co...s/Matrices.html[/url]

It looks like you're working in C++ with DirectX. So, this may not be terribly helpful if you have to load your own matrices.

But Khan Academy has lessons on Matrices.

I swear that I saw a good video on YouTube on the subject of using Matrices in game creation the other day. I thought I book marked it, but apparently I didn't and I can't find it.

For 3D, you are going to be using 4 by 4 matrices.

The camera matrix (called a View Matrix in XNA) should store your camera position and facing.

Hopefully someone else will post something more helpful.

Post Script: Ah HA! I [b]found[/b] it. I think this guy does a pretty good job of explaining how matrices are loaded for 3D games.

##### Share on other sites
[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3][background=transparent]I used the following code to get the camera:[/background][/size][/font][/color]

[CODE] Matrix3D Camera
{
get
{
Vector3D cameraZAxis = -this.LookDirection;
cameraZAxis.Normalize();

Vector3D cameraXAxis = Vector3D.CrossProduct(this.UpDirection, cameraZAxis);
cameraXAxis.Normalize();

Vector3D cameraYAxis = Vector3D.CrossProduct(cameraZAxis, cameraXAxis);

Vector3D cameraPosition = (Vector3D)this.Position;
double offsetX = -Vector3D.DotProduct(cameraXAxis, cameraPosition);
double offsetY = -Vector3D.DotProduct(cameraYAxis, cameraPosition);
double offsetZ = -Vector3D.DotProduct(cameraZAxis, cameraPosition);

return new Matrix3D(new double[,]{{cameraXAxis.X, cameraYAxis.X, cameraZAxis.X, 0},
{cameraXAxis.Y, cameraYAxis.Y, cameraZAxis.Y, 0},
{cameraXAxis.Z, cameraYAxis.Z, cameraZAxis.Z, 0},
{offsetX, offsetY, offsetZ, 1}});
}
}

[/CODE]

[color=#5A5A5A][font=tahoma, helvetica, arial, sans-serif][size=3][background=transparent][background=transparent]The default camera matrix is[/background]
[background=transparent]{1,0,0,0},[/background]
[background=transparent]{0,1,0,0},[/background]
[background=transparent]{0,0,1,0},[/background]
[background=transparent]{-100,0,0,1}[/background]
[background=transparent]after I multiply the three matrices(the third is the model matrix) by the point and the resulting W in the point is very huge like -13000 [img]http://forum.codecall.net/public/style_emoticons/default/vbmodern/set1/scared.gif[/img] [background=transparent]so I am very confused right now!![/background][/background]
[background=transparent][background=transparent]the model matrix is[/background][/background]
[background=transparent][background=transparent]{0,0,0,100}[/background][/background]
[background=transparent][background=transparent]{0,0,0,100}[/background][/background]
[background=transparent][background=transparent]{0,0,0,100}[/background][/background]
[background=transparent][background=transparent]{0,0,0,1} [/background][/background]
[background=transparent]Can you tell me where I have gone wrong?[/background]
note: this is not xn[/background][/size][/font][/color] Edited by Belos

##### Share on other sites

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