Particle S warm Simulation 145
float nVX = 0.0f, nVY = 0.0f;
// obtains the velocity vector of the closest boid
nVX = (float)Math.cos( nearestBug.direction );
nVY = (float)Math.sin( nearestBug.direction );
float fVX, fVY; // obtains the new directional vector
fVY = gravityWeight * gVY + sVY + nearWeight * nVY;
newDirection = Vector2Direction( fVX, fVY );
float dX = (float)(minDX - xPos);
float dY = (float)(minDY - yPos);
// from the current position,
// obtains the directional vector towards the closest boid
float inner = dX * fVX + dY * fVY; // used to determine the
proximity
float nearestDist = (float)Math.sqrt( dX * dX + dY * dY );
// obtains the new speed ‘‘new speed’’
// by changing the current speed ‘‘speed’’
if( inner > 0 ){ // If the closest boid was in front of self
if( nearestDist > optDistance ){
newSpeed = speed * accel; // speed up
else{
newSpeed= speed / accel; // speed down
}
}else{ // if the closest boid was behind
if( nearestDist > optDistance ){
newSpeed= speed / accel; // speed down
}else{
newSpeed = speed * accel; //speed up
}
}
}
After that, the following lines obtain the new coordinates (newX, newY) of the
boid.
newX = xPos + (int)( newSpeed * Math.cos( newDirection ) );
newY = yPos + (int)( newSpeed * Math.sin( newDirection ) );
Note that the behavior of the “boid” group slightly differs by changing
gravityWeight and nearWeight to various values. Apar t from this, the ex-
tended versions of “boid” are also provided as follows:
• Two types of tribes/races of boid are introduced.
• Obstacles are placed in the map.