예제 #1
0
Vec2 Boid::Flock(BoidFriend* boids, int count)
{
	Vec2 cohesion, alignment, separation;

	// average the positions and velocities of all boids in area
	int sepCount = 0;

	for (int i = 0; i != count; ++i)
	{
		cohesion += boids[i].boid->GetPosition() - GetPosition();
		alignment += boids[i].boid->GetVelocity();

		if (boids[i].distance < 1.25f)
		{
			Vec2 delta = (GetPosition() - boids[i].boid->GetPosition());
			if (!delta.IsZero())
			{
				separation += delta.Normalize() / boids[i].distance;
				++sepCount;
			}
		}
	}

	if (count != 0)
	{
		cohesion /= count;
		alignment /= count;
	}

	if (sepCount != 0)
		separation /= sepCount;

	return Steer(cohesion) + Steer(alignment) + Steer(separation);
}
예제 #2
0
Vec2 Boid::Steer(const Vec2& target) const
{
	if (!target.IsZero())
	{
		Vec2 dir = target.Normalize();
		dir *= m_MaxSpeed;
		Vec2 steer = dir - GetVelocity();
		steer = steer.Clamp(m_MaxSteeringForce);
		return steer;
	}
	else
		return Vec2();
}