AddAgent() public method

public AddAgent ( IAgent agent ) : IAgent
agent IAgent
return IAgent
        public void OnEnable()
        {
            //We might have an rvoAgent
            //which was disabled previously
            //if so, we can simply add it to the simulation again
            if (rvoAgent != null)
            {
                simulator.AddAgent(rvoAgent);
            }
            else
            {
                rvoAgent = simulator.AddAgent(transform.position);
            }

            UpdateAgentProperties();
            rvoAgent.Position = transform.position;
            adjustedY         = rvoAgent.Position.y;
        }
Exemplo n.º 2
0
    // Use this for initialization
    public void Start()
    {
#if RVOImp
        if (!astarRVO)
        {
//#if !AstarRelease
            agentID = RVO.Simulator.Instance.addAgent(new RVO.Vector2(transform.position.x, transform.position.z));
//#endif
        }
        else
        {
            Pathfinding.RVO.Simulator sim = (FindObjectOfType(typeof(RVOSimulator)) as RVOSimulator).GetSimulator();
            rvoAgent                     = sim.AddAgent(transform.position);
            rvoAgent.Radius              = radius;
            rvoAgent.MaxSpeed            = maxSpeed;
            rvoAgent.Height              = height;
            rvoAgent.AgentTimeHorizon    = agentTimeHorizon;
            rvoAgent.ObstacleTimeHorizon = obstacleTimeHorizon;
        }
#endif
        SetTarget(-transform.position);         // + transform.forward * 400);
    }
Exemplo n.º 3
0
    /** Create a number of agents in circle and restart simulation */
    public void CreateAgents(int num)
    {
        this.agentCount = num;

        agents = new List <IAgent>(agentCount);
        goals  = new List <Vector3>(agentCount);
        colors = new List <Color>(agentCount);

        sim.ClearAgents();

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

            for (int i = 0; i < agentCount; i++)
            {
                Vector3 pos   = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / agentCount)) * circleRad;
                IAgent  agent = sim.AddAgent(pos);
                agents.Add(agent);
                goals.Add(-pos);
                colors.Add(HSVToRGB(i * 360.0f / agentCount, 0.8f, 0.6f));
            }
        }
        else if (type == RVOExampleType.Line)
        {
            for (int i = 0; i < agentCount; i++)
            {
                Vector3 pos   = new Vector3((i % 2 == 0 ? 1 : -1) * exampleScale, 0, (i / 2) * radius * 2.5f);
                IAgent  agent = sim.AddAgent(pos);
                agents.Add(agent);
                goals.Add(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 < agentCount; i++)
            {
                Vector3 pos   = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / agentCount)) * exampleScale;
                IAgent  agent = sim.AddAgent(pos);
                agents.Add(agent);
                goals.Add(new Vector3(0, pos.y, 0));
                colors.Add(HSVToRGB(i * 360.0f / agentCount, 0.8f, 0.6f));
            }
        }
        else if (type == RVOExampleType.RandomStreams)
        {
            float circleRad = Mathf.Sqrt(agentCount * radius * radius * 4 / Mathf.PI) * exampleScale * 0.05f;

            for (int i = 0; i < 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);
                IAgent  agent       = sim.AddAgent(pos);
                agents.Add(agent);
                goals.Add(new Vector3(Mathf.Cos(targetAngle), 0, Mathf.Sin(targetAngle)) * uniformDistance(circleRad));
                colors.Add(HSVToRGB(targetAngle * Mathf.Rad2Deg, 0.8f, 0.6f));
            }
        }

        for (int i = 0; i < agents.Count; i++)
        {
            IAgent agent = agents[i];
            agent.Radius              = radius;
            agent.MaxSpeed            = maxSpeed;
            agent.AgentTimeHorizon    = agentTimeHorizon;
            agent.ObstacleTimeHorizon = obstacleTimeHorizon;
            agent.MaxNeighbours       = maxNeighbours;
            agent.NeighbourDist       = neighbourDist;
            agent.DebugDraw           = i == 0 && debug;
        }

        verts      = new Vector3[4 * agents.Count];
        uv         = new Vector2[verts.Length];
        tris       = new int[agents.Count * 2 * 3];
        meshColors = new Color[verts.Length];
    }
Exemplo n.º 4
0
        /// <summary>Create a number of agents in circle and restart simulation</summary>
        public void CreateAgents(int num)
        {
            this.agentCount = num;

            agents = new List <IAgent>(agentCount);
            goals  = new List <Vector3>(agentCount);
            colors = new List <Color>(agentCount);

            sim.ClearAgents();

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

                for (int i = 0; i < agentCount; i++)
                {
                    Vector3 pos   = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / agentCount)) * circleRad * (1 + Random.value * 0.01f);
                    IAgent  agent = sim.AddAgent(new Vector2(pos.x, pos.z), pos.y);
                    agents.Add(agent);
                    goals.Add(-pos);
                    colors.Add(AstarMath.HSVToRGB(i * 360.0f / agentCount, 0.8f, 0.6f));
                }
            }
            else if (type == RVOExampleType.Line)
            {
                for (int i = 0; i < agentCount; i++)
                {
                    Vector3 pos   = new Vector3((i % 2 == 0 ? 1 : -1) * exampleScale, 0, (i / 2) * radius * 2.5f);
                    IAgent  agent = sim.AddAgent(new Vector2(pos.x, pos.z), pos.y);
                    agents.Add(agent);
                    goals.Add(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 < agentCount; i++)
                {
                    Vector3 pos   = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / agentCount)) * exampleScale;
                    IAgent  agent = sim.AddAgent(new Vector2(pos.x, pos.z), pos.y);
                    agents.Add(agent);
                    goals.Add(new Vector3(0, pos.y, 0));
                    colors.Add(AstarMath.HSVToRGB(i * 360.0f / agentCount, 0.8f, 0.6f));
                }
            }
            else if (type == RVOExampleType.RandomStreams)
            {
                float circleRad = Mathf.Sqrt(agentCount * radius * radius * 4 / Mathf.PI) * exampleScale * 0.05f;

                for (int i = 0; i < 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);
                    IAgent  agent       = sim.AddAgent(new Vector2(pos.x, pos.z), pos.y);
                    agents.Add(agent);
                    goals.Add(new Vector3(Mathf.Cos(targetAngle), 0, Mathf.Sin(targetAngle)) * uniformDistance(circleRad));
                    colors.Add(AstarMath.HSVToRGB(targetAngle * Mathf.Rad2Deg, 0.8f, 0.6f));
                }
            }
            else if (type == RVOExampleType.Crossing)
            {
                float distanceBetweenGroups = exampleScale * radius * 0.5f;
                int   directions            = (int)Mathf.Sqrt(agentCount / 25f);
                directions = Mathf.Max(directions, 2);

                const int AgentsPerDistance = 10;
                for (int i = 0; i < 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;
                    IAgent  agent = sim.AddAgent(new Vector2(pos.x, pos.z), pos.y);
                    agent.Priority = (i % directions) == 0 ? 1 : 0.01f;
                    agents.Add(agent);
                    goals.Add(-pos.normalized * distanceBetweenGroups * 3);
                    colors.Add(AstarMath.HSVToRGB(angle * Mathf.Rad2Deg, 0.8f, 0.6f));
                }
            }

            SetAgentSettings();

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