Пример #1
0
        /// <summary>
        /// Boid constructor
        /// </summary>
        /// <param name="position">Initial position</param>
        /// <param name="genderBias">Gender Bias</param>
        private Boid(Vector3 position, float genderBias)
        {
            Properties = new BoidProperties(genderBias);
            Statistics = new BoidStatistics();

            int index = Random.Range(0, 3);
            string boidPrefab = Properties.Gender == Gender.Male ? malePrefab[index] : femalePrefab[index];
            gameObject = BootStrapper.Initialise("mmmm/" + boidPrefab, position, Quaternion.identity) as GameObject;

            Behaviour = new GoalSeekingBehaviour(this);
            mind = new Mind(this);
            Velocity = Behaviour.InitialVelocity();
        }
        private void AddBehaviour(CompositeBehaviour cb)
        {
            int oldCount = (cb.Behaviours != null) ? cb.Behaviours.Length : 0;

            BoidBehaviour[] newBehaviours = new BoidBehaviour[oldCount + 1];
            float[]         newWeights    = new float[oldCount + 1];
            for (int i = 0; i < oldCount; i++)
            {
                newBehaviours[i] = cb.Behaviours[i];
                newWeights[i]    = cb.Weights[i];
            }

            newWeights[oldCount] = 1f;
            cb.Behaviours        = newBehaviours;
            cb.Weights           = newWeights;
        }
Пример #3
0
	public void Initialize (Vector3 parentPosition, BoidBehaviour owner) {
        this.owner = owner;
        scaledDist = distToParent * avgScale;

        transform.position = parentPosition - (transform.forward * scaledDist);
        lastWorldPos = transform.position;

        if (child != null) {
            child.Initialize(transform.position, owner);
        }

        if (leftWing != null && rightWing != null) {
            leftWingDefault = leftWing.localRotation.eulerAngles.z;
            rightWingDefault = rightWing.localRotation.eulerAngles.z;
        }
    }
Пример #4
0
    public static Vector3[] GetOtherBoids(BoidBehaviour callingBoid)
    {
        Vector3 offsetFromVicinityBoids = Vector3.zero;
        int     flockmates = 0;

        foreach (var boid in boids)
        {
            if (boid != callingBoid)
            {
                float boidDistance = Vector3.Distance(callingBoid.transform.position, boid.transform.position);
                if (boidDistance < boid.boidData.boidPerceptionRadius)
                {
                    flockmates++;
                    flockCentrePosition     += boid.transform.position;
                    flockAlignmentDirection += boid.velocity;
                    Vector3 offsetOtherBoids = boid.transform.position - callingBoid.transform.position;
                    if (boidDistance < boid.boidData.boidAvoidanceRadius)
                    {
                        offsetFromVicinityBoids -= offsetOtherBoids;
                    }
                }
            }
        }
        int totalCount;

        if (flockmates != 0)
        {
            totalCount = flockmates;
        }
        else
        {
            totalCount = 1;
        }
        //Vector3 observerPosition = callingBoid.transform.position;

        //flockCentrePosition = ReturnPerceivedVector(flockCentrePosition, observerPosition, totalCount);
        flockAlignmentDirection /= totalCount;
        flockAlignmentDirection  = flockAlignmentDirection - callingBoid.transform.position;
        //flockAlignmentDirection = ReturnPerceivedVector(flockAlignmentDirection, observerPosition, totalCount);
        flockAlignmentDirection /= totalCount;
        flockAlignmentDirection  = flockAlignmentDirection - callingBoid.velocity;
        boidAvoidanceDirections  = offsetFromVicinityBoids;

        Vector3[] returnValues = { flockCentrePosition, flockAlignmentDirection, boidAvoidanceDirections };
        return(returnValues);
    }
Пример #5
0
    // Use this for initialization
    void Start()
    {
        if (camera == null)
        {
            camera = Camera.main;
        }

        // Borders based on cameras - WRONG

        /*
         * height = this.camera.orthographicSize*2;
         * width = height*this.camera.aspect;
         */
        height = borders.transform.localScale.y;
        width  = borders.transform.localScale.x;

        boid = GetComponent <BoidBehaviour>();
    }
Пример #6
0
    public static Vector3 MoveAwayFromNearbyObjects(List <BoidBehaviour> boidList, BoidBehaviour percivingBoid)
    {
        Vector3 result = Vector3.zero;

        foreach (var boid in boidList)
        {
            if (percivingBoid == boid)
            {
                continue;
            }

            if (Mathf.Abs((boid.transform.position - percivingBoid.transform.position).magnitude) < minDistanceToKeepFromObstacles)
            {
                result = result - (boid.transform.position - percivingBoid.transform.position) / MOVE_AWAY_FACTOR;
            }
        }

        return(result);
    }
Пример #7
0
    private void StandardBoidMovement(BoidBehaviour boid)
    {
        Vector3 defaultMovement, component1, component2, component3, component4;

        component1 = Rule1.FindPointTowardsGlobalMassCentre(boidList, boid);
        component2 = TargetPoint.TowardsPoint(boid, targetPoint);
        component3 = Rule2.MoveAwayFromNearbyObjects(boidList, boid);
        component4 = Rule3.MatchVelocityOfPercivedBoids(boidList, boid, perciveDistance);

        /*
         * component1 = Rule1.FindPointTowardsLocalMassCentre(boidList, boid, perciveDistance);
         * component2 = Rule2.MoveAwayFromNearbyObjects(boidList, boid);
         * component3 = Rule3.MatchVelocityOfPercivedBoids(boidList, boid, perciveDistance);
         * componentHunted = RuleHunted.MoveAwayFromPredators(predatorList, boid);
         */

        boid.CurrentVelocity    = boid.CurrentVelocity + component1 + component2 + component3 + component4;
        boid.transform.position = boid.transform.position + boid.CurrentVelocity;
        //Debug.Log(boid.transform.position);
    }
        private void RemoveBehaviour(CompositeBehaviour cb)
        {
            int oldCount = cb.Behaviours.Length;

            if (oldCount == 1)
            {
                cb.Behaviours = null;
                cb.Weights    = null;
                return;
            }

            BoidBehaviour[] newBehaviours = new BoidBehaviour[oldCount - 1];
            float[]         newWeights    = new float[oldCount - 1];
            for (int i = 0; i < oldCount - 1; i++)
            {
                newBehaviours[i] = cb.Behaviours[i];
                newWeights[i]    = cb.Weights[i];
            }
            cb.Behaviours = newBehaviours;
            cb.Weights    = newWeights;
        }
Пример #9
0
    // SIMPLIFIED - All boids have global awareness
    public static Vector3 FindPointTowardsGlobalMassCentre(List <BoidBehaviour> boidList, BoidBehaviour percivingBoid)
    {
        Vector3 massCentre = Vector3.zero;

        foreach (var boid in boidList)
        {
            massCentre.x += boid.transform.position.x;
            massCentre.y += boid.transform.position.y;
        }

        massCentre /= boidList.Count;

        return((massCentre - percivingBoid.transform.position) / DISTANCE_FACTOR);
    }
Пример #10
0
    // DETAILED - boids have limited awarness
    public static Vector3 FindPointTowardsLocalMassCentre(List <BoidBehaviour> boidList, BoidBehaviour percivingBoid, float perciveDistance)
    {
        Vector3 massCentre = Vector3.zero;

        int percivedBoidsCount = 0;

        foreach (var boid in boidList)
        {
            if (boid == percivingBoid)
            {
                continue;
            }

            if (Vector3.Distance(percivingBoid.transform.position, boid.transform.position) > perciveDistance)
            {
                continue;
            }

            percivedBoidsCount++;
            massCentre.x += boid.transform.position.x;
            massCentre.y += boid.transform.position.y;
        }

        massCentre /= percivedBoidsCount;

        return((massCentre - percivingBoid.transform.position) / DISTANCE_FACTOR);
    }
Пример #11
0
 void Awake() {
     boid = GetComponent<BoidBehaviour>();
 }
Пример #12
0
    public static Vector3 MatchVelocityOfPercivedBoids(List <BoidBehaviour> boidsList, BoidBehaviour percivingBoid, float perciveDistance)
    {
        Vector3 percivedVelocity   = Vector3.zero;
        int     percivedBoidsCount = 0;

        foreach (var boid in boidsList)
        {
            if (percivingBoid == boid)
            {
                continue;
            }

            if (Vector3.Distance(boid.transform.position, percivingBoid.transform.position) > perciveDistance)
            {
                continue;
            }

            percivedBoidsCount++;
            percivedVelocity = percivedVelocity + boid.CurrentVelocity;
        }

        percivedVelocity /= percivedBoidsCount;

        return((percivedVelocity - percivingBoid.CurrentVelocity) / PERCIVED_VELOCITY_FACTOR);
    }