コード例 #1
0
ファイル: BoidAgent.cs プロジェクト: mellofnd/wgj150
    private void ApplyRules()
    {
        Vector3 avoidPos    = Vector3.zero;
        Vector3 stirDir     = Vector3.zero;
        Vector3 cohesionPos = Vector3.zero;
        Vector3 allignPos   = Vector3.zero;

        float distance = 0;
        float avgSpeed = 0.1f;

        neighbourCount = 0;

        Vector3 goalPos = FlockManager.GetInstance().GoalPos;

        foreach (var neighbour in mBoids)
        {
            if (neighbour != gameObject)
            {
                distance = Vector3.Distance(neighbour.transform.position, transform.position);

                if (distance <= cohesionDist)
                {
                    neighbourCount++;
                    Debug.DrawLine(transform.position,
                                   neighbour.transform.position,
                                   new Color(1, 1, 1, 0.5f));
                    cohesionPos += neighbour.transform.position;
                    allignPos   += neighbour.transform.forward;
                    if (distance <= avoidanceDist)
                    {
                        avoidPos += neighbour.transform.position;
                    }

                    avgSpeed += neighbour.GetComponent <BoidAgent> ().speed;
                }
            }
        }

        if (neighbourCount > 0)
        {
            //Ajuste da direção de coesão e alinhamento
            cohesionPos = cohesionPos / neighbourCount;
            allignPos   = allignPos / neighbourCount;

            //Ajuste da velocidade média do agente
            speed = avgSpeed / neighbourCount;
            speed = Mathf.Clamp(speed, 0, maxSpeed);

            //Correção do vetor de movimento do agente
            stirDir = (cohesionPos + avoidPos + allignPos + goalPos) - transform.position;
        }
        else
        {
            stirDir = goalPos - transform.position;
        }

        transform.rotation = Quaternion.Slerp(transform.rotation,
                                              Quaternion.LookRotation(stirDir),
                                              Time.deltaTime * rotationSpeed);
    }
コード例 #2
0
ファイル: BoidAgent.cs プロジェクト: mellofnd/wgj150
 void Start()
 {
     speed       = initialSpeed;
     mBoids      = FlockManager.GetInstance().boids;
     m_boundSize = FlockManager.GetInstance().BoundSize;
 }