Advertisement

Matrices question

Started by June 21, 2002 10:02 AM
4 comments, last by z9u2K 22 years, 7 months ago
I was wondering. Creating a view matrix which is looking from point p to point d with the up vector u would be generated using this algorithm:
  
z = normal(d - p)
x = normal(cross(u, z))
y = cross(z, x)
  
and the view matrix will look like this:
  
 x.x           y.x           z.x          0
 x.y           y.y           z.y          0
 x.z           y.z           z.z          0
-dot(x, p)    -dot(y, p)    -dot(z, p)    1
  
Now since I''m programming a terrain engine in which the position of the camera sets the LOD of the terrain, I don''t like the fact that this method changes the position of my matrix. By my understanding, a matrix containes three orthogonals vetors that sets the direction of the matrix and a point in which the matrix is in. Okay, this algorithm managed to get the direction vectors for my looking-at view matrix using cross products. That I understand. But why the position of the matrix has to be changed? I mean, If I want it to look from p to d, and the matrix is already in point p, why do we have to change the position of the matrix?
The intention of the view matrix is to position the camera into the origin (0,0,0) facing the positive Z (3nd) axis. This is done so that (ignoring scale) X of the rotated system is equivalent to your monitors X. Y as well, just inverted.

In order to apply a transformation you multiply a point with a matrix. In order to undo a transformation or have the exatcly oposite effect you multiply the point with the inverse of the Matrix.

This is exactly what happens here.

x.x           y.x           z.x          0 x.y           y.y           z.y          0 x.z           y.z           z.z          0-dot(x, p)    -dot(y, p)    -dot(z, p)    1   


is the inverse of
x.x        x.y          x.z              0y.x        y.y          y.z              0z.x        z.y          z.z              0p.x        p.y          p.z              1 


The latter would move a point from the origin to the camera, therefore the inverse moves the camera from it''s point in space to the origin



I may be getting older, but I refuse to grow up
I may be getting older, but I refuse to grow up
Advertisement
Okay,
That''s for view matrices, but what if I want to make, let''s say... a tank that is pointing to a target with it''s barrel?

Won''t the change of the _4x components of the barrel''s matrix will move it to different place then on the tank?
(The barrel will ''fly'' around in the air if I''ll change it''s position relative to the tank in the world space right?)
Lets say you have a Matrix T which puts your tank in its position in the world. It will definitely have a forth line which is not zeros.
In order to have the barrel Matrix B put the barrel on top of the tank you create B by multiplying the relative rotation and position of the barrel rB to the tank with the tank Matrix T
B = rB * T 

If rB is the identity matrix, the barrel just points straight ahead of the tank. Maybe you want to add a small offset in p.y of rB, so that the Barrel comes out of the turret and not the front.
If rB is any rotation, the barrel will rotate on top of the tank. E.g if rB rotates by 90 degrees around the y-axis the barrel will allways point to the right of the tank, regardless of the tanks position and rotation. That''s the beauty of Matrixmultiplication.

Note that you can no longer set up rB like you did the view matrix before. You could set up B like that, but not rB.


---------------------------
I may be getting older, but I refuse to grow up
I may be getting older, but I refuse to grow up
Then how do you get to rB with only B and T? Using
B = rB * T 

and solving for rB give you
rB = B * T^-1 

with T^-1 beeing the inverse of T
The inverse of T can be built like the inverse of the view matrix.
Note that inverting a matrix is usually a rather tricky thing. The Transpose-the-inside-and-multiply-the-position method used for the View matrix and now for T only works on orhtonormal matrices. Meaning the rotation part of the matrix has to be made up from orthogonal vectors and each vector has to be of magnitude 1

Sorry I wanted to edit this into my last post, but editing doesn''t work for me. 500-Server not found



---------------------------
I may be getting older, but I refuse to grow up
I may be getting older, but I refuse to grow up
10x,
but I found out that if I want to create a look-atworld matrix, and not a view look-at matrix, all I have to do is to use the formula in my first post to find the x,z,y axii, map the x into the _1x compontns, the y into the _2x components, and the z into the _3x components. The all I have to do is to set the _4x components for the position, and that''s it! A looking-at world matrix!

This topic is closed to new replies.

Advertisement