i'm currently coding a 2D game in Java, and I would like to have some clues on how-to build this system :

the screen is moving on a grander map, for instance, the screen represent 800*600 units on a 100K*100K map.

When you command your unit to go to another position, the screen move on this map AND when you move your mouse on a side or another of the screen, you move the screen on the map.

Not sure that i'm clear, but we can retrieve this system in most RTS games (warcraft/starcraft for example).

I'm currently using Slick 2D.

Any idea ?


  • 2
    \$\begingroup\$ What's the question? There's various ways to do a map system like this, and there's tutorials out there for most of the common methods. \$\endgroup\$ – thedaian Jun 2 '11 at 17:16
  • \$\begingroup\$ Are you asking on how to move the camera? \$\endgroup\$ – Tetrad Jun 2 '11 at 18:57
  • \$\begingroup\$ Yes i'm asking how to move the camera, @thedaian you're talking about tutorials, can you give me some link ? I don't how to google it... \$\endgroup\$ – Cyril Jun 3 '11 at 9:23
  • \$\begingroup\$ Search for something like 2d game tile map tutorial java, then it's a matter of looking through the 68,000 results. \$\endgroup\$ – thedaian Jun 3 '11 at 14:34

As was said in the comments, you need to handle a camera, which in 2d is just an offset for your render method.

For exemple if you had a call like
MyObject.draw(x, y);
You replace it with
MyObject.draw(x + offsetx, y + offsety);

Then all you need to do is to modify the offset when the user uses the mouse or when a unit is moved.


You need some sort of a viewport into the larger area. Then, when the viewable area is changed you will need to draw it based on what should be displayed there (you can obtain this information from a database, for example -- in particular, PostgreSQL has some specialized indexing features available that are optimized for the needed types of queries).

  PostgreSQL (free and open source)

You'll also want to make sure the areas that are no longer needed will not remain in memory for too long, otherwise a player who likes to explore with the "run 20 times faster" boots will fill up their computer's memory quickly.

One problem you might experience will be that as you load the tiles that are in view, the player will see blanks at first that get filled in shortly after. One strategy you can use to resolve this problem is to also load tiles that are slightly out of view; that way, when the player moves, those next tiles will already be pre-loaded while your code starts loading other slightly-out-of-view tiles in preparation for the next moves (no matter what direction the player goes since you can't accurately predict this anyway, especially if the player suddenly sees a dangerous giant spider that likes to eat speed-boosting-boots).

The beauty of using a database to load up on the nearby areas is that you have the option of increasing the size of your world far beyond 100k-by-100k.

For your database, you'll need to have two columns that represent the X and Y positions. If your player can move their character up (e.g., onto a rooftop) or down (e.g., into a dungeon), then you'll need to have a third column that represents the Z position as well.


If you're new to game development, I would recommend that you take a look at Tiled. Tiled is a free tile based level editor, that supports orthogonal and isometric map formats.

Tiled Page

Slick has a class, TiledMap, that can load the tmx format generated by Tiled. You can draw the background of the map simply with tiledmap and then render your units, building etc. on top of it.

You can find the examples in the Slick2d wiki: Tiled WIKI Page

  • \$\begingroup\$ Ah sorry, didn't see the comment about the camera... You shoul'd update your question to reflect that information in the question itself, otherwise it is a bit misleading. \$\endgroup\$ – tom van green Sep 5 '12 at 14:32

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.