I am trying to build a game based on Advance Wars in XNA. I am so far where the tiles are placed on the screen, except here starts my problem. During the game, I ofcourse want to move the camera around over the map.

Except I got no idea on how to do this. I hope there is a way of just moving a camera around and not reposition every object on the map (thus all the tiles and units), since that is currently to only thing I might get to work, with my current knowledge.

Sorry for the short post, please let me know if I need to provide code or anything else.


1 Answer 1


there are a number of Camera Tutorials around that will show you how to go about creating a Matrix that will automatically move your camera to a position that you determine

all you then need to do is pass it to the Spritebatch and everything will work as expected

what you need to be careful with is then converting a onscreen event to a world position


code from article

public class Camera2d
    private const float zoomUpperLimit = 1.5f;
   private const float zoomLowerLimit = .5f;

   private float _zoom;
   private Matrix _transform;
       private Vector2 _pos;
   private float _rotation;
   private int _viewportWidth;
   private int _viewportHeight;
   private int _worldWidth;
   private int _worldHeight;        

   public Camera2d(Viewport viewport, int worldWidth, 
      int worldHeight, float initialZoom)
      _zoom = initialZoom;
      _rotation = 0.0f;
      _pos = Vector2.Zero;
      _viewportWidth = viewport.Width;
      _viewportHeight = viewport.Height;
      _worldWidth = worldWidth;
      _worldHeight = worldHeight;

   #region Properties

   public float Zoom
       get { return _zoom; }
           _zoom = value;
           if (_zoom < zoomLowerLimit)
              _zoom = zoomLowerLimit;
           if (_zoom > zoomUpperLimit)
              _zoom = zoomUpperLimit;

   public float Rotation
       get { return _rotation; }
       set { _rotation = value; }

   public void Move(Vector2 amount)
       _pos += amount;

   public Vector2 Pos
       get { return _pos; }
           float leftBarrier = (float)_viewportWidth *
                  .5f / _zoom;
           float rightBarrier = _worldWidth -
                  (float)_viewportWidth * .5f / _zoom;
       float topBarrier = _worldHeight -
                  (float)_viewportHeight * .5f / _zoom;
           float bottomBarrier = (float)_viewportHeight *
              .5f / _zoom;
       _pos = value;
       if (_pos.X < leftBarrier)
           _pos.X = leftBarrier;
       if (_pos.X > rightBarrier)
           _pos.X = rightBarrier;
       if (_pos.Y > topBarrier)
           _pos.Y = topBarrier;
       if (_pos.Y < bottomBarrier)
           _pos.Y = bottomBarrier;


   public Matrix GetTransformation()
     _transform = 
        Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
        Matrix.CreateRotationZ(Rotation) *
        Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
        Matrix.CreateTranslation(new Vector3(_viewportWidth * 0.5f,
            _viewportHeight * 0.5f, 0));

       return _transform;


                    null, null, null, null, null,

// Draw the background texture
   new Rectangle(0, 0, worldWidth, worldHeight),
   null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 1);

  • \$\begingroup\$ Mmh, yes, I think I have seen a few similar tutorials like this. To bad with mycurrent knowledge, I don't really understand it. I'll let you know how this works out. :) \$\endgroup\$
    – DijkeMark
    Jan 5, 2013 at 21:57
  • \$\begingroup\$ Looks like this seems to 'easiest' solution so far, thanks for helping \$\endgroup\$
    – DijkeMark
    Jan 6, 2013 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .