# Game theory: Putting units in formation

**URL:** <https://forum.kirupa.com/t/game-theory-putting-units-in-formation/261248>\
**Category:** programming\
**Created:** [May 24, 2008, 11:30pm UTC](https://forum.kirupa.com/t/game-theory-putting-units-in-formation/261248 "2008-05-24T23:30:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zuperxtreme](https://avatars.discourse-cdn.com/v4/letter/z/e9a140/32.png) [@zuperxtreme](https://forum.kirupa.com/u/zuperxtreme)\
**Post date:** [May 24, 2008, 11:30pm UTC](https://forum.kirupa.com/t/game-theory-putting-units-in-formation/261248/1 "2008-05-24T23:30:13Z")

</div>

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.

![](http://forum.kirupa.com/uploads/kirupa/4755/7fc6b2f5192a4e30.jpg)

Or 2) Making the distances between them same:

![](http://forum.kirupa.com/uploads/kirupa/4756/bb18abd18b01becc.jpg)

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…

---

<div class="post-metadata">

**Author:** ![bluemagica](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bluemagica](https://forum.kirupa.com/u/bluemagica)\
**Post date:** [May 25, 2008, 4:54am UTC](https://forum.kirupa.com/t/game-theory-putting-units-in-formation/261248/2 "2008-05-25T04:54:28Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Sirisian](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@Sirisian](https://forum.kirupa.com/u/Sirisian)\
**Post date:** [May 25, 2008, 6:28am UTC](https://forum.kirupa.com/t/game-theory-putting-units-in-formation/261248/3 "2008-05-25T06:28:29Z")

</div>

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](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](http://www.red3d.com/cwr/boids/)  
//also since people doing AI ask about steering it’s crucial you understand the [dot product of two vectors.](http://www.kirupa.com/forum/showpost.php?p=2216146&postcount=30)  
[and here’s the algorithm basically once you understand dot product.](http://www.gamedev.net/community/forums/topic.asp?topic_id=491277&whichpage=1&#3205580)

---

<div class="post-metadata">

**Author:** ![zuperxtreme](https://avatars.discourse-cdn.com/v4/letter/z/e9a140/32.png) [@zuperxtreme](https://forum.kirupa.com/u/zuperxtreme)\
**Post date:** [May 26, 2008, 8:24pm UTC](https://forum.kirupa.com/t/game-theory-putting-units-in-formation/261248/4 "2008-05-26T20:24:11Z")

</div>

[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](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](http://www.red3d.com/cwr/boids/)  
//also since people doing AI ask about steering it’s crucial you understand the [dot product of two vectors.](http://www.kirupa.com/forum/showpost.php?p=2216146&postcount=30)  
[and here’s the algorithm basically once you understand dot product.](http://www.gamedev.net/community/forums/topic.asp?topic_id=491277&whichpage=1&#3205580)[/quote]

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

Cheers! 🙂
