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
///
/// Objects stick together
///
///
private Vector3 Cohesion() {
Vector3 result = new Vector3(0.0f, 0.0f, 0.0f);
return result;
}
///
/// Object align
///
///
private Vector3 Align() {
Vector3 result = new Vector3(0.0f, 0.0f, 0.0f);
return result;
}
///
/// Object separates from each others
///
///
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)
Answer
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/
No comments:
Post a Comment