Steering behaviors aim to help autonomous characters move in a realistic manner, by using simple forces that are combined to produce life-like, improvisational navigation around the characters' environment. They are not based on complex strategies involving path planning or global calculations, but instead use local information, such as neighbors' forces. This makes them simple to understand and implement, but still able to produce very complex movement patterns
public static Vector2 GetHerdVelocity(HerdAnimal[] herdAnimals,
HerdAnimal currentAnimal)
{
Vector2 perceivedCentre = Vector2.Zero;
Vector2 separation = Vector2.Zero;
Vector2 perceivedVelocity = Vector2.Zero;
foreach (HerdAnimal animal in herdAnimals)
{
if (animal != currentAnimal)
{
perceivedCentre += animal.Position;
if ((animal.Position -
currentAnimal.Position).LengthSquared() < _distance)
{
separation -= (animal.Position -
currentAnimal.Position);
}
perceivedVelocity += animal.Velocity;
}
}
perceivedCentre /= herdAnimals.GetLength(0) - 1;
Vector2 cohesion = (perceivedCentre -
currentAnimal.Position) * _cohesionCoef;
perceivedVelocity /= (herdAnimals.GetLength(0) - 1);
Vector2 alignment = (perceivedVelocity -
currentAnimal.Velocity) * _alignmentCoef;
return cohesion + separation + alignment;
}
To get started...
- 🍴 Fork this repo!
- HACK AWAY! 🔨🔨🔨
- MIT license
- Copyright 2021 © Sierra.