View Matrix question (c#)

Started by
2 comments, last by Cygon 16 years, 2 months ago
Hi, I wonder if anyone can spot the problem in the following method? It should just move the camera. However, nothing happens. Been playing around for what feels like hours now :( Here's the code: public void Update(KeyboardInput keys) { if (keys.IsKeyPressed(Key.W)) position += lookAt * movementSpeed; if (keys.IsKeyPressed(Key.S)) position += lookAt*-movementSpeed; viewMatrix = Matrix.Translation(position); } Got a feeling its the viewMatrix line thats the problem but that should work shouldnt it? the method is definitely being called. and device.transform.view is definitely being set. So i'm positive the issues is in this function. Regards Luca
Advertisement
Yes, you're right it is that viewMatrix line. A viewMatrix is composed of:

right.x  right.y  right.z 0up.x     up.y     up.z    0view.x   view.y   view.z  0pos.x    pos.y    pos.z   1


And you are setting the viewMatrix to a translation matrix which is defined as:

1 0 0 0
0 1 0 0
0 0 1 0
x y z 1


For a simple translation, all you need to modify is the position component. So you can just do:

viewMatrix = Matrix.LookAtLH(pos, lookAt, up);

[Edited by - glaeken on February 13, 2008 10:07:38 PM]
thankyou, that got it working.
however, i've now discovered an interesting problem.

The issue is that my camera is looking at a point infront of the camera, ie: x0,y0,z0. but as the camera moves forward, that target never changes.
The result is that as my camera moves forward along the Z axis, passing z0, suddenly it does an instant pivot, because its always looking at the target x0, y0, z0.

As this is suppose to be a free moving camera, how can I update the target vector of the camera to always be infront of the camera?
#1 Recommendation:
Do not recreate your matrix each time you change it. Set up the matrix once and just modify it. This alone will avoid a lot of trouble.

I don't know what math library you're using (probably the one in MDX 1.0), but this is how I'm doing it in XNA:
public void HandleControls(GameTime gameTime) {  float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;  KeyboardState keyboardState = Keyboard.GetState();  // Translational controls  if(keyboardState[Keys.A] == KeyState.Down)    this.View.Translation += Vector3.Right * delta * 10.0f;  if(keyboardState[Keys.D] == KeyState.Down)    this.View.Translation -= Vector3.Right * delta * 10.0f;  if(keyboardState[Keys.W] == KeyState.Down)    this.View.Translation -= Vector3.Forward * delta * 10.0f;  if(keyboardState[Keys.S] == KeyState.Down)    this.View.Translation += Vector3.Forward * delta * 10.0f;  if(keyboardState[Keys.R] == KeyState.Down)    this.View.Translation -= Vector3.Up * delta * 10.0f;  if(keyboardState[Keys.F] == KeyState.Down)    this.View.Translation += Vector3.Up * delta * 10.0f;  // Rotational controls  if(keyboardState[Keys.NumPad4] == KeyState.Down)    this.View *= Matrix.CreateRotationY(-delta);  if(keyboardState[Keys.NumPad6] == KeyState.Down)    this.View *= Matrix.CreateRotationY(+delta);  if(keyboardState[Keys.NumPad8] == KeyState.Down)    this.View *= Matrix.CreateRotationX(+delta);  if(keyboardState[Keys.NumPad2] == KeyState.Down)    this.View *= Matrix.CreateRotationX(-delta);  if(keyboardState[Keys.NumPad7] == KeyState.Down)    this.View *= Matrix.CreateRotationZ(-delta);  if(keyboardState[Keys.NumPad9] == KeyState.Down)    this.View *= Matrix.CreateRotationZ(+delta);}


If you really want to keep the Matrix.LookAtLH(), you can just use the matrix' forward pointing vector ('view' in glaeken's illustration) to obtain the look-at point, eg.

viewMatrix = Matrix.LookAtLH(pos, pos + viewMatrix.Forward, up);
As I said before, I'm used to XNA, not MDX, so the forward pointing vector might be called 'viewMatrix.In' or something entirely different in your environment. Sorry.

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.

This topic is closed to new replies.

Advertisement