Sign in to follow this  

View Matrix question (c#)

This topic is 3593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Yes, you're right it is that viewMatrix line. A viewMatrix is composed of:


right.x right.y right.z 0
up.x up.y up.z 0
view.x view.y view.z 0
pos.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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
#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-

Share this post


Link to post
Share on other sites

This topic is 3593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this