Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 How to create highly scalable worlds  (Read 2784 times) 0 Members and 1 Guest are viewing this topic.
HeroesGraveDev

JGO Kernel

Medals: 352
Projects: 11
Exp: 3 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;   }}

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: 121
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

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.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 949
Projects: 4
Exp: 16 years

 « 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: 352
Projects: 11
Exp: 3 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.

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.

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

 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

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: 121
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

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

You cannot reply to this message, because it is very, very old.

 theagentd (13 views) 2015-03-27 23:08:20 wxwsk8er (54 views) 2015-03-20 15:39:46 Fairy Tailz (47 views) 2015-03-15 21:52:20 Olo (29 views) 2015-03-13 17:51:59 Olo (32 views) 2015-03-13 17:50:51 Olo (39 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (57 views) 2015-03-10 11:36:42 ClaasJG (42 views) 2015-03-10 11:33:01 Pippogeek (50 views) 2015-03-05 14:36:23
 BurntPizza 21x LiquidNitrogen 21x basil_ 19x EgonOlsen 17x theagentd 17x KevinWorkman 16x Roquen 16x 65K 11x wessles 11x Varkas 10x Riven 10x princec 9x phu004 8x SHC 8x Ashedragon 8x lcass 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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