Search code examples
c#xnalogic

C# XNA - I have a Camera looking at a point -- so why do I need a projection matrix as well? Logic Question


I am curious about why exactly nothing will display without a projection matrix and what exactly the projection matrix is doing. I've scoured MSNDN to no avail, so I thought I'd try asking someone with experience in it's use.

I have a look at point:

viewMatrix = Matrix.CreateLookAt(camPos, trackPos, player.getMatrix().Up);

...From my understanding of how this works I shouldn't need any supplemental information to make a camera work. I can make everything show up fine using a projection matrix but I really just don't understand why it is needed / what exactly it is accomplishing.

Maybe if someone can pass a link or explain it in brief I will be able to better manipulate the property and get some smoother camera work out of it, but overall I don't like using code I don't comprehend fully.


Solution

  • The projection matrix transforms from camera space to 2d.

    • World matrix = Object transformation
    • Camera matrix = Transforms World to Camera space
    • Projection matrix = Transforms Camera space to 2d space

    Without Projection you are stuck in 3d, and we dont have 3d monitors (yet).

    Easy example: If you have a object centered in object space and apply rotation to world matrix you will rotate object around its own centre.

    If you later on rotate the camera, all objects will rotate around the cameras position.

    Later on you will need to want to see what you have done, then you apply the projection. It can either use perspective or not. With perspective projection you basically divide the x,y with z, the most trivial perspective projection is probably:

    x2d = x/z * screenwidth + screenwidth/2;
    y2d = y/z * screenheight + screenheight/2;
    

    This gives us a "3d feeling" by points further into 3d space being closer middle of screen, think of a classic star field and youll understand. For more information you will need to understand basic linear algebra, and I suggest you start there as you are interested!