private void FollowerMovement(float attraction, float repulsionStrength, float repulsionDistance) { Copter leader = myArena.GetLeader(); foreach (CircleCopter thisCopter in myArena.GetAllCopters()) { //Attraction: Vector2 toLeader = leader.Position() - thisCopter.Position(); Vector2 toLeaderNormed = toLeader / (Vector2.Distance(leader.Position(), thisCopter.Position())); Vector2 attractionMovement = toLeaderNormed * attraction; //Repulsion from leader: Vector2 repulsionFromLeader = repulsionStrength * Mathf.Exp(-0.5f * (toLeader.magnitude * toLeader.magnitude) / (repulsionDistance * repulsionDistance)) * toLeader; //Attraction to and repulsion from other copters: Vector2 repulsionMovement = new Vector2(0.0f, 0.0f); foreach (CircleCopter otherCopter in myArena.GetAllCopters()) { if (thisCopter != otherCopter) { CollisionResult cr = thisCopter.CollidesWithCopter(otherCopter); Vector2 repulsionDirection = cr.CollisionPointThisObject - cr.CollisionPointOtherObject; Vector2 repulsionVector = repulsionStrength * Mathf.Exp(-0.5f * cr.Distance * cr.Distance / (repulsionDistance * repulsionDistance)) * repulsionDirection; repulsionMovement += repulsionVector - attraction * repulsionDirection; } } //Position update: Vector2 update = (attractionMovement + repulsionFromLeader + repulsionMovement).normalized * 0.05f; thisCopter.SetPosition(thisCopter.Position() + update); } }
public static Vector2 AttractToTarget(Copter thisCopter, Vector2 target, float attractionMagnitude) { Vector2 direction = target - thisCopter.Position(); Vector2 normedDirection = direction / (Mathf.Max(direction.magnitude, 1.0f)); return(normedDirection * attractionMagnitude); }
public static Vector2 Function3(Copter thisCopter, Vector2 otherCopter, float a, float b, float c) { Vector2 direction = thisCopter.Position() - otherCopter; float attraction = a; float repulsion = b * ((float)Math.Exp(-(direction.magnitude * direction.magnitude) / c)); return(-direction * (attraction - repulsion)); }
public static void LeaderMovement(TunnelArena myArena, float speed) { Copter leader = myArena.GetLeader(); Vector2 movementVector = new Vector2(0.0f, 0.0f); movementVector += SwarmingFormulas.AttractToTarget(leader, myArena.GetTarget(), 0.5f); movementVector += AvoidWalls(leader, myArena, 1, 0.1f); if (movementVector.magnitude > 1.0f) { movementVector.Normalize(); } leader.SetPosition(leader.Position() + (movementVector * speed)); }
public static Vector2 RepelWhenClose(Copter thisCopter, Vector2 otherPoint, float repulsionMagnitude, float repulsionRadius) { return(RepelWhenClose(thisCopter.Position(), otherPoint, repulsionMagnitude, repulsionRadius)); }
public static Vector2 Function3(Copter thisCopter, Copter otherCopter, float a, float b, float c) { return(Function3(thisCopter, otherCopter.Position(), a, b, c)); }