Hey Codeproject,
I wasn't sure how to phrase the question as I am not sure what this is exactly called. Anyways, to quickly get to the point I have a list of entities (players, ships, whatnot) that I send to a specific point using:
GoToTarget(Entity Entity, Vector2 Position)
{
Entity.GoToPosition(Vector2);
}
The problem with this method is that they will eventually all get to the same place and stack on top of each other. Now I'd usually say that adding collisions would solve this problem, but it does not look neat - and neither is it suppose to happen in my game (no this is not a homework assignment because I do not do homework.)
So that's why I asked how to make them in a "formation", formation being a way they will be ordered next to the target position - as can be seen in (
)Age of Empires 2[
^]
I don't want any fancy formations, because that to me is irrelevant at this stage. I just want them list of entities to be formated by the origin that is "Target".
Edit:
Assuming we have eight entities. We want two rows, meaning there's four on each row:
int Rows = 2;
for(int i =0; i<rows; i++)
for(int x=0; x <Entities.Count / rows; x++)
{
Position = parOrigin - ((Entities.Count / 2 * new Vector2(Entities[i].Size.X, i * Entities[i].Size.Y)) + new Vector2(Entities[i].Size.X, 0) * i);
Entities[Entities.Count / Rows * i + x].GoToTarget(Position);
}
}
-- I think this is the solution? Feels like it in my head. Any comments?
Edit (15-5-2016):
Using the only answer currently given I still have problems getting them all to sort in a rectangle. Some of them still stack on top of each other.
What I have tried:
The following is pseudo code to explain my attempt at making a simple formation.
Vector2 parTarget = MousePosition;
Vector2 parOrigin = parTarget;
for(int i =0; i<entities.count;>{
parTarget = parOrigin - ((Entities.Count / 2 * new Vector2(Entities[i].Size.X, 0)) + new Vector2(Entities[i].Size.X, 0) * i);
if(Entities[i].Selected)
{
Entities[i].GoToPosition(parTarget);
}
}
As you can most likely read this in pseudo language should send them in a straight line having target as origin, but this does not give a rectangle formation and will thus spread them out over the entire map, and I do not want that.
I want the rectangle to be as small as necessary but I have no idea how to do this with only a one dimensional for loop. I'm not very good with math, so if this is obvious I do apologize - I am here to learn.