Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 2559 times)
0 Members and 1 Guest are viewing this topic.
Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 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<Vector2Long, Tile> tiles = new HashMap<Vector2Long, Tile>();

   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<Vector2Long, Chunk> chunks = new HashMap<Vector2Long, Chunk>();
   
   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...

Offline matheus23

JGO Kernel


Medals: 106
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! Smiley
(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
Offline Best Username Ever

Junior Member





« 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!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
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
Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 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.

Offline Best Username Ever

Junior Member





« 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. Smiley 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.
Offline matheus23

JGO Kernel


Medals: 106
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. Smiley 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
Offline Best Username Ever

Junior Member





« 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.
Offline matheus23

JGO Kernel


Medals: 106
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 Cheesy:D)

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

Junior Member





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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (67 views)
2014-08-11 02:49:23

BurntPizza (45 views)
2014-08-09 21:09:32

BurntPizza (36 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!