Confused with matrices in XNA

Started by
2 comments, last by CC Ricers 11 years, 8 months ago
I have read several detailed tutorials about matrices, but I never can get them to work right. In my game, the player's position on the map is a Vector3 at the player's head . This vector is altered by keyboard input, so for example if Keys.W is pressed then position.Z+=speed*time since last frame. Then I create a new view matrix each frame with the new player position. Is this a good way to do a first person camera? It never seems to work right for me. Even without doing rotations, it rotates all over the place and goes out of view.
Advertisement
I think we would need more code from you to be able to tell what are you doing wrong.

In theory, what you said should work, so if it doesn't then you have some error somewhere. But of course it's only a very simple implementation, I think you are aware of the fact that you are not moving the player "forward" (in the direction he's facing) but always on the Z axis.

We really need more info from you, not just an example of what the W key is doing.
I've run into this kind of problem before, and it always stemmed from my camera/player's movement update function. If you have a method that updates position or rotation every frame based on another vector (modified by player input), make sure you zero out the referenced vector after you create the transformation matrix.

That's just a blind guess though, so like Tom mentioned, some code would be helpful ;)

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

I don't know whether your problem is related to the order of matrix operations you're doing, because matrix multiplication is not commutative. Also I don't quite understand what you mean when you say the first-person camera goes "out of view" from itself. But as a rule of thumb, for most cases you would need to follow the S-R-T mnemonic, that is, Scale, Rotate, then Translate in that order. This would help keep your camera from rotating around a point that's different from its own location.

New game in progress: Project SeedWorld

My development blog: Electronic Meteor

This topic is closed to new replies.

Advertisement