How do I progressively generate objects (Planets in space)

Get help using Construct 2

Post » Mon Jan 19, 2015 10:57 pm

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.
B
12
S
4
G
2
Posts: 132
Reputation: 1,749

Post » Tue Jan 20, 2015 12:19 am

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(-∞, ∞), random(-∞, ∞))' since ∞ 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.
Image

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.
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Tue Jan 20, 2015 8:57 pm

briggybros wrote: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.
B
12
S
4
G
2
Posts: 132
Reputation: 1,749


Return to How do I....?

Who is online

Users browsing this forum: guannstar, marcinkowski, Yahoo [Bot] and 15 guests