Ai Flocking Behavior - C# - not working properly

Acc2b16b9e981a5172c4954f19636ffd
0
cdxrd 101 Apr 25, 2008 at 17:14

Ok, so I thought that next on my list was tackling some AI. I was looking at Boids Psuedocode for a flocking setup (http://www.vergenet.net/\~conrad/boids/pseudocode.html)).

Well, I set up a new project, got a background and some sprites. Im populating the list, and displaying the sprites, then with a short delay it starts updating. Problem is, only some of the entities are behaving properly, the rest dart to the bottom right hand corner. I added in a virtual ‘goal’ for them to attempt to get to, and as you can see in the video, a couple do, the rest do not. They’re all operating off the same rules, so where am I going wrong here?

Its Rule 2 that Im having the problem in I think, since as soon as I make it return a 0,0 vector they all flock to the point they are supposed to, they just dont keep a distance from each other like rule 2 is supposed to provide. Any insights?

game1.cs - http://xna.pastebin.com/m48636854

flutterby.cs - http://xna.pastebin.com/m34f94e13

Project source if you have xna 2.0 installed - Zipped - 597kb

http://www.cdx-games.com/source/flutterby.zip

Video of errant behavior: http://www.youtube.com/watch?v=QJhuRkMasxc

Anyone see where im going wrong? I think i’ve gone cross eyed..

2 Replies

Please log in or register to post a reply.

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Apr 28, 2008 at 19:18

With a quick glance it looks like you intended to divide the rule 2 result vector by count, but did not do so. Since this is done for rules 1 and 3 it could result in a huge bias towards rule 2.

A2b88b0df0ec92f2a587208b4e2a5c6b
0
Sturm 101 May 15, 2008 at 12:43

Hi cdxrd,

First thing you should use Vector2.Distance as simply subtracting x and y does not return the correct value.

Also I think you have to take into account that the second flutte could be on the right/left or above/below I would guess that the calc should differ, ie subtracting is ok when the other is on the right side, but will bring them closer if on the left side.