# Why the birds from my flocking code looks more like moscito?

I tried to code the pseudo code found here : http://www.vergenet.net/~conrad/boids/pseudocode.html

My problem is that boids all fly to the center position of the group (like moscitos on a light source at night).

Do you see something wrong in the code below? I suspect the problem comes from the rule 1. But even if I reduce it’s impact… the boids just reduce their speed to the center mass of the group… but they still all fly to it.

The rule 3 seems to work for speed but not for direction since they all fly in opposite direction to reach the center mass of the group. See something wrong?

``````
#include "boids2d.h"

//--------------------------------------------------------------
boids2d::boids2d(){

perlin = new Perlin(4,4,1,94);

xMin = 0;
xMax = ofGetWidth();
yMin = 0;
yMax = ofGetHeight();

inSigth = 200;

for (int i=0;i<CONST_boidsNbr;i++){

boids[i].setId(i);

}

}

void boids2d::draw(){

for (int i=0;i<CONST_boidsNbr;i++){

boids[i].draw();

}

}

void boids2d::update(int mode){

for (int i=0;i<CONST_boidsNbr;i++){

v1 = rule1(boids[i]);
v2 = rule2(boids[i]);
v3 = rule3(boids[i]);
v4 = boundPosition(boids[i]);

boids[i].setVelocity(boids[i].getVelocity() + v1 + v2 + v4);
boids[i].setVelocity(limitSpeed(boids[i]));
boids[i].setPosition(boids[i].getPosition() + boids[i].getVelocity());

}

}

//Boids try to fly towards the centre of mass of neighbouring boids.
ofxVec3f boids2d::rule1(boid2d boid){

ofxVec3f center;
int neighbores = 0;

for(int i=0;i<CONST_boidsNbr;i++){

if (boid.getId() != boids[i].getId()){

if ((boid.getPosition()-boids[i].getPosition()).length() <= inSigth){

center += boids[i].getPosition();
neighbores++;

}

}

}

center = center/neighbores;

return ((center - boid.getPosition()) / 100);

}

//Boids try to keep a small distance away from other objects (including other boids).
ofxVec3f boids2d::rule2(boid2d boid) {

ofxVec3f v = 0;

for(int i=0;i<CONST_boidsNbr;i++){

if (boid.getId() != boids[i].getId()){

if((boids[i].getPosition() - boid.getPosition()).length() < 20){

v = v - (boids[i].getPosition()-boid.getPosition());

}

}

}

return v;

}

//Boids match near boid velocity

ofxVec3f boids2d::rule3(boid2d boid){

ofxVec3f v;
int neighbores;

for(int i=0;i<CONST_boidsNbr;i++){

if (boid.getId() != boids[i].getId()){

if ((boid.getPosition()-boids[i].getPosition()).length() <= inSigth){

v += boids[i].getVelocity();
neighbores ++;

}

}

}

v = v/(neighbores);

return (v - boid.getVelocity()) / 8;

}

//Bounding the position in order to keep the flock within a certain area
ofxVec3f boids2d::boundPosition(boid2d boid) {

ofxVec3f v;

if(boid.getPosition().x < xMin) {

v.x = 1;

} else if (boid.getPosition().x > xMax){

v.x = -1;

}

if(boid.getPosition().y < yMin) {

v.y = 1;

} else if (boid.getPosition().y > yMax){

v.y = -1;

}

return v;

}

//Limit speed
ofxVec3f boids2d::limitSpeed(boid2d boid) {

if(boid.getVelocity().length() > 10){

boid.setVelocity(boid.getVelocity()/boid.getVelocity().length()*3);

}

return boid.getVelocity();

}

float boids2d::pixelToPerlin(int x, int y){

return perlin->Get((float) x/ofGetWidth(),(float) y/ofGetHeight());

}

``````