# Help generating planets at random position in 2D

Attention Topic was automatically imported from the old Question2Answer platform.

Hi

Can any of you give me a link to something that can help me generate planets (circles) at random positions? It’s in 2D.
I have tried googling but for some reason i’m not finding anything.
I want to create like 20 circles in an area, then some distance from that area i want to do the same again.
I’m not sure how to create the nodes dynamically or if the circles at all should have some special node-type attached.

Any one who can give me a link for a video or written documentation guidance.

Thanks.

You would first create a scene representing one planet. For simplicity, let’s assume it consists of only one node: a Sprite with the texture of a circle (like this).

Then you would create another scene. I’ll assume a Node2D here. It will represent your “universe”. Now attach the following script to it:

``````# load your planet scene
var planet = preload("res://Planet.tscn")

# do this 20 times
for i in range(20):
# create a new instance of the planet scene
var new_planet = planet.instance()

# set its global_position to two random (float)
# values lying somewhere between 0 and 400
new_planet.global_position.x = rand_range(0, 400)
new_planet.global_position.y = rand_range(0, 400)

# add it as a child of this "universe"-node
``````

Depending on where your planet-scene is located, how many planets you want to create and how big the considered area should be, you will have to adapt the code. In this form it’s probably still not working precisely as you imagined it, but it should give you a rough idea on how to tackle a problem like this.

Awesome!
Thanks.

That is just what i need to get an idea about where to start.

CuriousCoder | 2020-02-17 08:23

Thanks, I was making a similar game as well.

TitanTre3 | 2020-11-03 20:54