Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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 3061 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. :/

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

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. 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
Offline R.D.

Senior Member


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 Member


Medals: 1



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

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...  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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (28 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!