Moving around on a map?

Okay, so moving around on the screen isn’t any problem.

The problem is that I can’t quite figure out a nice way of having a player move around on a large map and only viewing some small portion of it.

Say the map consists of 500x500 tiles, each 32x32 pixels. How do I then render the map area that surrounds the player at his given (x, y)-coordinates (in pixels)? (The viewport is… dunno, 800x600?)

you’d just render the whole map as you normally would and move the ‘map’ (so to speak) whenever your player ‘moves’

in essence: your player doesn’t move, but the map does (creates an illusion of player movement)

I’ve done that once, being based off of the players direction, you can use java Graphics2D method “g2d.rotate()” or “g2d.translate()”.

Believe I also had to add other variables to act as the actual increasers / decreasers of the perams in those methods.

Wouldn’t rendering the entire map cause a heck of a lot of overhead? I mean that’ll be a picture containing 256000000 pixels, where only 480000 of 'em will be shown, around 1/533 part of the image. :confused:

EDIT:

Just did a little more math, if 1px is a 32 bit integer, then we have 32 bits * 256000000 pixels… ~976mb… (256000000px * 32bits / 8 / 1024 / 1024) Where the viewport is only a little less than 2mb (480000 * 32 / 8 / 1024 / 1024 )… ^^

The parts that aren’t visible are pretty close to free. You should still only bother rendering the parts of the map that are at least near your viewport though – if it’s a tile-based map, the culling isn’t all that difficult to figure out.

The Slick2D source has some examples, one of which is for a overhead scrolling map. Something to do with tanks I think – you’ll have to browse through the source to find it.

Almost free? If I render the entire picture, even if it’s only showing part of it, the rest of the image will be in the buffer xD But yeah, I was after some culling of the tiles. I think I pretty much figured it out, shortly after asking in here though. :slight_smile: Haven’t had time to sit down and actually do it yet though. :confused:

EDIT:

So, I have it working now. 300+ fps, if I don’t restrict it. But, I just played around a little, and made each tile have it’s own small buffered image, and needless to say, that didn’t work out for a map of 500x500 tiles. It ran out of heap memory. :stuck_out_tongue: So currently a tile only holds it’s own position and color. (It probably doesn’t even need to have it’s own position though)

I’m rendering enough tiles to cover the screen plus 1 row on each side, just to make sure the screen always stays covered. Yay.

EDIT EDIT:

Nice inline editing btw. <3

You should rather use a TextureAtlas for something like this. Most common way is the “Rpg-Maker”-way. Have an 2D-Array with id which point to a tile of spritesheet. When rendering you only need to draw the little quad the id is pointing to.
For culling I use a camera. I just take the cam position and divide it by the tilesize (for smooth scrolling) for entites just draw to where you want but let the cam translate to it current view.
This way I don’t need to care about adding values to a entity’s position :smiley:
For the map rendering I just take the tiles inside the view rectangle of the cam (x,y, window width, window height). and set minus 2 for x and y and plus 2 for width and height.
Something like this xD

That’s more or less what I’m doing right now. :slight_smile:

Need to refactor the code now, so it’s nice to look at and easier to expand on.

Hello,

There is something i am not still getting, and its the fact that moving the map instead of the character represents making iterations over all the visible tiles (something that has been imposible for me to achieve personally because my game loop has a 12 fps rate and the calculations for moving each individually tile gets truncated).

i tried attatching all the tiles on a blank bufferedimage, and just move that image, and it worked. The problem i had with this was when it came the time to draw new tiles to the composition and dispose old ones…

plz, help me a little… :’(

^^

I’ll try and explain how I’m doing it. :slight_smile:

My map is a 2d array of Tiles of 32x32 pixels. So far the Tile just knows what kind of tile it is and it’s (x,y)-coords in tiles, so that (0,0) is the top left tile. When I “move” the player around, I just add or substract to his (x,y)-coords, which is stored in pixels.

So when it comes to rendering the portion of the map that the player can see in the screen, I convert his (x,y)-coords to the tile he’s standing on(In my case, divide the x and y-axis with 32). This will be the center tile of the screen, next you just need to fill in the rest of the tiles.