Ejemplo n.º 1
0
    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);
        }
    }
Ejemplo n.º 2
0
    public static void FollowerMovement(TunnelArena myArena, float speed)
    {
        List <Copter> followers = myArena.GetAllCopters();//Is the leader part of this list? No!

        //bool isLeaderPartOfList = followers.Contains(myArena.GetLeader());
        //Debug.Log(isLeaderPartOfList);
        foreach (Copter follower in followers)
        {
            Vector2 movementVector = new Vector2(0.0f, 0.0f);
            movementVector += AvoidWalls(follower, myArena, 30, 0.5f);//With repulsionMagnitude = 3 and radius = 0.5f they still sometimes move through walls.
            movementVector += SwarmingFormulas.Function3(follower, myArena.GetLeader(), 0.5f, 20.0f, 0.6f);
            foreach (Copter otherCopter in followers)
            {
                if (otherCopter != follower)
                {
                    movementVector += SwarmingFormulas.Function3(follower, otherCopter, 0.3f, 40.0f, 0.6f);
                }
            }
            if (movementVector.magnitude > 1.0f)
            {
                movementVector.Normalize();
            }
            follower.SetPosition(follower.Position() + movementVector * speed);
        }
    }