# Random location of space objects how to keep a specified min distance?

This topic is 2108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am currenlty randomly generating positions for my space objects. e.g. asteroids, planets, ect...

What I have so far is a distance check, but I think I need some kind of STL algorithm or something that automates running through a vector or list of obejcts over and over until all objects have been tested against each other....

Any ideas?

Thanks!

##### Share on other sites
This is actually an interesting problem if you want to solve it efficiently. There is a trivial method but which takes O(n^2) time (generating each object at random and checking against all existing ones until no collision occurs). Another, smarter method is to generate an AABB and subdivide it at random (making sure each subdivision has a minimum volume) generating some form of tree structure, and once you're subdivided enough, place planets at random in any of the leafs - guaranteed to be collision-free, but since you use AABB's some patterns can appear.

Of course, if you don't care about performance, and if there is no STL method for doing this, you can wrap the naive algorithm around a vector relatively easily if you have the generation method and the collision method. Here's an example with arrays, since I don't do C++...

 // generates a random, collision-free position for all objects for (int t = 0; t < objectCount; t++) { do { objects[t].randomizePosition(); } while (Collides(t)); } // checks collision of object at index t with all previous objects bool Collides(int t) { for (int k = 0; k < t; k++) if (collision(objects[t], objects[k])) return true; return false; } 

##### Share on other sites
Have you thought about approximating solar system formation?

I've been thinking about how to do this in my upcoming space simulator. I plan on starting with a density distribution of materials and where they exist relative to the host star in the accretion disc - this will probably be randomly generated within certain parameters (dense stuff near the star). I'll probably figure out some kind of clumping algorithm that will form orbiting bodies wherever there are local maxima in the material density. Finally, I'll use a distance check to see if there are any objects that cross or come near to each others orbits, these objects will be assumed to collide at some point and so I either combine the materials from each into a single body or produce many small bodies, the result of an explosive collision. This algorithm continues until the distance between the orbits is greater than some threshold.

This should produce results that if not accurate are at least plausible based on our current understanding of how solar systems form. If you're not going for realism, the AABB method discussed by Bacterius seems like a good way to do it.

##### Share on other sites
Yeah I thought about just using bounding spheres, also but wasn't sure if there was anything in the STL algorithm lib that would do some testing and sort of std::pair<Obj*, pos> an iterate over the pairs until they were at a minimum distance from each other....

Good ideas guys! I will try the bounding volumes first....

Thanks!

##### Share on other sites
Some thoughts about my terrain generation (large objects). I subdivide the area into cells (+ some spacing between cells). Each cell can contain one randomly placed object, the spacing between the objects guarantees, that you don't get overlapping objects. This is similiar to the AABB approach. Even better would be a voronio cell generation which will break up patterns more. You can place your objects randomly within the boundaries of a voronio cell to avoid intersection testing.

##### Share on other sites
all of the suggestions above are slow, or wrong
poisson-disc is one array lookup and possibly an &
google it, im sure it works in 3d as well, and it does (EDIT): http://devmag.org.za/2009/05/03/poisson-disk-sampling/ Edited by Kaptein

##### Share on other sites
Some thoughts about my terrain generation (large objects). I subdivide the area into cells (+ some spacing between cells). Each cell can contain one randomly placed object, the spacing between the objects guarantees, that you don't get overlapping objects. This is similiar to the AABB approach. Even better would be a voronio cell generation which will break up patterns more. You can place your objects randomly within the boundaries of a voronio cell to avoid intersection testing.[/quote]

I like this idea, and it can be made very simple and fast.

Suppose the minimum distance you want is 'd': No two object closer than 'd' points from each other. Store created objects on a sparse grid discritized at intervals of 'd.' A hashtable can handle the sparse grid at both storage and time O(n), where n is number of objects.

 ht = new HashTable(); //key for the hashtable is a triplet of integer coordinates , value is the object stored for (a=0; a< numobjects; a++) { bool reject = true; while (reject == true) { vec3 objectpos = random_position_in_3d(); //quantize down to a discreet grid coordinates, which are just increments of 'd' int x = objectpos.x/d; int y = objectpos.y/d; int z = objectpos.z/d; reject = false; // now check this spot in the grid, and all the immediate neighbors for (i=-1;i<=1;i++) { for (j=0;j<=1;j++) { for (k=0;k<=k;k++) { if ( obj2 = ht.get( new int_triplet(x+i, y+j, z+k ) ) //if there's on object here, it may be within 'd' of the new object { if (distance (objectpos , obj2.pos) < d) reject = true; } } } } } //loop back and pick a new position if we rejected //this should be a rare occurance, unless the RNG is bad, OR you are putting in so many objects the grid is filling up someobject obj = new someobject( objectpos, "some star"); ht.put(new int_triplet(x,y,z) , obj); } 

Note the above will only work well if the space is very sparse, and you want random points with a respected minimum distance. If you want something more uniformly spaced, use the poisson-disc mentioned above.

edit: adjust the code a bit
edit: give up getting the braces to line up; they really do look straight in the editor! Edited by DracoLacertae

##### Share on other sites
Ok the poisson-disc method sounds interesting... Anyone know of a lib that has a built in generator for this? Or examples? I really don't want to code this up when it should be a standard lib tool by now.... C++11 have something for this? And I would like to have negative values if possible? Reason is my Sun sits at 0,0,0 and looking down in a standard math grid left is negative and right positive ect... I want to position them with e.g. -1024, -1024 or 1024,1024

And from what I seen so far if I have a lamda value of say 1000 the numbers all return around 1000.... So how can I do position e.g. 20 objects around a 2048,2048 grid so they all don't sit around 1000?

Thanks! Edited by MARS_999

##### Share on other sites
DracoLacertae

Ok I think I have something in theory that works, but need to check it out later tonight. I will post back once I verify the code is working.

Thanks!

##### Share on other sites
DracoLacertae, that worked well. So thank you!

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631425
• Total Posts
3000015
×