示例#1
0
文件: util.cpp 项目: jacyxli/boids
/** Rule of Cohesion.
 ** @param bj Pointer to the boid
 ** @return The velocity vector for Cohesion
 **/
vector Cohesion(Boid* bj){
  vector c = NullVector();
  It itb = flock->getIter();
  List boids = flock->getBoids();
  Boid* tmp;
  for (itb = boids.begin(); itb!=boids.end(); itb++) {
    tmp = &(*itb);
    float d = 1 + Norm(Diff(bj->getPos(), tmp->getPos()));
    if (bj->getID() != tmp->getID() && d < 20)
      c = Add(c, Mult(tmp->getPos(),1/(2*d*d)));
  }
  c = Mult(c,(float(1)/float(boids.size()-1)));
  return Diff(c,bj->getPos());
}
示例#2
0
文件: util.cpp 项目: jacyxli/boids
/** Rule of Alignment.
 ** @param bj Pointer to the boid
 ** @return The velocity vector for Alignment
 **/
vector Alignment(Boid* bj) {
  vector mspeed = NullVector();
  It itb = flock->getIter();
  List boids = flock->getBoids();
  Boid* tmp;
  for (itb = boids.begin(); itb!=boids.end(); itb++) {
    tmp = &(*itb);
    float d = 1 + Norm(Diff(bj->getPos(), tmp->getPos()));
    if (bj->getID() != tmp->getID() && d < 20)
      mspeed = Add (mspeed, Mult(tmp->getSpeed(),1/(d*d)));
  }
  mspeed = Mult(mspeed,(float(1)/float(boids.size()-1)));
  return Diff(mspeed,bj->getSpeed());
}