Yup. I once had that very problem when I was simulating random icicles.
Lets say you have an x distance of 800. You'll decide on an x density for stars, and make a variable for that. Actually, lets use a #define. We might as well define a y density too, and matching offsets.
- Code: Select all
// in global code
int density_divisor = 8;
#define X_DENSITY view_width / density_divisor
#define Y_DENSITY view_height / density_divisor
// fancy division by 2 trick.. it always rounds to an integer
// 8 >> 1 is 4.
#define max_offset = density_divisor >> 1
We are going to be limited to a max of 8 in a row, and 8 in a column, but we wont always have 64 stars. Our max_offset is always smaller than our spacing determined by ?_DENSITY. This means we shouldnt have overlaps. By controlling max_offset, we can keep stars from being too close.
- Code: Select all
void placeStars() {
int randchance = 0;
int x_star_offset, y_star_offset;
for(i=0;i<X_DENSITY; i++) {
// we give each column chances to have stars. We dont actually place one yet.
for(j=0;j<Y_DENSITY;j++) {
if (randomStarHere() == 1) { // 50% chance? 1 in 5000? Only you and the function know...
x_star_offset = rand(0, max_offset); // offset from the grid...
y_star_offset = rand(0, max_offset); // but not enough to overlap the next
CreateActor("p_Large_star", "1", "(none)", "(none)", i*X_DENSITY+x_star_offset, j*Y_DENSITY+y_star_offset, true);
}
}
}
}
Unfortunately I cannot test it, but it should give you an idea. By controlling densities and max offsets, you can influence how the stars arrange themselves. If max offset is too small (or density is high), you will see stars arranged in a rough grid. Its random, but its a controlled random.