// RULE1: Boids try to fly towards the centre of mass of neighbouring boids private Vector3 cohesionRule(AEnemys curShip) { Vector3 pcj = Vector3.Zero; int count = 0; foreach (var otherShip in _ships) { if (otherShip != curShip && Vector3.Distance(otherShip.Position, curShip.Position) < _cohesionRadius) { pcj += otherShip.Position; count++; } } if (count > 0) { pcj /= count; } else { return(Vector3.Zero); } //richtungsvektor zur 'masse' return(pcj - curShip.Position); }
// some logic for the fighters private Vector3 droneStationRuleFighter(AEnemys curShip) { float distanceToDrone = Vector3.Distance(curShip.Position, Global.Camera.Target); if (curShip.flyingAwayFromDrone) { if (distanceToDrone < _avoidDroneRadius) { return(-goToPlace(curShip, Global.Camera.Target) / distanceToDrone); } else { curShip.flyingAwayFromDrone = false; } } else { if (distanceToDrone < _flyToDroneRadius) { if (distanceToDrone < _avoidObjRadius) { curShip.flyingAwayFromDrone = true; } else { return(goToPlace(curShip, Global.Camera.Target)); } } else if (curShip.Position.Length() > _flyToStationRadius) { return(goToPlace(curShip, Vector3.Zero)); } } return(Vector3.Zero); }
// RULE2.5: Boids try to keep a small distance away from other Objects private Vector3 avoidObjRule(AEnemys curShip) { Vector3 c = Vector3.Zero; foreach (var curObj in objectsToAvoid) { float distance = Vector3.Distance(curObj.Position, curShip.Position); if (curObj != curShip && distance < _avoidObjRadius) { c -= (curObj.Position - curShip.Position); } } return(c); }
// RULE2: Boids try to keep a small distance away from other boids private Vector3 avoidBoidRule(AEnemys curShip) { Vector3 c = Vector3.Zero; foreach (var otherShip in _ships) { float distanceDiff = Vector3.Distance(otherShip.Position, curShip.Position) - _avoidBoidsRadius; if (otherShip != curShip && distanceDiff < 0) { c -= (otherShip.Position - curShip.Position); } } return(c); }
// some logic for the bombers private Vector3 droneStationRuleBomber(AEnemys curShip) { float distanceToDrone = Vector3.Distance(curShip.Position, Global.Camera.Target); double distanceToStation = curShip.Position.Length(); if (distanceToDrone < 200) { curShip.flyingAwayFromDrone = true; } if (distanceToStation < 200) { curShip.flyingAwayFromStation = true; } if (curShip.flyingAwayFromDrone) { if (distanceToDrone < 300) { return(-goToPlace(curShip, Global.Camera.Target)); } else { curShip.flyingAwayFromDrone = false; } } else if (curShip.flyingAwayFromStation) { if (distanceToStation > Global.MapRingRadius + 75) { curShip.flyingAwayFromStation = false; } else { return(-goToPlace(curShip, Vector3.Zero)); } } else { return(goToPlace(curShip, Vector3.Zero)); } return(Vector3.Zero); }
// RULE3: Boids try to match velocity with near boids private Vector3 aligningRule(AEnemys curShip) { Vector3 pvj = Vector3.Zero; int count = 0; foreach (var otherShip in _ships) { float distance = Vector3.Distance(otherShip.Position, curShip.Position); if (otherShip != curShip && distance < _aligningRadius) { pvj += otherShip.Direction; count++; } } if (count > 0) { pvj /= count; } return(pvj); }
private Vector3 avoidStationRule(AEnemys curShip) { var awayVector = -goToPlace(curShip, Vector3.Zero); var shipDist = curShip.Position.Length(); if (shipDist < _avoidStationRadius) { var leftVectorLength = (curShip.Position + curShip.RotationMatrix.Left).Length(); var rightVectorLength = (curShip.Position + curShip.RotationMatrix.Right).Length(); // ship is left of station, fly left if (leftVectorLength > rightVectorLength) { return((new Vector3(awayVector.Z, 0, -awayVector.X) + awayVector) / shipDist); } // ship is right of station, fly right else { return((new Vector3(-awayVector.Z, 0, awayVector.X) + awayVector) / shipDist); } } return(Vector3.Zero); }
private Vector3 goToPlace(AEnemys curShip, Vector3 place) { Vector3 placeDir = place - curShip.Position; return(placeDir); }