Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 ?

Thanks.

share|improve this question
2  
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. –  thedaian Jun 2 '11 at 17:16
    
Are you asking on how to move the camera? –  Tetrad Jun 2 '11 at 18:57
    
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... –  Cyril Jun 3 '11 at 9:23
    
Search for something like 2d game tile map tutorial java, then it's a matter of looking through the 68,000 results. –  thedaian Jun 3 '11 at 14:34
add comment

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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)
  http://www.postgresql.org/

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.

share|improve this answer
add comment

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

share|improve this answer
    
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. –  tom van green Sep 5 '12 at 14:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.