Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (780) Games in Android Showcase (233) games submitted by our members Games in WIP (857) 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
 How to create highly scalable worlds  (Read 5599 times) 0 Members and 1 Guest are viewing this topic.
HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

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

 « Posted 2012-09-08 22:01:05 »

Introduction:

Well, I think it is time for me to put up a tutorial, and seeing that there are none on 'almost' infinite worlds, I have decided that I would make one.

IMPORTANT: READ THIS SECOND (after the introduction):

- This code may not be perfect, as I am not typing this into Eclipse and compiling it. If anyone points out an error, I will fix it as soon as possible.
- Also, this code may not be the best practice. It works, but it may not work well.
- There will be some parts missing, as I have not completely worked out how to do some of these things, but if anyone could give me help, I would be very thankful.

Now, for the tutorial

Firstly, you will need a Utility class:

Vector2Long (or course, you may use another implementation as long as it has all the required functions!)

(if you happen to not want the biggest world possible, just change everything into an Integer)

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50 `public class Vector2Long{   public long x, y;      public Vector2Long()   {      x = y = 0L;   }      public Vector2Long(long x, long y)   {      this.x = x;      this.y = y;   }      public Vector2Long(Vector2Long vec)   {      this.x = vec.x;      this.y = vec.y;   }      public boolean equals(Object obj)   {      if(obj == null)         return false;      if(obj == this)         return true;      if(obj.getClass() != this.getClass())         return false;      Vector2Long other = (Vector2Long) obj;      if(this.x == other.x && this.y == other.y)         return true;      return false;   }      public int hashCode()   {      final int prime = 31;      int hashcode = 1;      hashcode = prime*hashcode + x;      hashcode = prime*hashcode + y;            return hashcode;   }      public String toString()   {      return x + "," + y;   }}`

Make sure that your implementation overrides hashcode and equals in a similar way to the above, otherwise this will fail!

Secondly, you will need 3 classes: (Rename them if you wish)

• World.java
• Chunk.java
• Tile.java

Firstly, set up a 'Tile':

 1  2  3  4  5  6  7 `public class Tile(){   public Tile()   {      }}`

The Tile class can be given functions as needed, but all you need is that (and a function to get Texture Coords if using OpenGL)

Now, for the Chunk:

 1  2  3  4  5  6  7  8  9  10  11  12  13 `public class Chunk(){   private static final int size = World.chunksize;   private Vector2Long chunkPos;      private HashMap tiles = new HashMap();   public Chunk(Vector2Long pos)   {      this.chunkPos = pos;   }}`

This stores a hashmap of tiles that can be retrieved with a Vector2Int.
It also stores it's position in the world and it's size (which is static anyway).

Finally, the World:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `public class World{   public static final int chunksize = 16; // I use 16 because it's an easier size to work with   public final long worldsize;      private HashMap chunks = new HashMap();      public World()   {      this.worldsize = Long.MAX_VALUE;   }      public World(long size)   {      this.worldsize = size;   }}`

The world just stores a bunch of chunks. Dynamically loading/unloading chunks is up to you depending on what you world/game is like.

So there, You now have the framework for an (almost) infinite world!

(Coming Soon: How to change Tiles in the world, how to render with LWJGL (per Chunk or full World), 3D Worlds, and Entities)

But now I have to go back to work on The Gift/Curse of Evolution...

matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #1 - Posted 2012-09-09 08:54:00 »

This is pretty good. I have myself written a game somehow similar to minecraft or something like that and this is also propably the way minecraft does it.

So: Awesome work!
(Never thought about storing the chunks inside a hasmap... I always thought about linked chunks... so every chunk knows his neighbours... but hashmap is better!)

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Best Username Ever

Junior Devvie

 « Reply #2 - Posted 2012-09-09 19:44:49 »

Since it's a WIP, then I can't judge what the rest of the tutorial might be. Using longs for coordinates is a little disappointing. It might be interesting to have BigInteger, Strings, byte arrays, or something else could be interesting. Though, realistically, you probably won't visit 16 * 2 ^ 63 tiles in one sitting. Linked list style structures are interesting, too. You could use a graph instead of a square grid then. Square grids might not need them (unless you had teleportation or something, then it would help a lot), but it would make it easier to "forget" chunks to save memory since it would automatically handle reachability tests and garbage collect the chunks.
 Games published by our own members! Check 'em out!
Riven
Administrator

« JGO Overlord »

Medals: 1357
Projects: 4
Exp: 16 years

Hand over your head.

 « Reply #3 - Posted 2012-09-10 00:14:27 »

The Article & tutorials board is not a place for WIPs. As mentioned in the board listing, this sub-forum is moderated more intensely than others. That you have other things to do is, fine, but until then this topic is moved to Misc.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

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

 « Reply #4 - Posted 2012-09-10 05:30:31 »

Ok, sorry.

I was hoping to finish it yesterday, but then other stuff happened...

As for BigDecimals, I didn't even think about that, I will change it when I have enough time.

I will have a look into LinkedLists, but I an used to using Hashmaps, so unless there is a huge benefit, I will stick to what I know.

Best Username Ever

Junior Devvie

 « Reply #5 - Posted 2012-09-10 21:37:12 »

I will have a look into LinkedLists, but I an used to using Hashmaps, so unless there is a huge benefit, I will stick to what I know.

No, linked lists, not LinkedList. Here's an example.

 1  2  3  4  5  6 `public class Zone{  private Zone north, south, east, west;  private Zone portals[];  // ...}`

I was just thinking aloud about it, though.
matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #6 - Posted 2012-09-11 12:39:40 »

I will have a look into LinkedLists, but I an used to using Hashmaps, so unless there is a huge benefit, I will stick to what I know.

No, linked lists, not LinkedList. Here's an example.

 1  2  3  4  5  6 `public class Zone{  private Zone north, south, east, west;  private Zone portals[];  // ...}`

I was just thinking aloud about it, though.

Yes this was the idea... but you would need to have a root-zone (in my case I name them "chunks" (like minecraft)), and what should you do if that one gets unloaded? Which zone will be the next? and how do you want to save it?

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Best Username Ever

Junior Devvie

 « Reply #7 - Posted 2012-09-12 04:11:39 »

I would just keep a reference to whatever zone the player is in. Or the camera and player if they're separable.
matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #8 - Posted 2012-09-12 17:44:34 »

I would just keep a reference to whatever zone the player is in. Or the camera and player if they're separable.
What if the player falls into an not-loaded chunk (happens often in minecraft :D)

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Best Username Ever

Junior Devvie

 « Reply #9 - Posted 2012-09-12 22:49:30 »

That's a problem that has to be solved by preloading. It does not matter how neighboring nodes are accessed.
Pages: [1]
 ignore  |  Print

 hadezbladez (739 views) 2018-11-16 13:46:03 hadezbladez (379 views) 2018-11-16 13:41:33 hadezbladez (729 views) 2018-11-16 13:35:35 hadezbladez (191 views) 2018-11-16 13:32:03 EgonOlsen (2388 views) 2018-06-10 19:43:48 EgonOlsen (2550 views) 2018-06-10 19:43:44 EgonOlsen (1477 views) 2018-06-10 19:43:20 DesertCoockie (2141 views) 2018-05-13 18:23:11 nelsongames (1945 views) 2018-04-24 18:15:36 nelsongames (2624 views) 2018-04-24 18:14:32
 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