KAgent alignment

From KokkugiaWiki

arguments:
pop (ArrayList): an arraylist of the population of agents which it will align to.

returns:
a vector which is the sum of the velocity (vel) vectors of the nearest neighbors with an inverse weighting based on distance.


 
  // alignment
  kVec align (ArrayList pop) {
    kVec sum = new kVec(0,0,0);
    int count = 0;
    for (int i = 0 ; i < pop.size(); i++) {
      kAgent other = (kAgent) pop.get(i);
      float dist = pos.distance(other.pos);
      if ((dist > 0) && (dist < rangeOfVision)) {
        sum.plus(other.vel);
        count++;
      }
    }
    if (count > 0) {
      sum.scale(1/(float)count);
      sum.limit(maxForce);
    }
    return sum;
  }
Views