Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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/

share|improve this answer
    
But what if I control my leader and go around to tag each object, then would constant velocity make sense? Thank you. –  Chan Apr 7 '12 at 14:19
    
@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). –  Daniel Carlsson Apr 7 '12 at 14:24
    
@Daniel Carlsson: Thanks a lot, I got it ;). –  Chan Apr 7 '12 at 14:54
add comment

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.

share|improve this answer
    
Thanks, but I already read that page couple times. I still couldn't figure out how to it on pencil and paper. –  Chan Apr 7 '12 at 14:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.