views:

1376

answers:

3

I'm having some serious trouble understanding the view matrix in XNA. I've come pretty far with all the other parts and since I've just learnt myself the required maths I don't want to use the built in Matrix-functions without understanding what they do first.

Now I understand the basics of rotation, projection and translation but I can't for the life of me understand how the view matrix works in XNA.

From what I've gathered, the view matrix should transform the "world" to it's own space. Seems reasonable, but the Matrix.CreateLookAt method in the library is quite puzzling.

I've established (through examining what the library function outputs) that these two pieces of code yield the same results:

Matrix view = Matrix.CreateReflection(new Plane(Vector3.UnitX, 0)) * Matrix.CreateReflection(new Plane(Vector3.UnitZ, 0)) * Matrix.CreateTranslation(Position);
// ..equals this if (Position = (0 0 -5), since LookAt "looks at" but the above just looks straight down Z)..
Matrix blah = Matrix.CreateLookAt(Position, Vector3.Zero, Vector3.UnitY);

Why flip the X and Z axes? I thought you should rotate the world according to the cameras rotation, but in the opposite direction, and then translate the world by the same amount in the opposite direction.

Or is the view matrix not used as a transformation at all but just encodes the position and rotation of the camera in the world?

+1  A: 

composing two reflections yields one rotation, if I remember my geometry correctly.

Jimmy
Interesting. The above mentioned reflections should then correspond to a 180 degree rotation around Y.
Skurmedel
+5  A: 

There are three types of transforms you need to understand: world, view and projection.

The world transform changes your models coordinates from model space into world space. When you define the vertices of a model, you define them relative to the origin of that model. The world transform transforms those vertices so they are relative to an origin shared by all objects in the scene, i.e. world space.

The view transform then transforms these vertices into view space. The viewer/camera has a position and orientation in view space, and this is used to create the view matrix which you apply to all objects in the scene. In view space the viewer/camera is at the origin, so the view matrix translates and rotates all objects to place them in view space.

Finally the projection transform determines what type of "lens" the camera uses. So far we've only translated and rotated objects, there's no sense of scale or perspective. The projection transform creates a viewing frustum and transforms vertices into this viewport.

Now for your question. You reflect the coordinate system about the x-axis, then the z-axis and finally you translate it. This happens to yield the same result as the lookat matrix because the position is on the xz- and yz-plane, but notice what happens if you move the position one unit up the y-axis. Then the two matricies will not be equal

Tchami
Thanks for you answer. I picked the answer with the lowest rank. You both helped.
Skurmedel
No worries. Glad you figured it out :)
Tchami
+2  A: 

The view matrix is transforming the scene into view space. In view space the virtual camera is at the origin and is looking in the positive z-direction. There are different ways to achieve this, but most commonly the view matrix consists of a translation and three rotations around each axis.

Transform = Translate * RotationZ * RotationY * RotationX

whtimo
Thanks, it seems that my original idea was correct. But I had all these different coordinate systems a bit confused.
Skurmedel