void CreateBoid()
    {
        GameObject newboid = Instantiate(boidPrefab, this.transform.position, Quaternion.identity);

        newboid.transform.parent = this.transform;
        BoidBehavior newboidbehavior = newboid.GetComponent <BoidBehavior>();

        newboidbehavior.boidManager = this;
        newboidbehavior.speed       = boidSpeed;

        boids.Add(newboidbehavior);
    }
예제 #2
0
    //used to send data
    IEnumerator SendData()
    {
        yield return(new WaitForSeconds(.1f));

        for (int i = 0; i < bloidListLocal.Count; ++i)
        {
            currentObj   = bloidListLocal[i].GetComponent <BoidBehavior>();
            currObjTrans = bloidListLocal[i].transform.position;
            sendData(currentObj.objId, currObjTrans.x, currObjTrans.y, currObjTrans.z, currentObj.direction);
        }
        StartCoroutine("SendData"); // this is here so send data is called
    }
예제 #3
0
    public void update(ArrayList b)
    {
        Vector3 alignment  = Vector3.zero;
        Vector3 cohesion   = Vector3.zero;
        Vector3 separation = Vector3.zero;

        for (int i = 0; i < b.Count; i++)
        {
            GameObject   elt    = b[i] as GameObject;
            BoidBehavior cs     = elt.GetComponent <BoidBehavior>();
            Rigidbody    eltBod = cs.body;
            if (!eltBod.Equals(body))
            {
                Vector3 pathTo = eltBod.position - body.position;
                float   dist   = pathTo.magnitude;
                float   ang    = Vector3.Angle(body.velocity, pathTo);

                /*if (dist <= AVOID_BOIDS_RAD && ang <= SENSE_ANG)
                 * {
                 *  alignment += syncDir(eltBod);
                 *
                 *  separation += seperation(pathTo, dist);
                 *
                 * }*/
                if (dist <= SENSE_RAD && ang <= SENSE_ANG)
                {
                    Debug.DrawLine(body.position, eltBod.position);

                    alignment += syncDir(eltBod);

                    cohesion += flockCentering(pathTo);
                }
            }
        }

        body.velocity += (alignment.normalized * DIR_SYNC_COEF) + (cohesion.normalized * FLOCK_CENT_COEF) + (separation * SEPARATION_COEF);

        if (collisionImpending())
        {
            Vector3 dir = findSafePath();
            body.velocity += COLL_AVOID_COEF * dir.normalized;
        }


        body.velocity = body.velocity.normalized * MAX_VEL;

        Debug.DrawLine(body.position + body.velocity.normalized, body.position + (2 * body.velocity.normalized), Color.blue);
    }
 // Update is called once per frame
 void Update()
 {
     if (allowUpdates)
     {
         //updates my objects
         timer -= Time.deltaTime;
         for (int i = 0; i < bloidList2.Count; ++i)
         {
             currentObj = bloidList2[i].GetComponent <BoidBehavior>();
             if (timer <= 0)
             {
                 currentObj.SendMessage("SetDirection");
             }
             currentObj.SendMessage("simulatePos");
         }
         BloidData newData = receiveData();
         //update all objects
         if (newData.objectId >= 0)
         {
             for (int i = 0; i < bloidList1.Count && !found; i++)
             {
                 // find the object and update it
                 if (bloidList1[i].GetComponent <BoidBehavior>().objId == newData.objectId)
                 {
                     bloidList1[i].GetComponent <BoidBehavior>().SendMessage("setPos", new Vector3(newData.x, newData.y, newData.z));
                     bloidList1[i].GetComponent <BoidBehavior>().direction = newData.direction;
                     found = true;
                 }
             }
             // this was done for spawning events that never happened
             if (!found && newData.objectId <= 10000)
             {
                 GameObject dorkus = Instantiate(boid, new Vector3(newData.x, newData.y, newData.z), Quaternion.identity);
                 dorkus.GetComponent <BoidBehavior>().objId = newData.objectId;
                 bloidList1.Add(dorkus);
             }
             found = false;
         }
         if (timer <= 0)
         {
             timer = MAX_TIME;
         }
     }
 }
예제 #5
0
    // Update is called once per frame
    void Update()
    {
        if (allowUpdates)
        {
            //simulates local position of local objects
            for (int i = 0; i < bloidListLocal.Count; ++i)
            {
                currentObj = bloidListLocal[i].GetComponent <BoidBehavior>();
                currentObj.SendMessage("simulatePos");
            }

            BloidData newData = receiveData();

            //get timestamp from newdata and set relative times
            float currentTime = Time.deltaTime;
            float timeStamp   = Time.deltaTime + newData.timeStamp;
            float timeStep    = Time.deltaTime + (timeStamp * 2.0f);

            //tmp variables for loop iteration
            BoidBehavior tmp;
            Vector3      initialPos, timestepPos, timestepX2Pos, average, velFinal;

            //simulate all objects
            if (newData.objectId >= 0)
            {
                for (int i = 0; i < bloidListAll.Count && !found; i++)
                {
                    // find the object and update it
                    tmp = bloidListAll[i].GetComponent <BoidBehavior>();


                    if (tmp.objId == newData.objectId)
                    {
                        //calculate position with dead reckoning
                        //at current time
                        initialPos = deadReckon(tmp.transform.position, tmp.velocity, tmp.acceleration, currentTime);
                        //at timestamp
                        timestepPos = deadReckon(tmp.transform.position, tmp.velocity, tmp.acceleration, timeStamp);
                        //at double the timestamp
                        timestepX2Pos = deadReckon(tmp.transform.position, tmp.velocity, tmp.acceleration, timeStep);

                        //average vector of the three for the position
                        average.x = (initialPos.x + timestepPos.x + timestepX2Pos.x) / 3;
                        average.y = (initialPos.y + timestepPos.y + timestepX2Pos.y) / 3;
                        average.z = (initialPos.z + timestepPos.z + timestepX2Pos.z) / 3;

                        //set velocity of boid for future
                        velFinal = tmp.velocity + (tmp.acceleration * timeStamp);

                        //send position, direction, and final velocity to individual bloid, nice
                        tmp.velocity = velFinal;
                        tmp.SendMessage("setPos", average);
                        tmp.direction = newData.direction;
                        found         = true;
                    }
                }
                // this was done for spawning events that never happened
                if (!found && newData.objectId <= 10000)
                {
                    GameObject dorkus = Instantiate(boid, new Vector3(newData.x, newData.y, newData.z), Quaternion.identity);
                    dorkus.GetComponent <BoidBehavior>().objId = newData.objectId;
                    bloidListAll.Add(dorkus);
                }
                found = false;
            }
        }
    }