Clear() public method

Clears the simulation.
public Clear ( ) : void
return void
    public void ExecuteRemoves()
    {
        if (m_removes.Count > 0 || m_forceRefresh)
        {
            if (m_removes.Count > 0)
            {
                m_removes.Sort();
                for (int i = m_removes.Count - 1; i >= 0; i--)
                {
                    m_agents.RemoveAt(m_removes[i]);
                }
                m_removes.Clear();
            }

            m_instance.Clear();
            Init();
            for (int i = 0; i < m_obstacles.Count; i++)
            {
                m_obstacles[i].Add(m_instance);
            }
            m_instance.processObstacles();
            for (int i = 0; i < m_agents.Count; i++)
            {
                m_agents[i].Add();
                if (m_agents[i].Id != i)
                {
                    Debug.LogErrorFormat("RVO instance mismatch [rebuild] {0} {1}", i, m_agents[i].Id);
                }
            }
        }
    }
Beispiel #2
0
    /// <summary>Create a number of agents in circle and restart simulation</summary>
    public void CreateAgents(int num)
    {
        this.m_agentCount = num;

        goals      = new Dictionary <int, Vector3>();
        colors     = new List <Color>(m_agentCount);
        m_agentIds = new List <int>(num);

        sim.Clear();
        sim.setTimeStep(m_timeStep);
        sim.setAgentDefaults(neighborDist: m_neighbourDist, maxNeighbors: m_maxNeighbours, radius: m_radius, maxSpeed: m_maxSpeed, timeHorizon: 2, timeHorizonObst: 2, velocity: new RVO.Vector2(1, 1));

        if (type == RVOExampleType.Circle)
        {
            float circleRad = Mathf.Sqrt(m_agentCount * m_radius * m_radius * 4 / Mathf.PI) * exampleScale * 0.05f;

            for (int i = 0; i < m_agentCount; i++)
            {
                Vector3 pos = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / m_agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / m_agentCount)) * circleRad * (1 + Random.value * 0.01f);
                var     id  = sim.addAgent(new RVO.Vector2(pos.x, pos.z));
                m_agentIds.Add(id);
                goals.Add(id, -pos);
                colors.Add(ColorUtility.HSVToRGB(i * 360.0f / m_agentCount, 0.8f, 0.6f));
            }
        }
        else if (type == RVOExampleType.Line)
        {
            for (int i = 0; i < m_agentCount; i++)
            {
                Vector3 pos = new Vector3((i % 2 == 0 ? 1 : -1) * exampleScale, 0, (i / 2) * m_radius * 2.5f);
                var     id  = sim.addAgent(new RVO.Vector2(pos.x, pos.z));
                m_agentIds.Add(id);
                goals.Add(id, new Vector3(-pos.x, pos.y, pos.z));
                colors.Add(i % 2 == 0 ? Color.red : Color.blue);
            }
        }
        else if (type == RVOExampleType.Point)
        {
            for (int i = 0; i < m_agentCount; i++)
            {
                Vector3 pos = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / m_agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / m_agentCount)) * m_radius;
                var     id  = sim.addAgent(new RVO.Vector2(pos.x, pos.z));
                m_agentIds.Add(id);
                //sim.AddAgent(new Vector2(0, 0));
                goals.Add(id, new Vector3(0, pos.y, 0));
                colors.Add(ColorUtility.HSVToRGB(i * 360.0f / m_agentCount, 0.8f, 0.6f));
            }
        }
        else if (type == RVOExampleType.RandomStreams)
        {
            float circleRad = Mathf.Sqrt(m_agentCount * m_radius * m_radius * 4 / Mathf.PI) * exampleScale * 0.05f;

            for (int i = 0; i < m_agentCount; i++)
            {
                float   angle       = Random.value * Mathf.PI * 2.0f;
                float   targetAngle = Random.value * Mathf.PI * 2.0f;
                Vector3 pos         = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * uniformDistance(circleRad);
                var     id          = sim.addAgent(new RVO.Vector2(pos.x, pos.z));
                m_agentIds.Add(id);
                goals.Add(id, new Vector3(Mathf.Cos(targetAngle), 0, Mathf.Sin(targetAngle)) * uniformDistance(circleRad));
                colors.Add(ColorUtility.HSVToRGB(targetAngle * Mathf.Rad2Deg, 0.8f, 0.6f));
            }
        }
        else if (type == RVOExampleType.Crossing)
        {
            float distanceBetweenGroups = exampleScale * m_radius * 0.5f;
            int   directions            = (int)Mathf.Sqrt(m_agentCount / 25f);
            directions = Mathf.Max(directions, 2);

            const int AgentsPerDistance = 10;
            for (int i = 0; i < m_agentCount; i++)
            {
                float   angle = ((i % directions) / (float)directions) * Mathf.PI * 2.0f;
                var     dist  = distanceBetweenGroups * ((i / (directions * AgentsPerDistance) + 1) + 0.3f * Random.value);
                Vector3 pos   = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * dist;
                var     id    = sim.addAgent(new RVO.Vector2(pos.x, pos.z));
                m_agentIds.Add(id);
                goals.Add(id, -pos.normalized * distanceBetweenGroups * 3);
                colors.Add(ColorUtility.HSVToRGB(angle * Mathf.Rad2Deg, 0.8f, 0.6f));
            }
        }

        verts      = new Vector3[4 * m_agentCount];
        uv         = new Vector2[verts.Length];
        tris       = new int[m_agentCount * 2 * 3];
        meshColors = new Color[verts.Length];
    }