Skip to main content
gamedev.net
🔒 Locked

Group formation problem

Started by hello_there 5 replies 1k views
Original Post
hello_there
hello_there
I'm making an RTS game and i want to be able to have group formations i have written the code under and it works when the Angle is 0 and 180 but does for any other angle. The angle determines what way the formation faces. I'm pretty sure it's to do with the Z value. Can someone please tell me how i would change it to make it work. I've tried different ways but i can't seem to get it to work. void CUnitController::GetDestinations(CVector MouseClickedAt) { int RowSize = 0; double Radius = 0; double Diameter = 0; int Counter = 0; int NoSelectedUnits = 0; double Angle = 0; int RowCounter = 0; // Get the biggest radius for( ulint i = 0; i < MaxSize; i++ ) if(IsFull && Units->IsSelected()) { if(Units->Radius > Radius) Radius = Units->Radius; NoSelectedUnits++; } RowSize = int(sqrt(NoSelectedUnits)*1.5); if(NoSelectedUnits < RowSize) RowSize = NoSelectedUnits; Diameter = 2*Radius; double SinTemp = sin(Angle + (PI/2)); double CosTemp = cos(Angle); for( ulint i = 0; i < MaxSize; i++ ) if(IsFull && Units->IsSelected()) { if(Units == NULL)throw "Problem In CUnitController Class"; double X = MouseClickedAt.x + (SinTemp*((Counter-(RowSize/2))*Diameter)); double Z = MouseClickedAt.z + (CosTemp*Diameter*RowCounter); Units->SetDestination(CVector(X,MouseClickedAt.y,Z)); Counter++; // If Counter = RowSize move down to next row if(Counter == RowSize) { RowCounter++; Counter -= RowSize; } } }
____________How could hell be worse?
Raduprv
Raduprv
I don't think this is the way to do it.
Just select a formation leader (can be virtual or real) and make the units in that formation follow that leader. Otheriwse they might split if they encounter a problem in the path (for example, on a narrow bridge, some units will go on the bridge, while the others are going around it).
If the leader unit dies, assign a different leader.
wendigo23
wendigo23
One thing I'll point out is that

double SinTemp = sin(Angle + (PI/2));
double CosTemp = cos(Angle);

are the same value. Is this your intention?
hello_there
hello_there
I was trying stuff and that worked i forgot to check why. How would I workout the Z value though.

double SinTemp = sin(Angle + (PI/2));
double CosTemp = cos(Angle);

hmm good point about making a leader. I havn't got up to doing pathfinding yet. I might modify if to have a leader after.
____________How could hell be worse?
hello_there
hello_there
Yeah your probably right.
____________How could hell be worse?
IADaveMark
IADaveMark
There are many schools of thought about moving formations or even just loose groups of units.

If you choose the follow-the-leader approach strictly (no station-keeping) then you end up with the "boat wake" look where everyone is strung out behind the lead dude.

If you choose the station-keeping (formation-based) approach, you have to store each unit's desired relative position to the leader and then move toward that. (e.g. I'm supposed to be 20 units straight off his left side - get there!)

The problem with both approaches is that it gets really silly in some situations. For example, imagine 3 paralell bridges. If the leader chooses any one of them, they may ALL try to go over that one instead of dividing up across all three of them.

Another problem that needs to be addressed is units of different sizes. If the leader is a person but you have tanks, etc. in there, the leader could go where the tanks could not. You have to account for the size of the largest unit in the group. Same can be said for speeds of a mixed group. If the leader is fast and there are slower units, they will get left behind.

There is a lot of information floating around about these issues. I suggest looking in (and moving this thread to) the AI board.
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.