Exemplo n.º 1
0
 //Cohesion produces a steering force that moves a vehicle toward the center of mass of its neighbors
 //A sheep running after its flock is demonstrating cohesive behavior. Use this force to keep a group of vehicles together.
 public static Vector2 Cohesion(ref Vehicle[] allCars, Vehicle me, Vector2 currentPosition, Vector2 velocity, int max_speed, int cohesionRadius)
 {
     int j = 0;
     Vector2 averagePosition = new Vector2(0);
     Vector2 distance = new Vector2(0);
     for (int i = 0; i < allCars.Length; i++)
     {
         distance = Vector2.Subtract(currentPosition, allCars[i].CurrentPosition);
         if (Vector2.Length(distance) < cohesionRadius && allCars[i] != me)
         {
             j++;
             averagePosition = Vector2.Add(averagePosition, allCars[i].CurrentPosition);
             //averagePosition = Vector2.Multiply(averagePosition, 10);
             //averagePosition = Vector2.Add(averagePosition, Vector2.Normalize(distance) / Vector2.Length(distance));
         }
     }
     if (j == 0)
     {
         return None();
     }
     else
     {
         averagePosition = averagePosition / j;
         return Seek(ref averagePosition, ref currentPosition, ref velocity, max_speed);
     }
 }
Exemplo n.º 2
0
 //Alignment attempts to keep a vehicle's heading aligned with its neighbors
 //The force is calculated by first iterating through all the neighbors and averaging their heading vectors.
 //This value is the desired heading, so we just subtract the vehicle's heading to get the steering force.
 public static Vector2 Alignment(ref Vehicle[] allCars, Vehicle me, ref Vector2 currentPosition, ref Vector2 velocity, int max_speed)
 {
     int j = 0;
     Vector2 averageDirection = new Vector2(0);
     Vector2 distance = new Vector2(0);
     for (int i = 0; i < allCars.Length; i++)
     {
         distance = Vector2.Subtract(currentPosition, allCars[i].CurrentPosition);
         if (Vector2.Length(distance) < 100 && allCars[i] != me)
         {
             j++;
             averageDirection = Vector2.Add(averageDirection, allCars[i].Velocity);
         }
     }
     if (j == 0)
     {
         return None();
     }
     else
     {
         averageDirection = averageDirection / j;
         return Vector2.Subtract(averageDirection, velocity);
     }
 }
Exemplo n.º 3
0
 public static void SetCarsData(ref Vehicle[] cars)
 {
     allCars = cars;
 }
Exemplo n.º 4
0
 //initialize all cars including "otherCar"
 private void InitializeCars()
 {
     otherCar = new Vehicle(g, clientSize, SB.Wander, -2);
     otherCar.Mass = 2;
     otherCar.MaxSpeed = 4;
     otherCar.CreateObstacles();
     for (int i = 0; i < cars.Length; i++)
     {
         cars[i] = new Vehicle(g, clientSize, SBs, i + 1);
         if (!mainForm.identicalVehicles)
             Thread.Sleep(50);
     }
     Vehicle.SetCarsData(ref cars);
 }
Exemplo n.º 5
0
 //Separation creates a force that steers a vehicle away from those in its neighborhood region.
 //When applied to a number of vehicles, they will spread out, trying to maximize their distance from every other vehicle
 public static Vector2 Separation(ref Vehicle[] allCars, Vehicle me, ref Vector2 currentPosition, ref Vector2 velocity, int max_speed)
 {
     int j = 0;
     Vector2 separationForce = new Vector2(0);
     Vector2 averageDirection = new Vector2(0);
     Vector2 distance = new Vector2(0);
     for (int i = 0; i < allCars.Length; i++)
     {
         distance = Vector2.Subtract(currentPosition, allCars[i].CurrentPosition);
         if (Vector2.Length(distance) < 100 && allCars[i] != me)
         {
             j++;
             separationForce += Vector2.Subtract(currentPosition, allCars[i].CurrentPosition);
             separationForce = Vector2.Normalize(separationForce);
             separationForce = Vector2.Multiply(separationForce, 1 / .7f);
             averageDirection = Vector2.Add(averageDirection, separationForce);
         }
     }
     if (j == 0)
     {
         return None();
     }
     else
     {
         //averageDirection = averageDirection / j;
         return averageDirection;
     }
 }