Game theory: Putting units in formation

OK, I’m trying to make a game and something I want to implement is a simple way of putting units in formation.

I thought about 2 ways of doing this:

  1. Putting them in quadrants and separating them evenly across them.

Or 2) Making the distances between them same:

I have it down in theory, but not really sure how I can do it coding wise.

Any ideas?:hat:

PS. Formation AROUND the leader, or red circle.

EDIT: The more I think about it… I should just switch to tile-based…

You will be better off in a tilebased or gridbased environment. Grouping/formation is one of the game theories, that is often used with path-finding algorithms to give a good feel to the game. Formation is done with many different algorithms, and most programmers have their own approach to it. If you want to understand formation, then though it will seem very different, start with a carousel example…

Try messing around with this:

var vel: Point = new Point(target.x - pos.x, target.y - pos.y);
var velLength = vel.length();
vel.Normalize(characterSpeed);
if(velLength < 10){
vel.x = 0;
vel.y = 0;
}

and when updating the character increment it’s position by the vel aka velocity.

Do that for each of the units. You’ll notice this is just vector math.

Also if you want to study it. Just look at Boids. It’s a group of algorithms for flocking behavior. If you study it long enough it should make sense. You’ll end up with something like:
http://www.assaultwars.com/flash/AS3Boids/AS3Boids.html

It’s a simple set of algorithms but it takes a lot of understanding to use them right.

//here
//also since people doing AI ask about steering it’s crucial you understand the dot product of two vectors.
and here’s the algorithm basically once you understand dot product.

[quote=Sirisian;2330852]Try messing around with this:

var vel: Point = new Point(target.x - pos.x, target.y - pos.y);
var velLength = vel.length();
vel.Normalize(characterSpeed);
if(velLength < 10){
vel.x = 0;
vel.y = 0;
}

and when updating the character increment it’s position by the vel aka velocity.

Do that for each of the units. You’ll notice this is just vector math.

Also if you want to study it. Just look at Boids. It’s a group of algorithms for flocking behavior. If you study it long enough it should make sense. You’ll end up with something like:
http://www.assaultwars.com/flash/AS3Boids/AS3Boids.html

It’s a simple set of algorithms but it takes a lot of understanding to use them right.

//here
//also since people doing AI ask about steering it’s crucial you understand the dot product of two vectors.
and here’s the algorithm basically once you understand dot product.[/quote]

Thanks a ton. It looks like I have more work ahead of me than expected.

Cheers! :slight_smile: