0
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Feb 24, 2014 at 9:32

1 Answer 1

2
\$\begingroup\$

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).

\$\endgroup\$
2
  • \$\begingroup\$ Busy at work, will try this tonight when I get home, thanks! \$\endgroup\$
    – G. Meadows
    Feb 25, 2014 at 11:08
  • \$\begingroup\$ 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. \$\endgroup\$
    – Katana314
    Apr 25, 2014 at 13:52

You must log in to answer this question.

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