XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Simple 2D Camera

Last post 6/29/2009 2:54 AM by Pak514. 3 replies.
  • 6/28/2009 11:46 PM

    Simple 2D Camera

    Hi, I've been searching for a simple 2D camera for a while now with no success. All tutorials I've come across demonstrate a camera following a sprite or object.

    I'm developing a tile based rts. What I'm looking for is advice on how to transverse through a texture tile map with input such as mouse or keyboard left/right/up/down. I've made a simple tile engine to render a map based off of an xml structure, but camera concepts escape me.


  • 6/29/2009 1:15 AM In reply to

    Re: Simple 2D Camera

    I am working on a similar project and just for scrolling, I've found a very basic camera class to be sufficient. 
    My camera class contains:
    1. Position
    2. Move Speed
    3. Screen Size


    then in my update camera method, I check for movement (the keyboard in your case) and increment position by the move speed and the gametime.
    use Math.Clamp to ensure that your position doesn't exceed your map size.
    Lastly, in my draw method, check to see if the unit/tile/etc is within the screen based on the screen size and position and if it is, draw it at the units position minus the camera position.

    That is how I have done my camera to this point and it has worked without a problem for me.  Good luck
  • 6/29/2009 2:12 AM In reply to

    Re: Simple 2D Camera

    For my 2d tile game I solved the problem of the camera by passing a translation matrix to the spritebatch.  Basically I create a matrix based on the position of a focus object , in my case the player, and the screen width / height.

    I then decompose the matrix, and check the X and Y values of the translation vector3 from the decompose.  I modify those values to keep them in range as far as clamping to map boundaries when the player is near the sides / top / bottom, but keeping the player centered otherwise.

    Then create a new matrix from that vector3 with a z value of 1.  Pass the returned matrix to spritebatch.


  • 6/29/2009 2:54 AM In reply to

    Re: Simple 2D Camera

    Thanks for the info, this should be sufficient to get me started.
Page 1 of 1 (4 items) Previous Next