Sign in to follow this  
Burnhard

Appropriate procedural function for generation of an asteroid belt?

Recommended Posts

Burnhard    96
I'm no mathematician, so please bear with me. I'm interested to know if there is an appropriate function that would generate a random point in a torus, or perhaps in a ring. What I want to do is as much is possible provide an even spread of points and for no point to be repeated (ie. just using "rand" won't work). That's right, I want a pseudo random distribution within a torus or ring. I'm not sure if this is Cambridge Tripos level stuff or not. I want this in order to procedurally generate an asteroid field around a star. Any ideas? Thanks in advance.

Share this post


Link to post
Share on other sites
Cypher19    768
Get a random radius, r, within the desired threshold.

Get a random angle, theta (0-360, or 0-2pi).

Then just a simple polar to cartesian conversion...

XLoc = r * cos(theta);
YLoc = r * sin(theta);

and if you want some z, throw in a bit of randomization for that too. That's it.

Share this post


Link to post
Share on other sites
alfith    2337

#define NB_SLICES 360
#define MAIN_RADIUS 10 // "big" torus radius
#define SEC_RADIUS 1 // "small" torus radius

float rndSR, x, y, z, l;
int i, rndDisplay;

for (i = 0; i < NB_SLICES; i++)
{
rndDisplay = rand();
// only display some "points"
// no more than 1 point for each slice
if (rndDisplay > RAND_MAX / 2)
{
rndSR = 360.0f * rand() / (float)RAND_MAX;
l = SEC_RADIUS * cos(DEG2RAD(rndSR)) + MAIN_RADIUS;
z = SEC_RADIUS * sin(DEG2RAD(rndSR));
x = l * cos(DEG2RAD(i * 360.0f / NB_SLICES));
y = l * sin(DEG2RAD(i * 360.0f / NB_SLICES));
}
}



if you want more points for each slice, you can do this:

#define NB_SLICES 360
#define NB_SLICES2 60
#define MAIN_RADIUS 10 // "big" torus radius
#define SEC_RADIUS 1 // "small" torus radius

float rndSR, x, y, z, l;
int i, j, rndDisplay;

for (i = 0; i < NB_SLICES; i++)
{
rndDisplay = rand();
// only display some "slides"
if (rndDisplay > RAND_MAX / 2)
{
for (j = 0; j < NB_SLICES2; j++)
{
rndDisplay = rand();
// only display some "points"
if (rndDisplay > RAND_MAX / 3)
{
rndSR = j * 360.0f / NB_SLICES2;
l = SEC_RADIUS * cos(DEG2RAD(rndSR)) + MAIN_RADIUS;
z = SEC_RADIUS * sin(DEG2RAD(rndSR));
x = l * cos(DEG2RAD(i * 360.0f / NB_SLICES));
y = l * sin(DEG2RAD(i * 360.0f / NB_SLICES));
}
}
}
}


Share this post


Link to post
Share on other sites
JohnBolton    1372
One thing to watch out for is floating point precision. The radius of the asteroid belt is about 4x1011 meters. At that scale, the resolution of a 32-bit float is about 40 km, and the resolution of a 64-bit float is about 0.1 mm.

Anyway, to answer the original question...

The previous suggestions don't actually generate uniform distribution. In those algorithms, the density closer to the center will be higher than the density farther from the center. However, the belt is narrow enough that it probably won't be noticeable. The easiest way to generate uniform distribution in complex shapes (such as a torus) would be to generate random locations in a bounding box and reject the ones that are not in the shape, doing this until you have enough points.

Good random number generators typically generate sequences that do not repeat for many iterations (231 or better). The problem is that when you convert the output to your range, unique locations cannot be guaranteed. If you want to guarantee unique locations, you could maintain a sorted list of locations and check against it.

Share this post


Link to post
Share on other sites
zedzeek    529
another way (coincidently which i use for placing asteroids so theyre not to close or far apart ) is to place all the points in the area. + then iterate over all the points checking if 2 points aint to close together if they are then move them apart

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this