Ai Flocking Behavior - C# - not working properly

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 (\~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 -

flutterby.cs -

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

Video of errant behavior:

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

2 Replies

Please log in or register to post a reply.

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.

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.