Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (769) Games in Android Showcase (230) 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
Pages: [1]
 ignore  |  Print
 Loading a map chunk BEGINNER QUESTION  (Read 1768 times) 0 Members and 1 Guest are viewing this topic.
jays

Junior Newbie

 « Posted 2014-01-21 03:28:31 »

Ok, i apologize if you have trouble understanding me, my biggest problem in this world is putting my thoughts into words or on paper, so that others can understand

i'm working on a 2d engine purely using the java Graphics class

right now i have an 2d array of tiles(originally arraylist but fixed), i'm wanting to get a 20 x 20 chunk out of the entire arraylist, based on a location

HERES THE ISSUE i've ran into a problem where the location given to load the chunks will also determine how big the chunk loaded is, which should be a 20x20 tile chunk, i'm guessing it has to do with the function trying to get tiles that arent there(nulls) for example if i wanted to grab a chunk from location 1x1 then it would only grab 10 tiles into the positive direction, making it only a 10x10 chunk

heres my code

 1  2  3  4  5  6  7  8  9  10  11  12 `public Tile[][] getChunk(Locatable locatable){      Location location = locatable.getLocation();      Tile[][] chunk = new Tile[20][20];      for(Tile tile : tiles){//tiles being the tiles array         int tileX = tile.getLocation().getX(), tileY = tile.getLocation().getY();         if(tileX < location.getX() + 10 && tileX > location.getX() -10               && tileY < location.getY() + 10 && tileY > location.getY() - 10){            chunk[tileX][tileY] = tile;         }      }      return chunk;   }`
trollwarrior1
 « Reply #1 - Posted 2014-01-21 06:31:25 »

Don't use lists for tiles. Use arrays.
Andre Lopes
 « Reply #2 - Posted 2014-01-21 12:02:49 »

Well if the tiled Area is a square/Cube.
You just point to the center of it and grab the tiles around in the % you want.

Like, if you want to get the chunk of the position 100x100, and you want to get 10x10,

You will get 95->105 x 95 ->105

Get it? I hope that helps.
StrideColossus
 « Reply #3 - Posted 2014-01-21 12:23:23 »

You didn't say what the problem was, but looking at the code I'm guessing it's an IndexOutOfBounds exception.

You're using tileX/Y as the array indices when you're inserting a tile into chunks, but they're the indices of the 'world' tiles array, instead they should be 0..20 range.

i.e. something like this:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `...for( int x = 0; x < 20; ++x ) {   // Calc world X coord   final int tx = location.getX() + x;   if( ( tx < 0 ) || ( tx >= width ) ) continue;   for( int y = 0; y < 20; ++y ) {      // Calc world Y coord      final int ty = location.getY() + y;      if( ( ty < 0 ) || ( ty >= height ) ) continue;      // Calc 1-D index into world array      final int idx = ty * width + tx;      chunk[ x ][ y ] = tiles[ idx ];   }}`

Any tiles at your location that are outside of the world will be null.

This assumes you know the width and height of the tiles array (easy to work out presumably from your loader).

It also has the added bonus that you don't need to iterate through the entire world tiles array to pick out the 20x20 you want.

Hope this helps.

- stride
jays

Junior Newbie

 « Reply #4 - Posted 2014-01-21 18:47:39 »

You didn't say what the problem was, but looking at the code I'm guessing it's an IndexOutOfBounds exception.

You're using tileX/Y as the array indices when you're inserting a tile into chunks, but they're the indices of the 'world' tiles array, instead they should be 0..20 range.

i.e. something like this:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `...for( int x = 0; x < 20; ++x ) {   // Calc world X coord   final int tx = location.getX() + x;   if( ( tx < 0 ) || ( tx >= width ) ) continue;   for( int y = 0; y < 20; ++y ) {      // Calc world Y coord      final int ty = location.getY() + y;      if( ( ty < 0 ) || ( ty >= height ) ) continue;      // Calc 1-D index into world array      final int idx = ty * width + tx;      chunk[ x ][ y ] = tiles[ idx ];   }}`

Any tiles at your location that are outside of the world will be null.

This assumes you know the width and height of the tiles array (easy to work out presumably from your loader).

It also has the added bonus that you don't need to iterate through the entire world tiles array to pick out the 20x20 you want.

Hope this helps.

- stride

ok thanks, this isn'nt dynamic but i should be able to find a work around

also note that im using a 3d array for chunks, chunk[1] being a 2d tile array

and i did point out the problem but not obviously, it is obvious now, ill get back to you when i have the time to put some work in
Opiop
 « Reply #5 - Posted 2014-01-21 19:35:37 »

First off, why 3D array when you only need two dimensioms of tiles?

Second, it is dynamic. Instead of using < 20, just replace 20 with a variable.
jays

Junior Newbie

 « Reply #6 - Posted 2014-01-21 21:01:09 »

First off, why 3D array when you only need two dimensioms of tiles?

Second, it is dynamic. Instead of using < 20, just replace 20 with a variable.

its either a 3d array or a chunk class that stores a 2d tile array, then i would have a 1d array of chunks, which would basically be the same as a 3d array of tiles wouldn't it not? the first dimension being the place keeper and the other 2 being the actual 2d array of tiles

if im wrong please correct me, i'm still a novice
Troubleshoots

JGO Knight

Medals: 36
Exp: 7-9 months

Damn maths.

 « Reply #7 - Posted 2014-01-21 21:17:43 »

First off, why 3D array when you only need two dimensioms of tiles?

Second, it is dynamic. Instead of using < 20, just replace 20 with a variable.

its either a 3d array or a chunk class that stores a 2d tile array, then i would have a 1d array of chunks, which would basically be the same as a 3d array of tiles wouldn't it not? the first dimension being the place keeper and the other 2 being the actual 2d array of tiles

if im wrong please correct me, i'm still a novice

Java is an object orientated language. If something is easier to use by using objects, then use them; that's what they're for. Creating a chunk class is definitely the better option since it'll be easier to manage and to debug.

My rule of thumb for when to create a class is if the following conditions are true:
• It's convenient to do so
• Each object has unique states but shared behaviours
• You will create multiple instances of the object

Why are all OpenGL tutorials written in Brainf**k?
jays

Junior Newbie

 « Reply #8 - Posted 2014-01-22 05:04:58 »

First off, why 3D array when you only need two dimensioms of tiles?

Second, it is dynamic. Instead of using < 20, just replace 20 with a variable.

its either a 3d array or a chunk class that stores a 2d tile array, then i would have a 1d array of chunks, which would basically be the same as a 3d array of tiles wouldn't it not? the first dimension being the place keeper and the other 2 being the actual 2d array of tiles

if im wrong please correct me, i'm still a novice

Java is an object orientated language. If something is easier to use by using objects, then use them; that's what they're for. Creating a chunk class is definitely the better option since it'll be easier to manage and to debug.

My rule of thumb for when to create a class is if the following conditions are true:
• It's convenient to do so
• Each object has unique states but shared behaviours
• You will create multiple instances of the object

ah thanks for clarifying, also realized creating a 3d array of tiles would be totally atrocious as the first dimension wouldn't be holding any tiles

will get back on this topic once i finished a decent map editor and parser for the map files
Pages: [1]
 ignore  |  Print

 EgonOlsen (1669 views) 2018-06-10 19:43:48 EgonOlsen (1706 views) 2018-06-10 19:43:44 EgonOlsen (1158 views) 2018-06-10 19:43:20 DesertCoockie (1585 views) 2018-05-13 18:23:11 nelsongames (1185 views) 2018-04-24 18:15:36 nelsongames (1709 views) 2018-04-24 18:14:32 ivj94 (2533 views) 2018-03-24 14:47:39 ivj94 (1758 views) 2018-03-24 14:46:31 ivj94 (2835 views) 2018-03-24 14:43:53 Solater (971 views) 2018-03-17 05:04:08
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 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