void Flock::update(const Scene & scene){
	for(std::vector<Boid * >::iterator it = boids.begin(); it != boids.end(); ++it) {
		 	int index = it - boids.begin();
		 	Vector v = rule1(index) + rule2(index) + rule3(index);
		 	v = v * damping;
			Boid * boid = *it;
		 	v = v + boid->getVelocity();

		 	if(v.getLength() > maxSpeed){
		 		v = v * (maxSpeed/v.getLength());
		 	}

			Point p = boid->getCenter();
			double wind = 0.12;
			Vector windVector;
			if(p.x> 900 ){
				windVector = Vector(-wind,0);
			} else if(p.x< 150 ){
				windVector = Vector(wind,0);
			} else if(p.y> 350 ){
				windVector = Vector(0,-wind);
			} else if(p.y< 150 ){
				windVector = Vector(0,wind);
			}

			boid->setVelocity(v + windVector);
		 	boid->setCenter(boid->getCenter() + boid->getVelocity());
	}
}
Vector Flock::rule3(unsigned index){
	Vector avVelocity;
	for(std::vector<Boid * >::iterator it = boids.begin(); it != boids.end(); ++it) {
		if(*it != boids.at(index)){
			Boid * boid = *it;
			avVelocity = avVelocity+boid->getVelocity();
		}
	}
	avVelocity /= boids.size();
	Vector v = avVelocity - boids.at(index)->getVelocity();

	v = v*alignment;
	return v;
}
Esempio n. 3
0
void Loop::update()
{
	Vector2 velocity;
	for (size_t i = 0; i < mDataStructure.getBoidCount(); i++)
	{
		Boid* currentBoid = mDataStructure.getMatrix()[i / MAT_SIZE][i % MAT_SIZE];

		velocity = mBoidRules.calculateVelocity(mDataStructure.getMatrix(), currentBoid, mLeader, i / MAT_SIZE, i % MAT_SIZE, mFood.getPosition(), mBlocks, mPathVertices);
		velocity += currentBoid->getVelocity();

		currentBoid->setVelocity(velocity.limit(Boid::SPEED));
		currentBoid->boundPosition(GUI_WIDTH + 5, 5, SCREEN_WIDTH - 5, SCREEN_HEIGHT - 5);
		currentBoid->move();
	}
}