• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Chunk based space generation

T

TypicalHog

Guest
So, I need help with making chunk based generation for my game set in space.
I really don't know how to approach it.

Main issues:
-How to handle non-player objects like asteroids or projectiles leaving loaded chunks
-What should I use? An array/buffer/ds_map/something else.
 

Phil Strahl

Member
For mission-critical objects, e.g. the player/enemy craft, I would have some kind of "global coordinate system".

Say, your whole game space is 10000×10000 px in size, and each chunk (= room) is 1000×1000 px. The x and y values define the positions within this chunk, but additionally, you could calculate the global x and global y for each instance. If you're in column 4 all local x-coords need to be multiplied by this for the global coordinate, global_x = x + chunk_column * chunk_width, the same for y, etc. You get the picture.

Now, if an instance leaves the current chunk, you can still calculate its position, AI and what else you need. When you're about to switch chunks, store all mission-critical instances and their global positions, load the next chunk and re-create them, or better: Have them be persistent. Unless you're not storing and updating 1000s of instances, this shouldn't be a memory concern.

Effectively, you always have the state and position of critical instances in memory and when loading each chunk you decide which of them are actually inside. Disclaimer: Never tested this, it's just how I would try to do this :)
 
T

TypicalHog

Guest
For mission-critical objects, e.g. the player/enemy craft, I would have some kind of "global coordinate system".

Say, your whole game space is 10000×10000 px in size, and each chunk (= room) is 1000×1000 px. The x and y values define the positions within this chunk, but additionally, you could calculate the global x and global y for each instance. If you're in column 4 all local x-coords need to be multiplied by this for the global coordinate, global_x = x + chunk_column * chunk_width, the same for y, etc. You get the picture.

Now, if an instance leaves the current chunk, you can still calculate its position, AI and what else you need. When you're about to switch chunks, store all mission-critical instances and their global positions, load the next chunk and re-create them, or better: Have them be persistent. Unless you're not storing and updating 1000s of instances, this shouldn't be a memory concern.

Effectively, you always have the state and position of critical instances in memory and when loading each chunk you decide which of them are actually inside. Disclaimer: Never tested this, it's just how I would try to do this :)
I plan on having chunks of 1024x1024 on average with up to 10 objects in each chunk.
 

lolslayer

Member
Let me guess, every chunck is randomly generated and has a seed attended to it based on the location it is in, this way you can have an infinite universe and every time you go to the same location it will generate excactly the same data as when you went there before? :)
 
T

TypicalHog

Guest
Let me guess, every chunck is randomly generated and has a seed attended to it based on the location it is in, this way you can have an infinite universe and every time you go to the same location it will generate excactly the same data as when you went there before? :)
Exactly, and I only save chunks that have been altered to the disk. Does built in radnom functions give the same result on all Windows devices?
 

lolslayer

Member
Exactly, and I only save chunks that have been altered to the disk. Does built in radnom functions give the same result on all Windows devices?
You can assign a seed so the generation code will always generate the same with the same seed, just like minecraft, as long as the generation code keeps the same, you'll get the same results with the same seed
 
Top