Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (847)
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  
  Loading a map chunk BEGINNER QUESTION  (Read 1606 times)
0 Members and 1 Guest are viewing this topic.
Offline 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;
   }
Offline trollwarrior1
« Reply #1 - Posted 2014-01-21 06:31:25 »

Don't use lists for tiles. Use arrays.
Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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
Offline 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
Offline 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.
Offline 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
Offline 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?
Offline 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 (367 views)
2018-06-10 19:43:48

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

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

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

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

nelsongames (857 views)
2018-04-24 18:14:32

ivj94 (1318 views)
2018-03-24 14:47:39

ivj94 (437 views)
2018-03-24 14:46:31

ivj94 (1100 views)
2018-03-24 14:43:53

Solater (449 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!