# How do I progressively generate objects (Planets in space)

0 favourites
• 3 posts
From the Asset Store
Several elements of the game interface hidden objects
• Simple question, probably not so simple answer.

How should I go about spawning objects in endless space?

This would allow me to spawn planets in endless space as well as enemies.

My goal is to create a simple space game with replay value because it's progressively generated/random and endless.

• So there are many fancy algorithms out there for generation. But I'll try to make a simple one work.

First you're going to want to randomly place your objects (planets and stuff) in your infinite level. There is your first problem, you cannot simply write 'Set Position (random(-8, 8), random(-8, 8))' since 8 is not a number. So what you could do is generate it relative to something else, the spaceship for example. You could give the spaceship 4 instance variables, maxX, maxY, minX and minY which represent how far the ship has travelled in any direction and when it get close to one of its boundries, simply generate more planets with say random(maxX + 100, maxY + 100) or something similar. So the universe is procedurally generated. This method is still not ideal as if done right it will take a lot of processing power the more the ship travels, and if done wrong will leave you with huge patches of empty space.

Now I don't know if you've ever played Minecraft, but we can learn something from its procedural generation methods. You can split your infinite universe into an infinite number of 'chunks' with finite volumes. When the ship gets close to ungenerated chunks it generates those chunks. You will need to store these chunks in an array and load them and unload them as you see fit. NOTE: Having too many objects loaded at a time will cause performance problems. You can create a variable called render distance, which takes an integer larger than that of half the screen, maybe double that. Then you can set up an equation for a circle using this render distance as a value. If any chunks fall into this circle load them (if they do not exist generate them), if there are chunks outside of this circle save them and remove them.

Generating a chunk

Say you want 2 planets in every 200 square pixels you can make your chunks the size of 200x200, then when you require to generate the chunk from the above method you can simply spawn 2 planets at any coordinates between 0 and 200 for both x and y. To place the planets properly the coordinates would be chunk.minX + genX and chunk.minY + genY. So the planets are only positioned relative to the chunk. The chunk has finite coordinates which can be assigned incrementally.

Then you can have different types of chunk etc to have variety where the generation properties are different and then conditions about where they can spawn etc. It depends on what level of complexity you want.

I hope this garbled explanation helps in some way.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• So there are many fancy algorithms out there for generation. But I'll try to make a simple one work..

Wow, thank you for the thorough response. You even included a diagram haha!

I will have to play around with the theory this evening but I'm afraid I might not be knowledgeable enough to implement the idea. I get it, but I'm not sure I know how to make it function.

I think that the simple idea of generating objects the further you move from the "origin" of spawn is intriguing. I understand the part about leaving blank space vs consuming large amounts of CPU so I thought it might be neat to implement a mechanic like the old MS game "Ski Free" where you just die if you go too far haha. I could load test where "too far" would be and then just back it up with a simple story so players turn back.

• 3 posts