Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 MARS_999   Members   -  Reputation: 1279

Like
0Likes
Like

Posted 10 September 2012 - 08:04 PM

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!

Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8856

Like
1Likes
Like

Posted 10 September 2012 - 08:26 PM

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;
}

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Aressera   Members   -  Reputation: 1417

Like
0Likes
Like

Posted 10 September 2012 - 09:29 PM

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.

#4 MARS_999   Members   -  Reputation: 1279

Like
0Likes
Like

Posted 10 September 2012 - 10:08 PM

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!

#5 Ashaman73   Crossbones+   -  Reputation: 7442

Like
2Likes
Like

Posted 10 September 2012 - 11:40 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.

#6 Kaptein   Prime Members   -  Reputation: 2148

Like
1Likes
Like

Posted 11 September 2012 - 01:07 AM

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, 11 September 2012 - 01:12 AM.


#7 carangil   Members   -  Reputation: 490

Like
1Likes
Like

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 MARS_999   Members   -  Reputation: 1279

Like
0Likes
Like

Posted 12 September 2012 - 09:20 PM

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, 12 September 2012 - 10:32 PM.


#9 MARS_999   Members   -  Reputation: 1279

Like
0Likes
Like

Posted 13 September 2012 - 04:02 AM

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!

#10 MARS_999   Members   -  Reputation: 1279

Like
1Likes
Like

Posted 15 September 2012 - 07:23 PM

DracoLacertae, that worked well. So thank you!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS