3
\$\begingroup\$

I read and understand the basic of flocking algorithm. Basically, we need to have 3 behaviors:
1. Cohesion
2. Separation
3. Alignment

From my understanding, it's like a state machine. Every time we do an update (then draw), we check all the constraints on both three behaviors. And each behavior returns a Vector3 which is the "correct" orientation that an object should transform to. So my initial idea was

        /// <summary>
        /// Objects stick together
        /// </summary>
        /// <returns></returns>
        private Vector3 Cohesion() {
            Vector3 result = new Vector3(0.0f, 0.0f, 0.0f);
            return result;
        }

        /// <summary>
        /// Object align 
        /// </summary>
        /// <returns></returns>
        private Vector3 Align() {
            Vector3 result = new Vector3(0.0f, 0.0f, 0.0f);
            return result;
        }

        /// <summary>
        /// Object separates from each others
        /// </summary>
        /// <returns></returns>
        private Vector3 Separate() {
            Vector3 result = new Vector3(0.0f, 0.0f, 0.0f);
            return result;
        }

Then I search online for pseudocode but many of them involve velocity and acceleration plus other stuffs. This part confused me. In my game, all objects move at constant speed, and they have one leader. So can anyone share me an idea how to start on implement this flocking algorithm? Also, did I understand it correctly? (I'm using XNA 4.0)

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

You really don't want constant velocity. Flocking is going to work and look better if the boids can speed up or slow down while flocking. Otherwise a separated boid may never be able to catch up to the flock.

This article has some very easy to read code (CoffeeScript), a decent explanation, and a pretty demo you can tweak:

http://harry.me/2011/02/17/neat-algorithms---flocking/

\$\endgroup\$
2
  • \$\begingroup\$ But what if I control my leader and go around to tag each object, then would constant velocity make sense? Thank you. \$\endgroup\$
    – roxrook
    Commented Apr 7, 2012 at 14:19
  • \$\begingroup\$ @Chan You can of course mimic flocking by having a whole flock act as one entity. The leader is fixed and all the others positions is an offset from the leader, but it would not look or behave the same when turning, or when multiple flocks converge (with flocking a new leader would naturally be in the lead and the two flocks would converge naturally). \$\endgroup\$ Commented Apr 7, 2012 at 14:24
3
\$\begingroup\$

I would tend to assume you have already read through Reynold's (the guy who invented boids) page about flocking, but since you didn't directly mention it I'm going to link it here:

http://www.red3d.com/cwr/boids/

He gives great explanations of how several aspects of his flocking algorithms work, including Cohesion, Separation, and Alignment.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, but I already read that page couple times. I still couldn't figure out how to it on pencil and paper. \$\endgroup\$
    – roxrook
    Commented Apr 7, 2012 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .