Esempio n. 1
0
void Boid::AvoidWalls()
{
	for (GameObject* obj = GameObject::All(); obj != NULL; obj = obj->GetNext())
	{
		// do strong separation for walls
		if (obj->GetType() == GameObject::TWall)
		{
			Vec2 delta = GetPosition() - obj->GetPosition();
			float dist = delta.Length();
			if (dist < 2.f && dist > 0.f)
				AddForce(Steer(delta.Normalize() / dist));
		}
	}

	// avoid outer walls
	Vec2 steer;

	if (GetPosition().x < 1.f)
		steer += Steer(Vec2(1.f, 0.f));
	if (GetPosition().x > 19.f)
		steer += Steer(Vec2(-1.f, 0.f));
	if (GetPosition().y < 1.f)
		steer += Steer(Vec2(0.f, 1.f));
	if (GetPosition().y > 14.f)
		steer += Steer(Vec2(0.f, -1.f));

	AddForce(2.f * steer);
}
Esempio n. 2
0
void Boid::FindClosest(BoidFriend* boids, int& count, int maxCount)
{
	count = 0;

	for (GameObject* obj = GameObject::All(); obj != NULL; obj = obj->GetNext())
	{
		if (obj != this)
		{
			Vec2 delta = GetPosition() - obj->GetPosition();
			float dist = delta.Length();

			// average position and direction for boids close enough to matter
			if (dist < 2.25f)
				InsertClosest((Boid*)obj, dist, boids, count, maxCount);
		}
	}
}