# What would be the easiest way to keep objects at a constant point on a 2D map, even when off-screen?

Like in Starcraft, where your army units stay put even though your view is moving around the map.

I have a static tiled map that I made using Mappy and I can display that fine, I just want my off-screen objects to stay put while I am moving my view around. At the moment everything is drawn relative to my view instead of at a global (x, y) position and I would like to change this but I am not sure how.

Would I need another array aside from the one to create my map? That seems like a terrible idea to me.

• Hey there and welcome to GD:SE, if you could provide any code examples of how you handle your map and drawing functions we might be able to help you more. Feb 24, 2014 at 9:32

One way to do this is to have a 2D camera.

This way your player can move the camera around in the world and everything in the world is drawn relative to the cameras transformation matrix.

Here I found a camera implemented in XNA with some comments about what has been done that seems relatively straight forward: http://www.dreamincode.net/forums/topic/237979-2d-camera-in-xna/

While the camera implementation I linked is in C#/XNA you shouldn't have any trouble adapting it to C++. When you have your camera you then need to transform an objects position (x, y) to be relative to the cameras view. When you do this you get the position in world cooridnates rather than screen coordinates.

In XNA there is a Vector2.Transform() function which makes it rather easy to use by multiplying a Vector2 with a transformation matrix, so what you'd need to do to use the above example is to implement your own Transform function (or use one if there is one in the framework you are using).

• Busy at work, will try this tonight when I get home, thanks! Feb 25, 2014 at 11:08
• The basic idea is this: When your user pans in any direction, the ONLY object variable that should be changing is the X and Y of your "camera" object. The rest, how to draw it, is deterministic every time "Draw()" is called. It takes a little bit of effort reorienting your mind around it, instead of thinking of "moving the sprites off the screen" but makes perfect sense afterward. Apr 25, 2014 at 13:52