This project has moved and is read-only. For the latest updates, please go here.

Objects in Objects

Apr 16, 2009 at 6:09 AM
I would like to, once again, thank all of you for your work. Farseer physics is one if, if the the, most important building block in my current project. Everything is coming along nicely.

I was working on my map creation and have added a random element. Some objects start at random locations and from time to time objects, such as asteroids, are starting inside each other. Linked up, sometimes they "bounce" out and go flying like a crazy bat out of hell, and sometimes they stay stuck inside each other and just tweak out.

I was wondering if theres anyway I can check the simulation for collisions and easily move everything before the actual game play starts. I was thinking of checking all of the "OnCollision" events and moving objects accordingly, but then theres the chance they would be moved into another object. Or I could remove one of the colliders in each collision, but I was wondering if there is an easy way to "play out" the collisons, just to get each body/geom beside each other, then kill their "freak out" speeds gained from the unrealistic collision?

Is this practical or should I look into chaning my random placement a bit?
Apr 16, 2009 at 6:27 AM
What you can do is change your random placement algorithm to avoid placing asteroids near already placed ones. As far as doing this, you have two options: LINQ or a regular for loop. If you're working with extremely large lists (50000+), then you want the regular for. Otherwise, you should go with LINQ. Linq syntax goes something like this:

var distQuery = from vec in asteroidPositions where (Vector2.Distance(generatedPosition, vec) < avoidanceRadius) select vec;
//doing this because its easier to check a list for emptiness than an ienumerable
List<Vector2> tempList = (List<Vector2>)distQuery;
while(tempList.Count > 0) { [generate new number and rebuild the list] }

I think the syntax is cool, but that's just me. You can use a for loop, but i suggest the above code.
Apr 17, 2009 at 12:40 AM
I'm storing my asteroids in arrays, what I'm doing is doing a round randomly creating a bunch of small asteroids for the field, then going back and creating bases, big asteroids, and other large objects.

I guess I'm going to have to create the large objects first, then use code like what you suggested to make sure the small ones start away from the big ones.

Thanks for the idea :)