I am making an FPS in XNA. I cannot seem to make a FPS camera that I can move Forward, Back, Side to Side using the keyboard. How do I do that?

  • 1
    \$\begingroup\$ What have you tried so far? Google has tons of information, especially on such a simple subject. Look it up on the internet, you will find it. Then, if you have a specific problem, you can ask your question here. This question is way to broad for this site. \$\endgroup\$ – Basaa Aug 13 '13 at 13:46
  • \$\begingroup\$ Sorry about that.... Obviously, I'm kinda new to the site \$\endgroup\$ – RageGolem10 Aug 13 '13 at 17:34

here is some basic code that will work for you.

//class scope fields
Vector3 camPosition, camTarget;

//in the camera Update method
Matrix ViewToWorld = Matrix.Invert(view);

if(/* W key is down */)
   camPositon += viewToWorld.Forward * speed;//speed should include a factor of frame delta time
   camTarget += viewToWorld.Forward * speed;

// same code as above for S key but use viewToWorld.Backward instead

if(/* D key is down */)
   camPositon += viewToWorld.Right * speed;
   camTarget += viewToWorld.Right * speed;

//same code as above for A key but use viewToWorld.Left instead

view = Matrix.CreateLookAt(camPosition, camTarget, Vector3.Up);
| improve this answer | |
  • \$\begingroup\$ Quick Question. When you say Matrix ViewToWorld = Matrix.Invert(view); is it supposed to be viewToWorld? \$\endgroup\$ – RageGolem10 Aug 13 '13 at 15:57
  • \$\begingroup\$ read again your question... without dude... yes.. ViewToWorld is supposed to be ViewToWorld... the view matrix converts from world to view... if you invert it... you get a matrix that converts from view to world... ;) \$\endgroup\$ – Blau Aug 13 '13 at 16:06
  • 1
    \$\begingroup\$ I don't think you understand :P What I'm trying to ask is was there a typo and is ViewToWorld supposed to be viewToWorld (v is lowercased) \$\endgroup\$ – RageGolem10 Aug 13 '13 at 16:13
  • \$\begingroup\$ Yes, that was a typo. Sorry. They should all be same case \$\endgroup\$ – Steve H Aug 13 '13 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.