Creating a circle-shaped asteroid field

Hello there! I’m new to Panda3d, this is my first program. I want to create a Solar System, with planets, moons, asteroids, etc.
Now I have problem creating the asteroid field that separates the inner Solar System and the outer one. I used a loop to create several asteroids at random positions (between parameters), but it doesn’t work. This code is just a try to make a part of the full circle.

    def astergen(self):
        params = [2000, 2100, 0, 100, -25, 25]
        af = 0
        while af != 500:
           x = randrange(params[0]-1, params[1])
           y = randrange(params[2]-1, params[3])
           z = randrange(params[4]+1, params[5])
           aster = loader.loadModel("models/Asteroid.bam")
           aster.reparentTo(render)
           aster.setPos(x, y, z)
           aster.setScale(30)
           if af == 100 or af == 200 or af == 300 or af == 400:
               params = [params[0]+50, params[1]+50, params[2]+100, params[3]+100, -25, 25]
           af = af+1




Can you help me out?

Hmm… Well, since you’re adding a linear amount to your parameters on each iteration, the result will indeed be linear.

To get a circle, I’d suggest using sine- and cos- functions. Specifically, the coordinates of a circle can be described as follows, if I recall correctly:

x = sin(angle) * radius
y = cos(angle) * radius

The value of “angle” could then be linearly increased from 0 to pi * 2 in order to describe a full circle, I believe.

Thank you for your help! I’ll try your solution.

1 Like

You mean that I increase angle from 0 to pi*2 with like, 10, or something? (Sorry if I’m stupid, I only started programming 3 years ago(I’m 15).

Not at all. I doubt that many of us start off knowing everything!

Sort of–except that pi * 2 equals 6.28…, meaning that an increase of 10 would far overshoot.

Exactly what value you use would depend on the results that you want. Right now you seem to be placing one asteroid per iteration, so you could then just divide pi * 2 by the number of iterations that you intend.

To clarify, just in case, what I’m saying is something like this:

import math

numAsteroids = 500
angularStep = math.pi * 2 / numIterations
angle = 0
for i in range(numIterations):
    # <code using sin and cos of the angle to calculate coordinates here>
    # <code to place an asteroid here>
    angle += angularStep

Thank you, again. It worked! Now I have an asteroid field of 3000 asteroids. (“Proceeds to make the outer solar system with its planets”)

1 Like