Jump to content


Ai Flocking Behavior - C# - not working properly


2 replies to this topic

#1 cdxrd

    New Member

  • Members
  • Pip
  • 1 posts

Posted 25 April 2008 - 05:14 PM

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....seudocode.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...e/flutterby.zip

Video of errant behavior:


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

#2 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 28 April 2008 - 07:18 PM

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.
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#3 Sturm

    New Member

  • Members
  • PipPip
  • 26 posts

Posted 15 May 2008 - 12:43 PM

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users