infinite 3d position generation

Started by
3 comments, last by nordwindranger 17 years ago
I'm writing a space game with c# and managed direct x when a fleet enters a new region of space, the vector3's for each ship in the fleet need to reset so that they are not all on top of each other. The problem is that a fleet can be any size, and needs to arrayed in some kind of formation (in other words not just a straight line). The other problem is that any number of other ships might also be in that area of space. I've figured out a couple of ideas for working this out, but they all rely on making some sort of spiraling pattern that keeps expanding around one center ship. I haven't actually wrote the code for this, because it seemed kind of silly. As ships are all different sizes, I would also have to take this into account. maybe its time to write some flocking behavior instead.. Ships don't really seem to move around enough to warrant spending the cpu time on this though (thats a side effect of using lasers, its not exactly sword fighting). I guess i don't really have a question per se, but if anyone has any insights into this type of problem, don't hesitate to send them my way.
Advertisement
how many ships you have? If it's not that big, I'd try to place them randomly inside a volume, and run a few iterations (say, 20, or until you have no intersections) of that algorythm.

bool Placeships(C_Sphip* ships[], int numShips, int maxIterations=30, float relaxation=0.85f){    for(int k = 0; k < maxIterations; k++)    {        bool bIntersect = false;        for(int i = 0; i < numShips; i++)        {            for(int j = i+1; j < numShips; j++)            {                // Delta position of the ships                Vector Delta = ships.m_Position - ships[j].m_Position;                // Check if ships are in 'collision' range                float d2 = Delta.DotProduct(Delta);                float r  = ships.m_InfluenceRadius - ships[j].m_InfluenceRadius;                float r2 = r*r;                // Nope, ships are at least 98% distance of their sphere of influence                if(d2 > r2 * (0.98f * 0.98f)) continue;                 float d = sqrt(d2);                // Calculate a vector to push spheres apart                Vector Push = Delta * ((r - d) / d) * relaxation;                // Use a weighted average. Largest ships move less, smallest move more                float invr_i = 1.0f / ships.m_InfluenceRadius;                float invr_j = 1.0f / ships[j].m_InfluenceRadius;                // Move ships apart using weights                ships.m_Position += Push * invr_i / (invr_i + invr_j);                ships[j].m_Position -= Push * invr_j / (invr_i + invr_j);                bIntersect = true;            }        }        if(!bIntersect) return true;    }    return bIntersect;}


[Edited by - oliii on March 28, 2007 8:09:17 AM]

Everything is better with Metal.

Do these ships have some kind of collision object, like a collision sphere? It is cheap to test on this if a ship is within the collision sphere of an other ship and how much.

This ofcourse doesn't apply if it is possible that on ship could dock with an other ship, but that is an other scenario.

Regards,

Xeile
As for flocking behaviour, yeah, that's feasable. Have a look at the good old Autonmous A.I. for that sort of stuff.

Everything is better with Metal.

yup each ship has a collision sphere.

thanks for all the tips guys. I'll get to work trying them out!

This topic is closed to new replies.

Advertisement