Random location of space objects how to keep a specified min distance?
#1 GDNet+ - Reputation: 859
Posted 10 September 2012 - 08:04 PM
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!
#2 Crossbones+ - Reputation: 3861
Posted 10 September 2012 - 08:26 PM
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;
}
#3 Members - Reputation: 922
Posted 10 September 2012 - 09:29 PM
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.
#4 GDNet+ - Reputation: 859
Posted 10 September 2012 - 10:08 PM
Good ideas guys! I will try the bounding volumes first....
Thanks!
#5 Members - Reputation: 4742
Posted 10 September 2012 - 11:40 PM
My game: Gnoblins
Developer journal about Gnoblins
Small goodies: Simple alpha transparency in deferred shader
#6 Members - Reputation: 695
Posted 11 September 2012 - 01:07 AM
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, 11 September 2012 - 01:12 AM.
#7 Members - Reputation: 405
Posted 11 September 2012 - 01:10 PM
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.
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, 11 September 2012 - 02:13 PM.
#8 GDNet+ - Reputation: 859
Posted 12 September 2012 - 09:20 PM
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, 12 September 2012 - 10:32 PM.






