Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Moving around on a map?  (Read 7071 times)
0 Members and 1 Guest are viewing this topic.
Offline Regenuluz
« Posted 2012-03-24 14:44:58 »

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?)
Offline zyzz

Senior Newbie

« Reply #1 - Posted 2012-03-24 23:13:01 »

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)

Offline GabrielBailey74
« Reply #2 - Posted 2012-03-25 02:21:06 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Regenuluz
« Reply #3 - Posted 2012-03-25 09:16:19 »

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. :/


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 ).. ^^
Offline sproingie

JGO Kernel

Medals: 202

« Reply #4 - Posted 2012-03-25 17:54:51 »

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.
Offline Regenuluz
« Reply #5 - Posted 2012-03-26 09:13:39 »

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. Smiley Haven't had time to sit down and actually do it yet though. :/


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


Nice inline editing btw. <3
Offline R.D.

Senior Devvie

Medals: 2
Projects: 1

"For the last time, Hats ARE Awesome"

« Reply #6 - Posted 2012-03-26 13:07:13 »

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 Cheesy
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
Offline Regenuluz
« Reply #7 - Posted 2012-03-26 13:38:19 »

That's more or less what I'm doing right now. Smiley

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

Junior Devvie

Medals: 1

« Reply #8 - Posted 2012-04-05 13:14:40 »


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

Warning: Plz, excuse my bad english...
Offline Regenuluz
« Reply #9 - Posted 2012-04-05 13:29:55 »


I'll try and explain how I'm doing it. Smiley

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.

Pages: [1]
  ignore  |  Print  

hadezbladez (335 views)
2018-11-16 13:46:03

hadezbladez (177 views)
2018-11-16 13:41:33

hadezbladez (336 views)
2018-11-16 13:35:35

hadezbladez (82 views)
2018-11-16 13:32:03

EgonOlsen (2175 views)
2018-06-10 19:43:48

EgonOlsen (2203 views)
2018-06-10 19:43:44

EgonOlsen (1376 views)
2018-06-10 19:43:20

DesertCoockie (2008 views)
2018-05-13 18:23:11

nelsongames (1646 views)
2018-04-24 18:15:36

nelsongames (2297 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!