[Solved] Rendering Tiles from an Array of Chunks

I have a project I’m working on using Slick2D. Now, I have a enum for my Tiles, nothing too fancy, it simply has what image the tiles should use and the scale. I then have another class to generate chunks. This class will store the data in a 2D Tile array which I had no problem rendering and moving about. Now to the tricky part, I have a world class that I am using to store entities in an ArrayList and then a 2D Chunk array. Now, in my game state I initialize the world and fill it with chunks, my question is how would I go about rendering the tiles?

When rendering a chunk by itself without the world, I have a getTileImage method that grabs the tile image based on the pixel on the screen, my problem is now I have to figure out what chunk the pixel is in and then also figure out the tile within that chunk. I’m not exactly sure if this is the best way to go about this so if you have a recommendation I will gladly listen. Also, if you need any of my code let me know, I am still relatively new to Java so it might not be pretty.

Thanks!

Not sure I understand the question exactly, but I would go about it like this. Let’s say your chunks are 32x32 tiles and your world is made up of 100x100 chunks. You have an area of 3200x3200 to explore.

Let’s say you know that the character is at tile 1000,2000 - the starting point. You get the chunk that tile is in by Math.floor(1000/32) and Math.floor(2000/32). Those are the coordinates of the chunk.

You can go in the other direction too. If you know a tile is at 10,10 in a chunk then its world coordinate is 32*chunkCoordinate + 10.

Note that the coordinates go from 0 - 31 for a chunk with 32x32 tiles.

For rendering you can test if any of a chunk is on the screen before you render it. This saves you from rendering things that are not visible. Do this check at the chunk level. Then for chunks that are partially on the screen run the check again at the tile level. To improve performance just grab the chunk coordinates around the player to do the tests, rather than all of them.

For debugging, I find it is helpful to insert some code to color the corners of the chunk so that you can see that everything lines up well. You remove the coloring once it works. In fact you can render dummy chunks with just corner tiles to test this out.

Ask more questions if that does not help or if I misunderstood the question.

Chris

Thanks for the reply! I’m going to try what you said and I’ll post my results.

[quote=“chrisethompson,post:2,topic:40654”]
Thanks, worked out well. Now it’s just a matter of me only rendering the chunks within view, but I’m sure I’ll figure that out. Thanks again!

You might want the 32 to be a floating point number (32f, 32d, 32.0 etc.) because it is actually passing 1000/32 as an int, not a float. Therefore making Math.floor() useless and giving you the wrong value for negative coordinates.

Ah, okay, I’ll just make that change. Thanks!