- //otherBoids ist ein array, welches alle Boids enthält
- private double[] calcSeperation()
- {
- double pos[] = {0,0};
- for (int i = 0; i < otherBoids.length; i++)
- {
- if (otherBoids[i] != null)
- {
- if (this == otherBoids[i]) continue;
- // schauen welche Boids im Bereich sind
- if (calcDistance(otherBoids[i]) < getRadius())
- {
- pos[0] = pos[0] + otherBoids[i].calcNextXPos(); //calcNextPos berechnet die nächste Position des Boid
- pos[1] = pos[1] + otherBoids[i].calcNextYPos();
- }
- }
- }
- pos[0] = pos[0]*SEPERATION; //Konstante für die Gewichtung
- pos[1] = pos[1]*SEPERATION;
- return pos;
- }
- private double[] calcCohesion()
- {
- double pos[] = {0,0};
- int boidCnt = 0;
- for (int i = 0; i < otherBoids.length; i++)
- {
- if (otherBoids[i] != null)
- {
- if (this == otherBoids[i]) continue;
- if (calcDistance(otherBoids[i]) < getRadius())
- {
- pos[0] = pos[0] + otherBoids[i].calcNextXPos();
- pos[1] = pos[1] + otherBoids[i].calcNextYPos();
- boidCnt++;
- }
- }
- }
- if (boidCnt != 0)pos[0] = (pos[0]*COHESION)/boidCnt;
- if (boidCnt != 0)pos[1] = (pos[1]*COHESION)/boidCnt;
- return pos;
- }
- private double calcAligment()
- {
- double speed = 0;
- int boidCnt = 0;
- for (int i = 0; i < otherBoids.length; i++)
- {
- if (otherBoids[i] != null)
- {
- if (this == otherBoids[i]) continue;
- if (calcDistance(otherBoids[i]) < getRadius())
- {
- speed = speed + otherBoids[i].getSpeed();
- boidCnt++;
- }
- }
- }
- if (boidCnt == 0) speed = getSpeed();
- else speed = speed/boidCnt;
- return speed;
- }
- private void boidRules()
- {
- double XPos = 0;
- double YPos = 0;
- double pos[] = {0,0};
- pos = calcCohesion();
- XPos = XPos + pos[0];
- YPos = YPos + pos[1];
- pos = calcSeperation();
- XPos = XPos + pos[0];
- YPos = YPos + pos[1];
- setSpeed(calcAligment());
- if (XPos != 0 || YPos != 0) setAngle(Math.toDegrees((Math.atan(Math.abs(YPos - getYPos())/Math.abs(XPos - getXPos()))))); //setzt den Winkel mit tan^-1
- }