1

I am making a game with some friends. At the moment, we are trying to get the controls working as they should, i.e. WASD to move in the 4 main directions, and use the mouse to rotate the view.

The problem is that we are running into difficulties with the matrices, specifically the view matrix. I have read more tutorials than I care to count on matrices over the past few weeks, and I understand the basic principles, and how to do some simple stuff, but I am still lacking that fundamental understanding of how it all comes together.

Right now, our main problem is getting the camera transformations to act as one would expect. Using the tutorials I have seen, the camera transformations all take place along the local axis, which causes some strange behavior, for instance, after moving around a bit, I can easily end up sideways, or even up-side-down.

These are the two functions I am using for rotating the view matrix, as seen in all the tutorials i've looked at:

def xrotate(M,theta):
t = math.pi*theta/180
cosT = math.cos( t )
sinT = math.sin( t )
R = numpy.array(
    [[ 1.0,  0.0,  0.0, 0.0 ],
     [ 0.0, cosT,-sinT, 0.0 ],
     [ 0.0, sinT, cosT, 0.0 ],
     [ 0.0,  0.0,  0.0, 1.0 ]], dtype=np.float32)
M[...] = np.dot(M,R)

def yrotate(M,theta):
t = math.pi*theta/180
cosT = math.cos( t )
sinT = math.sin( t )
R = numpy.array(
    [[ cosT,  0.0, sinT, 0.0 ],
     [ 0.0,   1.0,  0.0, 0.0 ],
     [-sinT,  0.0, cosT, 0.0 ],
     [ 0.0,  0.0,  0.0, 1.0 ]], dtype=np.float32)
M[...] = np.dot(M,R)

How would I go about applying view matrix transformations in global space?

Oh, and also, if you know of any really good matrix tutorials that don't just say 'the math here is complicated so we won't go into it, just know it works', I would love to take a look.

1 Answers1

0

It's all about order of operation:

  • Vmodel, this is what you get from the mesh

  • Vworld, this is what you get when you do MatModel*Vmodel; this is the position of of the vertex in the world

  • Vcamera, this is what you get when you do MatView*Vworld = MatView*MatModel*Vmodel; this is the position relative to the camera before projection is applied

  • Vscreen, this is the eventual position on the screen after you apply the projection matrix: MatProj*Vcamera=MatProj*MatView*Vworld = MatProj*MatView*MatModel*Vmodel;

Together the MatProj*MatView*MatModel is the MVP matrix you hear so much about.

To move the camera to the left you would left multiply the View matrix with the movement matrix that goes to the positive X direction or to the translation to the Right vector of the camera.

For a standard FPS-style heading-pitch camera (up is always to the sky) you would recreate the entire matrix when either heading or pitch changes.

MatView = xrotate(pitch)*yrotate(heading)*translate(-cameraPos);
ratchet freak
  • 47,288
  • 5
  • 68
  • 106