Пример #1
0
        static void setPreferredVelocities(RVOSimulator sim)
        {
            /*
               * Set the preferred velocity to be a vector of unit magnitude (speed) in the
               * direction of the goal.
               */
            #pragma omp parallel for
              for (int i = 0; i < sim.getNumAgents(); ++i) {
                Vector2 goalVector = goals[i] - sim.getAgentPosition(i);

                if ((goalVector.LengthSquared()) > 1.0f) {
                  goalVector = Vector2.Normalize(goalVector);
                }

                sim.setAgentPrefVelocity(i, goalVector);

                /*
                 * Perturb a little to avoid deadlocks due to perfect symmetry.
                 */

                float angle = (float)r.NextDouble() * 2.0f * (float)Math.PI ;
                float dist = (float)r.NextDouble() * 0.0001f ;

                sim.setAgentPrefVelocity(i, sim.getAgentPrefVelocity(i) +
                                          dist * new Vector2((float) Math.Cos(angle), (float)Math.Sin(angle)));
              }
        }
Пример #2
0
 internal SuperAgent(RVOSimulator sim)
 {
     sim_      = sim;
     position_ = new Vector2();
     radius_   = 1.0f;
     velocity_ = new Vector2();
 }
Пример #3
0
    public override void OnLatePostScan()
    {
        if (!Application.isPlaying)
        {
            return;
        }

        RemoveObstacles();

        NavGraph[] graphs = AstarPath.active.graphs;

        RVOSimulator rvosim = FindObjectOfType(typeof(RVOSimulator)) as RVOSimulator;

        if (rvosim == null)
        {
            throw new System.NullReferenceException("No RVOSimulator could be found in the scene. Please add one to any GameObject");
        }

        Pathfinding.RVO.Simulator sim = rvosim.GetSimulator();

        for (int i = 0; i < graphs.Length; i++)
        {
            AddGraphObstacles(sim, graphs[i]);
        }

        sim.UpdateObstacles();
    }
Пример #4
0
    /** <summary>Create a DataThread object given necessary argument </summary>
     * <param name="follow" Whether the simulation is looped or not/>
     *  <param name="looped" Whether the simulation is looped or not/>
     *  <param name="sim" The simulator from which data have to be saved />*/
    public DataThread(RVOSimulator sim, bool looped, bool follow)
    {
        sim_    = sim;
        loop_   = looped;
        follow_ = follow;

        Thread myThread = new Thread(new ThreadStart(ThreadLoop));

        myThread.Start();
    }
Пример #5
0
    /** Finds a simulator in the scene.
     *
     * Saves found simulator in #sim.
     *
     * \throws System.InvalidOperationException When no RVOSimulator could be found.
     */
    protected void FindSimulator()
    {
        RVOSimulator rvosim = FindObjectOfType(typeof(RVOSimulator)) as RVOSimulator;

        if (rvosim == null)
        {
            throw new System.InvalidOperationException("No RVOSimulator could be found in the scene. Please add one to any GameObject");
        }

        sim = rvosim.GetSimulator();
    }
Пример #6
0
		public void Start()
		{
			this.cam = Camera.main;
			RVOSimulator rvosimulator = UnityEngine.Object.FindObjectOfType(typeof(RVOSimulator)) as RVOSimulator;
			if (rvosimulator == null)
			{
				base.enabled = false;
				throw new Exception("No RVOSimulator in the scene. Please add one");
			}
			this.sim = rvosimulator.GetSimulator();
		}
Пример #7
0
        static bool reachedGoal(RVOSimulator sim)
        {
            /* Check if all agents have reached their goals. */
              for (int i = 0; i < sim.getNumAgents(); ++i) {
                if ((sim.getAgentPosition(i) - goals[i]).LengthSquared() > 20.0f * 20.0f) {
                  return false;
                }
              }

              return true;
        }
Пример #8
0
        static void setPreferredVelocities(RVOSimulator sim)
        {
            for (int i = 0; i < sim.getNumAgents(); ++i) {
                Vector3 goalVector = goals[i] - sim.getAgentPosition(i);

                if ((goalVector.LengthSquared()) > 1.0f) {
                    goalVector = Vector3.Normalize(goalVector);
                }

                sim.setAgentPrefVelocity(i, goalVector);
            }
        }
Пример #9
0
    public void Awake()
    {
        tr = transform;

        RVOSimulator sim = FindObjectOfType(typeof(RVOSimulator)) as RVOSimulator;

        if (sim == null)
        {
            Debug.LogError("No RVOSimulator component found in the scene. Please add one.");
            return;
        }
        simulator = sim.GetSimulator();
    }
Пример #10
0
		public void Start()
		{
			this.mesh = new Mesh();
			RVOSimulator rvosimulator = UnityEngine.Object.FindObjectOfType(typeof(RVOSimulator)) as RVOSimulator;
			if (rvosimulator == null)
			{
				Debug.LogError("No RVOSimulator could be found in the scene. Please add a RVOSimulator component to any GameObject");
				return;
			}
			this.sim = rvosimulator.GetSimulator();
			base.GetComponent<MeshFilter>().mesh = this.mesh;
			this.CreateAgents(this.agentCount);
		}
Пример #11
0
    public void UpdateLogic(int delta)
    {
        //pathFinding.Update();
        //Int3 i3 = new Int3(20, 0, 0);
        //Int1 y = 0;
        //actor.ActorTransform.position += (Vector3)PathfindingUtility.Move(actor, i3, out y);
        MyTileHandlerHelper.Singleton.Update();
        // controller.Move(new Int3(0,0,5000));
        // controller.UpdateLogic(delta);

        ActorManager.Singleton.UpdateLogic(delta);

        RVOSimulator.GetInstance().UpdateLogic(delta);
    }
Пример #12
0
    public void Start()
    {
        mesh = new Mesh();
        RVOSimulator rvoSim = FindObjectOfType(typeof(RVOSimulator)) as RVOSimulator;

        if (rvoSim == null)
        {
            Debug.LogError("No RVOSimulator could be found in the scene. Please add a RVOSimulator component to any GameObject");
            return;
        }
        sim = rvoSim.GetSimulator();
        GetComponent <MeshFilter>().mesh = mesh;

        CreateAgents(agentCount);
    }
Пример #13
0
        static void Main(string[] args)
        {
            /* Create a new simulator instance. */
            RVOSimulator sim = new RVOSimulator();

            /* Set up the scenario. */
            setupScenario(sim);

            /* Perform (and manipulate) the simulation. */
            do
            {
                updateVisualization(sim);
                setPreferredVelocities(sim);
                sim.doStep();
            } while (!reachedGoal(sim));
        }
Пример #14
0
    // Use this for initialization
    IEnumerator Start()
    {
        if (target == null)
        {
            target = transform;
        }

        currentNodeInThePath = 0;
        simulator            = GameObject.FindGameObjectWithTag("RVOSim").GetComponent <RVOSimulator>();
        pathNodes            = new List <Vector3>();
        yield return(StartCoroutine(StartPaths()));

        if (agentIndex == -1)
        {
            agentIndex = simulator.addAgentToSim(transform.position, gameObject, pathNodes);
        }
        isAbleToStart = true;
    }
Пример #15
0
    IEnumerator Start()
    {
        currentNodeInThePath = 0;
        simulator            = GameObject.FindGameObjectWithTag("RVOSim").GetComponent <RVOSimulator>();
        pathNodes            = new List <Vector3>();
        yield return(StartCoroutine(StartPaths()));

        agentIndex = simulator.addAgentToSim(transform.position, gameObject, pathNodes);

        if (updatePathtime <= 0)
        {
            Debug.Log("Incorrect Value of updatePathtime, setted to 1");
            updatePathtime = 1;
        }
        updatePathtime_tmp = updatePathtime;

        isAbleToStart = true;
    }
Пример #16
0
    public void Awake()
    {
        tr = transform;

        // Find the RVOSimulator in this scene
        if (cachedSimulator == null)
        {
            cachedSimulator = FindObjectOfType <RVOSimulator>();
        }

        if (cachedSimulator == null)
        {
            Debug.LogError("No RVOSimulator component found in the scene. Please add one.");
        }
        else
        {
            simulator = cachedSimulator.GetSimulator();
        }
    }
Пример #17
0
        static void setupScenario(RVOSimulator sim)
        {
            /* Specify the global time step of the simulation. */
            sim.setTimeStep(0.125f);

            /* Specify the default parameters for agents that are subsequently added. */
            sim.setAgentDefaults(15.0f, 10, 10.0f, 1.5f, 2.0f,new Vector3());

            /* Add agents, specifying their start position, and store their goals on the opposite side of the environment. */
            for (float a = 0; a < Math.PI; a += 0.1f) {
                float z = 100.0f * (float)Math.Cos(a);
                float r = 100.0f * (float)Math.Sin(a);

                for (int i = 0; i < r / 2.5f; ++i) {
                    float x = r * (float)Math.Cos(i * 2.0f * (float)Math.PI / (r / 2.5f));
                    float y = r * (float)Math.Sin(i * 2.0f * (float)Math.PI / (r / 2.5f));

                    sim.addAgent(new Vector3(x, y, z));
                    goals.Add(sim.getAgentPosition(sim.getNumAgents() - 1));
                }
            }
        }
Пример #18
0
    public static Simulator GetSimulator()
    {
        if (null == AstarPath.active)
        {
            EB.Debug.LogError("No AstarPath component found in the scene.");
            return(null);
        }

        RVOSimulator sim = AstarPath.active.gameObject.GetComponent <RVOSimulator>();

        if (sim == null)
        {
            EB.Debug.LogError("No RVOSimulator component found in the scene. Please add one.");
            return(null);
        }
        Simulator simulator = sim.GetSimulator();

        if (simulator == null)
        {
            EB.Debug.LogError("No Simulator component found in the scene. Please add one.");
            return(null);
        }
        return(simulator);
    }
Пример #19
0
    // Update is called once per frame
    void Update()
    {
        if (group_b != group.isOn || avoidance_b != avoidance.isOn)
        {
            group_b     = group.isOn;
            avoidance_b = avoidance.isOn;
            int range = agents.Count;
            for (int i = 0; i < range; i++)
            {
                Destroy(agents[i].gameObject);
            }
            agents.Clear();
            sim_ = new RVOSimulator();
            Start();
        }

        if (!reachedGoal())
        {
            /*Changing preferred velocity*/
            setPreferredVelocities();
            /* Doing a step*/
            doStep(false);
            /*Printing Time  * May harm performances */
            //Debug.Log(sim_.getGlobalTime());
            // This loop will change position,  facing direction of the 3D agents on the unity scene
            // They will move accordingly to the goal you gave them in the Setup
            // you can also change their position in order to create a mouvement loop
            for (int i = 0; i < getNumAgents(); ++i)
            {
                RVO.Vector2 position = getPosition(i);
                // Determine if the agent is in the group One ...
                if (i < getNumAgents() / 2)
                {
                    if (position.x() >= 90.0f)
                    {
                        RVO.Vector2 vector2 = sim_.getAgentVelocity(i);
                        agents[i].rotation = Quaternion.LookRotation(new Vector3(vector2.x_, 0, vector2.y_));
                        setPosition(i, new RVO.Vector2(-89, position.y()));
                        agents[i].position = new Vector3(-89, 0f, position.y());
                    }
                    else
                    {
                        RVO.Vector2 vector2 = sim_.getAgentVelocity(i);
                        agents[i].rotation = Quaternion.LookRotation(new Vector3(vector2.x_, 0, vector2.y_));
                        agents[i].position = new Vector3(position.x(), 0f, position.y());
                    }
                }
                //... Or in the group Two
                else
                {
                    if (position.x() <= -90.0f)
                    {
                        setPosition(i, new RVO.Vector2(89, position.y()));
                        RVO.Vector2 vector2 = sim_.getAgentVelocity(i);
                        agents[i].rotation = Quaternion.LookRotation(new Vector3(vector2.x_, 0, vector2.y_));
                        agents[i].position = new Vector3(89, 0f, position.y());
                    }
                    else
                    {
                        RVO.Vector2 vector2 = sim_.getAgentVelocity(i);
                        agents[i].rotation = Quaternion.LookRotation(new Vector3(vector2.x_, 0, vector2.y_));
                        agents[i].position = new Vector3(position.x(), 0f, position.y());
                    }
                }
            }
        }
        else
        {
            for (int i = 0; i < getNumAgents(); ++i)
            {
                agents[i].GetComponent <Rigidbody>().isKinematic = true;
            }
        }
    }
Пример #20
0
 internal static HandleRef getCPtr(RVOSimulator obj)
 {
     return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
Пример #21
0
        static void setupScenario(RVOSimulator sim)
        {
            /* Specify the global time step of the simulation. */
              sim.setTimeStep(0.25f);

              /* Specify the default parameters for agents that are subsequently added. */
              sim.setAgentDefaults(15.0f, 10, 5.0f, 5.0f, 2.0f, 2.0f,new Vector2());

              /*
               * Add agents, specifying their start position, and store their goals on the
               * opposite side of the environment.
               */
              for (int i = 0; i < 5; ++i) {
                for (int j = 0; j < 5; ++j) {
                  sim.addAgent(new Vector2(55.0f + i * 10.0f,  55.0f + j * 10.0f));
                  goals.Add(new Vector2(-75.0f, -75.0f));

                  sim.addAgent(new Vector2(-55.0f - i * 10.0f,  55.0f + j * 10.0f));
                  goals.Add(new Vector2(75.0f, -75.0f));

                  sim.addAgent(new Vector2(55.0f + i * 10.0f, -55.0f - j * 10.0f));
                  goals.Add(new Vector2(-75.0f, 75.0f));

                  sim.addAgent(new Vector2(-55.0f - i * 10.0f, -55.0f - j * 10.0f));
                  goals.Add(new Vector2(75.0f, 75.0f));
                }
              }

              /*
               * Add (polygonal) obstacles, specifying their vertices in counterclockwise
               * order.
               */
                List<Vector2> obstacle1 = new List<Vector2>();
                List<Vector2>  obstacle2= new List<Vector2>();
                List<Vector2>  obstacle3= new List<Vector2>();
                List<Vector2>  obstacle4= new List<Vector2>();

              obstacle1.Add(new Vector2(-10.0f, 40.0f));
              obstacle1.Add(new Vector2(-40.0f, 40.0f));
              obstacle1.Add(new Vector2(-40.0f, 10.0f));
              obstacle1.Add(new Vector2(-10.0f, 10.0f));

              obstacle2.Add(new Vector2(10.0f, 40.0f));
              obstacle2.Add(new Vector2(10.0f, 10.0f));
              obstacle2.Add(new Vector2(40.0f, 10.0f));
              obstacle2.Add(new Vector2(40.0f, 40.0f));

              obstacle3.Add(new Vector2(10.0f, -40.0f));
              obstacle3.Add(new Vector2(40.0f, -40.0f));
              obstacle3.Add(new Vector2(40.0f, -10.0f));
              obstacle3.Add(new Vector2(10.0f, -10.0f));

              obstacle4.Add(new Vector2(-10.0f, -40.0f));
              obstacle4.Add(new Vector2(-10.0f, -10.0f));
              obstacle4.Add(new Vector2(-40.0f, -10.0f));
              obstacle4.Add(new Vector2(-40.0f, -40.0f));

              sim.addObstacle(obstacle1);
              sim.addObstacle(obstacle2);
              sim.addObstacle(obstacle3);
              sim.addObstacle(obstacle4);

              /* Process the obstacles so that they are accounted for in the simulation. */
              sim.processObstacles();
        }
Пример #22
0
 static void updateVisualization(RVOSimulator sim)
 {
     /* Output the current position of all the agents. */
       for (int i = 0; i < sim.getNumAgents(); ++i) {
         Console.WriteLine(sim.getAgentPosition(i) );
       }
 }