Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
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  
  [Solved] Rendering Tiles from an Array of Chunks  (Read 881 times)
0 Members and 1 Guest are viewing this topic.
Offline dissonance

Senior Newbie





« Posted 2013-01-08 03:06:12 »

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!
Offline chrisethompson

Senior Newbie


Medals: 1
Exp: 5 years



« Reply #1 - Posted 2013-01-09 19:24:28 »

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
Offline dissonance

Senior Newbie





« Reply #2 - Posted 2013-01-10 01:11:01 »

Thanks for the reply! I'm going to try what you said and I'll post my results.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dissonance

Senior Newbie





« Reply #3 - Posted 2013-01-16 07:54:38 »

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

Chris

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!
Online HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2013-01-16 09:31:39 »


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

Offline dissonance

Senior Newbie





« Reply #5 - Posted 2013-01-17 01:15:45 »

Ah, okay, I'll just make that change. Thanks!
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.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!