public static BoidObject[] GetNeighbours(BoidObject Boid) // get array of neighbouring boids within vision distance { int viewLeft = Boid.IntPosX - (VisionDistance / 2); int viewRight = Boid.IntPosX + (VisionDistance / 2); int viewUp = Boid.IntPosY + (VisionDistance / 2); int viewDown = Boid.IntPosY - (VisionDistance / 2); BoidObject[] Neighbours = new BoidObject[] {}; int neighbourCount = 0; for (int i = 0; i < BoidArray.Length; i++) { if (viewLeft <BoidArray[i].IntPosX& viewRight> BoidArray[i].IntPosX) { if (viewDown <BoidArray[i].IntPosY& viewUp> BoidArray[i].IntPosY) { Array.Resize(ref Neighbours, Neighbours.Length + 1); Neighbours[neighbourCount] = BoidArray[i]; neighbourCount = neighbourCount + 1; } } } return(Neighbours); }
public static float GetDistance(BoidObject Boid1, BoidObject Boid2) { double DiffX = Boid1.PosX - Boid2.PosX; double DiffY = Boid1.PosY - Boid2.PosY; float PosDiff = (float)Math.Sqrt(DiffX * DiffX + DiffY * DiffY); return(PosDiff); }
public static void GenerateBoids(int x, int y) { BoidObject Boid = new BoidObject(); Array.Resize(ref BoidArray, BoidArray.Length + 1); Boid.PosX = x; Boid.PosY = y; BoidArray[BoidArray.Length - 1] = Boid; }
// public static ParallelOptions LimitCores = new ParallelOptions(); public static void Reset(int StartX, int StartY) { int BoidCount = 0; // initial number of boids BoidArray = new BoidObject[BoidCount]; for (int i = 0; i < BoidCount; i++) { BoidObject Boid = new BoidObject(); Boid.PosX = StartX; Boid.PosY = StartY; BoidArray[i] = Boid; } }
public static double[] CohesionCalc(BoidObject Boid, BoidObject[] Neighbours) // Aim to center of surrounding boids { double SumOfPosX = 0; double SumOfPosY = 0; for (int i = 0; i < Neighbours.Length; i++) { SumOfPosX = SumOfPosX + Neighbours[i].PosX; SumOfPosY = SumOfPosY + Neighbours[i].PosY; } double AveragePosX = SumOfPosX / Neighbours.Length; double AveragePosY = SumOfPosY / Neighbours.Length; double[] PosDiff = new double[2]; PosDiff[0] = (AveragePosX - Boid.PosX) * CohesionWeight; PosDiff[1] = (AveragePosY - Boid.PosY) * CohesionWeight; return(PosDiff); }
public static double[] AlignmentCalc(BoidObject Boid, BoidObject[] Neighbours) // Aim to match the velocity of surrounding boids { double SumOfVelX = 0; double SumOfVelY = 0; for (int i = 0; i < Neighbours.Length; i++) { SumOfVelX = SumOfVelX + Neighbours[i].VelX; SumOfVelY = SumOfVelY + Neighbours[i].VelY; } double AvgVelX = SumOfVelX / Neighbours.Length; double AvgVelY = SumOfVelY / Neighbours.Length; double[] VelocityDiff = new double[2]; VelocityDiff[0] = (AvgVelX - Boid.VelX) * AlignmentWeight; VelocityDiff[1] = (AvgVelY - Boid.VelY) * AlignmentWeight; return(VelocityDiff); }
public static double[] SeparateCalc(BoidObject Boid, BoidObject[] Neighbours) // find vector in opposite direction of neighbouring boids { double SumCloseX = 0; double SumCloseY = 0; for (int i = 0; i < Neighbours.Length; i++) { float PosDiff = GetDistance(Boid, Neighbours[i]); if (PosDiff < 10) { SumCloseX = SumCloseX + (Boid.PosX - Neighbours[i].PosX); SumCloseY = SumCloseY + (Boid.PosY - Neighbours[i].PosY); } } double[] Separate = new double[2]; Separate[0] = SumCloseX * SeparateWeight; Separate[1] = SumCloseY * SeparateWeight; return(Separate); }