Defines a two-dimensional vector.
Ejemplo n.º 1
0
    void move()
    {
        if (interactScript.isSit)
        {
            return;
        }
        RVO.Vector2         agentVelRVO = Simulator.Instance.getAgentVelocity(agentId);
        RVO.Vector2         agentPosRVO = Simulator.Instance.getAgentPosition(agentId);
        UnityEngine.Vector2 agentVel    = toUnityVector2(agentVelRVO);
        Vector3             agentPos    = toUnityVector(agentPosRVO);
        float remDist = Vector3.Distance(agentPos, agent.destination);

        shouldMove = agentVel.magnitude * velFactor > 0.01f && remDist > agent.radius;

        float velx = Mathf.Lerp(-0.5f, 0.5f, agentVelRVO.x());

        velx *= transform.forward.x;


        float vely = Mathf.Lerp(-0.5f, 0.5f, agentVelRVO.y());

        vely *= transform.forward.z;

        // Update animation parameters
        anim.SetBool("move", shouldMove);
        anim.SetFloat("velx", velx);
        anim.SetFloat("vely", vely);

        checkPeople(); // look at near people
    }
Ejemplo n.º 2
0
 public RVO.Vector2 VectorConvert(Vector3 vector)
 {
     RVO.Vector2 _vector = new RVO.Vector2 ();
     _vector.x_ = vector.x;
     _vector.y_ = vector.z;
     return _vector;
 }
Ejemplo n.º 3
0
    /**
     * <summary>Unity function. Called once at the beginning of the scenario. Used to initialize everything you need</summary>
     */
    void Start()
    {
        // Change the maximum framerate of the simulation
        Application.targetFrameRate = 60;
        //Initialisation of the list
        agents = new List <Transform>();
        // Set up the scenario.
        setupScenario();

        //Creation of 2 walls on both side of the corridor
        //Instantiate(wall, new Vector3(0, 0f, 0f), Quaternion.identity);
        //Instantiate(wall, new Vector3(0,0f, 30f), Quaternion.identity);

        //For each agents added in setupScenario() , add a Unity Agent at the same position
        for (int i = 0; i < getNumAgents(); ++i)
        {
            RVO.Vector2 position = getPosition(i);
            agents.Add(Instantiate(agent, new Vector3(position.x(), sim_.getAgentRadius(i) / 2, position.y()), Quaternion.identity));
            agents[i].localScale = new Vector3(sim_.getAgentRadius(i), sim_.getAgentRadius(i), sim_.getAgentRadius(i));
        }
        //Filling the colors table
        colors.Add(new Color(0.000f, 0.000f, 0.804f));
        colors.Add(new Color(0.118f, 0.565f, 1.000f));
        colors.Add(new Color(0.251f, 0.878f, 0.816f));
        colors.Add(new Color(0.400f, 0.804f, 0.667f));
        colors.Add(new Color(0.604f, 0.804f, 0.196f));
    }
Ejemplo n.º 4
0
    // Update is called once per frame
    void Update()
    {
        if (!reachedGoal())
        {
            setPreferredVelocities();
            doStep(false);

            /* Output the current global time. */
            //print(Simulator.Instance.getGlobalTime());
            for (int i = 0; i < getNumAgents(); ++i)
            {
                RVO.Vector2 position = getPosition(i);
                agents[i].position = new Vector3(position.x(), 0f, position.y());

                /*  RVO.Vector2 vector2 = sim_.getAgentVelocity(i);
                 * agents[i].rotation = Quaternion.LookRotation(new Vector3(vector2.x_, 0, vector2.y_));*/
                //setColor(i); To go further
            }
        }
        else
        {
            for (int i = 0; i < getNumAgents(); ++i)
            {
                agents[i].GetComponent <Rigidbody>().isKinematic = true;
            }
        }
    }
Ejemplo n.º 5
0
    void OnDrawGizmos()
    {
        if (!Debug)
        {
            return;
        }
        Gizmos.color = Color.red;

        // ORCA
        Vector3 from;
        Vector3 to;

        foreach (Line msGizmosLine in msGizmosLines)
        {
            RVO.Vector2 from_ = msGizmosLine.point - msGizmosLine.direction * 100;
            RVO.Vector2 to_   = msGizmosLine.point + msGizmosLine.direction * 100;

            from = transform.position + new Vector3(from_.x(), 0, from_.y());
            to   = transform.position + new Vector3(to_.x(), 0, to_.y());

            Gizmos.DrawLine(from, to);
        }
        msGizmosLines.Clear();
        // velocity
        Gizmos.color = Color.green;

        from = transform.position;
        to   = transform.position + new Vector3(msVelocity.x(), 0, msVelocity.y());

        Gizmos.DrawLine(from, to);
    }
Ejemplo n.º 6
0
    // Update is called once per frame
    void Update()
    {
        int agentNum = Simulator.Instance.getNumAgents();

        Debug.Log("simulatorAir agent number = " + agentNum.ToString());
        try
        {
            for (int i = 0; i < agentNum; i++)
            {
                RVO.Vector2 agentPos   = Simulator.Instance.getAgentPosition(i);
                RVO.Vector2 goalVector = rvoGameObjs[i].GetComponent <RVO_AirAgentMove>().goalVector();

                if (RVOMath.absSq(goalVector) > thresholdToMove)
                {
                    goalVector = goalVector * speed_target;
                }

                Simulator.Instance.setAgentPrefVelocity(i, goalVector);

                rvoGameObjs[i].transform.position = toUnityPosition(agentPos);
            }
            Simulator.Instance.doStep();
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex.StackTrace);
        }
    }
Ejemplo n.º 7
0
    // Update is called once per frame
    void Update()
    {
        int agentNumber = Simulator.Instance.getNumAgents();

        try
        {
            for (int i = 0; i < agentNumber; i++)
            {
                RVO.Vector2 agentLoc = Simulator.Instance.getAgentPosition(i);
                RVO.Vector2 station  = rvoGameObj[i].GetComponent <RVOAgent>().calculateNextStation() - agentLoc;

                if (RVOMath.absSq(station) > 1.0f)
                {
                    station = RVOMath.normalize(station);
                }

                Simulator.Instance.setAgentPrefVelocity(i, station);
                agentPositions[i] = Simulator.Instance.getAgentPosition(i);
                //Debug.Log(agentLoc + "  ///  " + agentPositions[i]);
            }
            Simulator.Instance.doStep();
        }
        catch (System.Exception ex) {
            Debug.Log("Exeption: " + ex.Message);
        }
    }
Ejemplo n.º 8
0
    // Update is called once per frame
    void Update()
    {
        int agentNum = Simulator.Instance.getNumAgents();

        try
        {
            for (int i = 0; i < agentNum; i++)
            {
                RVO.Vector2 agentPos   = Simulator.Instance.getAgentPosition(i);
                RVO.Vector2 goalVector = rvoGameObjs[i].GetComponent <RVO_Agent>().nextPathNode() - agentPos;

                if (RVOMath.absSq(goalVector) > thresholdToMove)
                {
                    goalVector = RVOMath.normalize(goalVector) * speed_target;
                }

                Simulator.Instance.setAgentPrefVelocity(i, goalVector);

                rvoGameObjs[i].transform.position = toUnityPosition(agentPos);
            }
            Simulator.Instance.doStep();
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex.StackTrace);
        }
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        rallyIsReady = GameObject.FindGameObjectWithTag("Manager").GetComponent <UI_ButtonControl>().SpawnIsDone;
        if (rallyIsReady)
        {
            int agentNum = Simulator.Instance.getNumAgents();
            try
            {
                for (int i = 0; i < agentNum; i++)
                {
                    RVO.Vector2 agentPos   = Simulator.Instance.getAgentPosition(i);
                    RVO.Vector2 goalVector = rvoGameObjs[i].GetComponent <RVO_Agent>().nextPathNode() - agentPos;

                    if (RVOMath.absSq(goalVector) > thresholdToMove)
                    {
                        goalVector = RVOMath.normalize(goalVector) * speed_target;
                    }

                    Simulator.Instance.setAgentPrefVelocity(i, goalVector);

                    rvoGameObjs[i].transform.position = toUnityPosition(agentPos);
                }
                Simulator.Instance.doStep();
            }
            catch (System.Exception ex)
            {
                Debug.Log(ex.StackTrace);
            }
        }
    }
Ejemplo n.º 10
0
    // Use this for initialization
    void Start()
    {
        group_b     = group.isOn;
        avoidance_b = avoidance.isOn;
        Application.targetFrameRate = 60;
        agents = new List <Transform>();
        /* Set up the scenario. */
        setupScenario();
        Instantiate(wall, new Vector3(0, 12.5f, 40.5f), Quaternion.identity);
        Instantiate(wall, new Vector3(0, 12.5f, -39.5f), Quaternion.identity);

        /* Perform (and manipulate) the simulation. */
        for (int i = 0; i < getNumAgents(); ++i)
        {
            RVO.Vector2 position = getPosition(i);
            int         mid      = (group_b ? getNumAgents() / 2 : 1);

            if (i < mid)
            {
                addAgent(agent_rose, new Vector3(position.x(), 0f, position.y()), sim_.getDefaultRadius());
            }
            else
            {
                addAgent(agent_vert, new Vector3(position.x(), 0f, position.y()), sim_.getDefaultRadius());
            }
        }
    }
Ejemplo n.º 11
0
    void setPreferredVelocities()
    {
        /*
         * Set the preferred velocity to be a vector of unit magnitude
         * (speed) in the direction of the goal.
         */
        for (int i = 0; i < Simulator.Instance.getNumAgents(); ++i)
        {
            Vector2 goalVector = goals[i] - Simulator.Instance.getAgentPosition(i);

            if (RVOMath.absSq(goalVector) > 1.0f)
            {
                goalVector = RVOMath.normalize(goalVector);
            }

            Simulator.Instance.setAgentPrefVelocity(i, goalVector);

            /* Perturb a little to avoid deadlocks due to perfect symmetry. */
            float angle = (float)random.NextDouble() * 2.0f * (float)Math.PI;
            float dist  = (float)random.NextDouble() * 0.0001f;

            Simulator.Instance.setAgentPrefVelocity(i, Simulator.Instance.getAgentPrefVelocity(i) +
                                                    dist * new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)));
        }
    }
Ejemplo n.º 12
0
    RVO.Vector2 rotation(RVO.Vector2 vector, float angle)
    {
        float x_prime = (float)Math.Cos(angle) * vector.x() - (float)Math.Sin(angle) * vector.y();
        float y_prime = (float)Math.Sin(angle) * vector.x() + (float)Math.Cos(angle) * vector.y();

        return(new RVO.Vector2(x_prime, y_prime));
    }
Ejemplo n.º 13
0
    // Update is called once per frame
    void Update()
    {
        if ((m_destPos - transform.position).sqrMagnitude <= 0.1)
        {
            if (m_cornerIndex == path.corners.Count() - 1 || path.corners.Count() == 0)
            {
                Stop();
            }
            else
            {
                m_cornerIndex++;
                m_destPos = path.corners[m_cornerIndex];
            }
            return;
        }

        //moving...
        Vector3 dir = m_destPos - transform.position;

        RVO.Vector2 rdir = new RVO.Vector2(dir.x, dir.z);
        rdir = RVO.RVOMath.normalize(rdir);
        RVO.Simulator.Instance.setAgentPrefVelocity(m_agentHandle, rdir);
        RVO.Vector2 velocityInSimulator = RVO.Simulator.Instance.getAgentVelocity(m_agentHandle);
        transform.forward = new Vector3(velocityInSimulator.x_, 0, velocityInSimulator.y_).normalized;
    }
Ejemplo n.º 14
0
        void calculateVandS()
        {
            Vector2 pos_new = Simulator.Instance.getAgentPosition(0);

            double car_direction = mainCar.transform.eulerAngles.y / 180.0 * Math.PI;

            double dx = pos_new.x() - mainCar.transform.position.x;
            double dz = pos_new.y() - mainCar.transform.position.z;
            double target_direction = -Math.Atan2(dz, dx) + Math.PI / 2;

            double delta_direction = target_direction - car_direction;

            while (delta_direction > Math.PI)
            {
                delta_direction -= Math.PI * 2;
            }
            while (delta_direction < -Math.PI)
            {
                delta_direction += Math.PI * 2;
            }

            SteeringAngle = (float)(delta_direction / Math.PI * 180.0 / m_Car.MaxSteerAngleInDegree);

//             double dt_direction = car_direction - pre_direction;
//             pre_direction = car_direction;
//             Debug.Log("dt_direction " + (dt_direction * 50 / Math.PI * 180.0).ToString());
//             Debug.Log("delta_direction " + delta_direction.ToString());
//             Debug.Log("SteeringAngle " + (SteeringAngle * m_Car.MaxSteerAngleInDegree).ToString());
            SteeringAngle = Mathf.Clamp(SteeringAngle, -1, 1);

            float   current_speed    = m_Car.CurrentSpeed;
            var     desired_velocity = Simulator.Instance.getAgentVelocity(0);
            Vector3 des_v            = new Vector3(desired_velocity.x(), 0, desired_velocity.y());
            float   desired_speed    = des_v.magnitude;

            //             Debug.Log("pos_new " + pos_new.ToString());
            //             Debug.Log("current_speed " + current_speed.ToString());
            //             Debug.Log("desired_speed " + desired_speed.ToString());
            //
            //             double zz = Math.Sqrt( dx * dx + dz * dz) * 50;
            //             Debug.Log("zz " + zz.ToString());

            Acceleration = (desired_speed - current_speed) / 0.02f;
            if (Acceleration > 1)
            {
                Acceleration = 1;
            }
            if (Acceleration < 0)
            {
                if (desired_speed - current_speed > -1)
                {
                    Acceleration = 0;
                }
            }
            //Debug.Log("frameIdx " + frameIdx.ToString() + " Acceleration " + Acceleration.ToString());

            frameIdx++;
            //if (frameIdx > 100) Acceleration = 0;
        }
Ejemplo n.º 15
0
    private Vector3 GetSkillPos(Skill _skill)
    {
        RVO.Vector2 tmpPos = _skill.pos;

        int fix = battle.clientIsMine ? 1 : -1;

        return(new Vector3((float)tmpPos.x * fix, 0, (float)tmpPos.y * fix));
    }
Ejemplo n.º 16
0
    public Vector3 GetUnitPos(Unit _unit)
    {
        int fix = battle.clientIsMine ? 1 : -1;

        float y = _unit.sds.GetUnitType() == UnitType.AIR_UNIT ? AIR_UNIT_Y : 0f;

        RVO.Vector2 tmpPos = _unit.pos;

        return(new Vector3((float)tmpPos.x * fix, y, (float)tmpPos.y * fix));
    }
Ejemplo n.º 17
0
    public static Vector2 GetDirection(RVOAgent agent)
    {
        Vector2 nowVelocity = Simulator.Instance.getAgentVelocity(agent.GetIndex());

        if (RVOMath.abs(nowVelocity) < eps)
        {
            nowVelocity = agent.GetDirection();
        }
        return(RVOMath.normalize(nowVelocity));
    }
Ejemplo n.º 18
0
    // Use this for initialization
    void Awake()
    {
        RVO.Vector2 agentPos = new RVO.Vector2(transform.position.x, transform.position.z);
        m_agentHandle = RVO.Simulator.Instance.addAgent(agentPos);
        RVO.Simulator.Instance.setAgentRadius(m_agentHandle, 0.5f);
        gameObject.name = m_agentHandle.ToString();

        m_destPos           = transform.position;
        m_agentMaxNeighbors = RVO.Simulator.Instance.getAgentMaxNeighbors(m_agentHandle);
    }
Ejemplo n.º 19
0
    public void Start_Click()
    {
        if (numAgent.text.ToString() != "")
        {
            agents_number = Int32.Parse(numAgent.text.ToString());
            Debug.Log("Agents oui ");
        }
        else
        {
            agents_number = 40;
            Debug.Log("Agents non ");
        }
        if (width.text.ToString() != "")
        {
            corridor_width = Int32.Parse(width.text.ToString());
            Debug.Log("Width oui ");
        }
        else
        {
            corridor_width = 10;
            Debug.Log("Width non ");
        }
        if (length.text.ToString() != "")
        {
            corridor_lenght = Int32.Parse(length.text.ToString());
            Debug.Log("Length oui ");
        }
        else
        {
            corridor_lenght = 30;
            Debug.Log("Length non ");
        }

        panel.SetActive(false);
        Application.targetFrameRate = 60;
        agents = new List <Transform>();
        setupScenario();


        colors.Add(new Color(0.000f, 0.000f, 0.804f));
        colors.Add(new Color(0.118f, 0.565f, 1.000f));
        colors.Add(new Color(0.251f, 0.878f, 0.816f));
        colors.Add(new Color(0.400f, 0.804f, 0.667f));
        colors.Add(new Color(0.604f, 0.804f, 0.196f));


        for (int i = 0; i < getNumAgents(); i++)
        {
            RVO.Vector2 position = getPosition(i);
            agents.Add(Instantiate(agent, new Vector3(position.x(), 1.5f, position.y()), Quaternion.identity));
            agents[i].GetComponent <MeshRenderer>().material.color = colors[0];
            agents[i].localScale = new Vector3(sim_.getAgentRadius(i), sim_.getAgentRadius(i), sim_.getAgentRadius(i));
        }
    }
Ejemplo n.º 20
0
    public static Vector3 SafeNormalized(this RVO.Vector2 _this)
    {
        float magSqrd = _this.x() * _this.x() + _this.y() * _this.y();

        if (magSqrd > 0.0001f)
        {
            var mag = Mathf.Sqrt(magSqrd);
            return(new Vector3(_this.x() / mag, 0, _this.y() / mag));
        }
        return(Vector3.zero);
    }
Ejemplo n.º 21
0
    public static bool IsVectorNear(Vector2 a, Vector2 b, float range)
    {
        Vector2 aNormalize = RVOMath.normalize(a);
        Vector2 bNormalize = RVOMath.normalize(b);

        //Debug.Log(Mathf.Abs(RVOMath.det(aNormalize, bNormalize)));
        if (Mathf.Abs(RVOMath.det(aNormalize, bNormalize)) < range && RVOMath.abs(aNormalize - bNormalize) < 1.0f)
        {
            return(true);
        }
        return(false);
    }
Ejemplo n.º 22
0
    void Awake()
    {
        BoxCollider[]     boxColliders = GameObject.Find("Ground").GetComponentsInChildren <BoxCollider>();
        CapsuleCollider[] cylinders    = GameObject.Find("Ground").GetComponentsInChildren <CapsuleCollider>();
        for (int i = 0; i < boxColliders.Length; i++)
        {
            Vector2         position  = RVOWithUnity.Vec3ToVec2(boxColliders[i].transform.position);
            float           angle     = boxColliders[i].transform.rotation.eulerAngles.y * Mathf.Deg2Rad;
            float           sizeX     = boxColliders[i].transform.lossyScale.x * 0.5f;
            float           sizeZ     = boxColliders[i].transform.lossyScale.z * 0.5f;
            Vector2         right     = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
            Vector2         left      = new Vector2(-Mathf.Sin(angle), -Mathf.Cos(angle));
            Vector2         up        = new Vector2(-Mathf.Cos(angle), Mathf.Sin(angle));
            Vector2         down      = new Vector2(Mathf.Cos(angle), -Mathf.Sin(angle));
            Vector2         rightUp   = position + right * sizeZ + up * sizeX;
            Vector2         rightDown = position + right * sizeZ + down * sizeX;
            Vector2         leftUp    = position + left * sizeZ + up * sizeX;
            Vector2         leftDown  = position + left * sizeZ + down * sizeX;
            IList <Vector2> obstacle  = new List <Vector2>();
            obstacle.Add(rightUp);
            obstacle.Add(leftUp);
            obstacle.Add(leftDown);
            obstacle.Add(rightDown);

            /* Debug.Log(boxColliders[i].gameObject.name.ToString() + "::" +
             *  rightUp.ToString() + " " +
             *  leftUp.ToString() + " " +
             *  leftDown.ToString() + " " +
             *  rightDown.ToString()
             *   ); */
            Simulator.Instance.addObstacle(obstacle);
        }
        for (int i = 0; i < cylinders.Length; i++)
        {
            float           size     = cylinders[i].transform.lossyScale.x * 0.5413f;
            Vector2         position = RVOWithUnity.Vec3ToVec2(cylinders[i].transform.position);
            float           angle    = 360.0f / 8.0f;
            float           nowAngle = 360.0f - angle / 2.0f;
            IList <Vector2> obstacle = new List <Vector2>();
            //Debug.Log(cylinders[i].gameObject.name.ToString() + "::");
            for (int j = 0; j < 8; j++)
            {
                float   nowAngleRad = nowAngle * Mathf.Deg2Rad;
                Vector2 point       = new Vector2(Mathf.Sin(nowAngleRad), Mathf.Cos(nowAngleRad));
                point = position + point * size;
                obstacle.Add(point);
                nowAngle -= angle;
                //Debug.Log(point);
            }
            Simulator.Instance.addObstacle(obstacle);
        }
    }
Ejemplo n.º 23
0
    // Update is called once per frame
    void Update()
    {
        if (!reachedGoal())
        {
            setPreferredVelocities();
            doStep(false);

            /* Output the current global time. */
            //print(Simulator.Instance.getGlobalTime());
            for (int i = 0; i < getNumAgents(); ++i)
            {
                RVO.Vector2 position = getPosition(i);
                if (position.x() >= 80.0f)
                {
                    if (RVO.Vector2.abs(sim_.getAgentVelocity(i)) > sim_.getAgentMaxSpeed() * 3 / 5)
                    {
                        setVelocity(i, sim_.getAgentVelocity(i) / 2);
                    }
                    position           = getPosition(i);
                    agents[i].position = new Vector3(position.x(), 0f, position.y());

                    /* RVO.Vector2 vector2 = sim_.getAgentVelocity(i);
                     * agents[i].rotation = Quaternion.LookRotation(new Vector3(vector2.x_, 0, vector2.y_));*/
                }
                if (position.x() >= 90.0f)
                {
                    setPosition(i, new RVO.Vector2(-140, position.y()));
                    position           = getPosition(i);
                    agents[i].position = new Vector3(position.x(), 0f, position.y());

                    /*  RVO.Vector2 vector2 = sim_.getAgentVelocity(i);
                     * agents[i].rotation = Quaternion.LookRotation(new Vector3(vector2.x_, 0, vector2.y_));*/
                }
                else
                {
                    agents[i].position = new Vector3(position.x(), 0f, position.y());

                    /*  RVO.Vector2 vector2 = sim_.getAgentVelocity(i);
                     * agents[i].rotation = Quaternion.LookRotation(new Vector3(vector2.x_, 0, vector2.y_));*/
                }
                setColor(i);
            }
        }
        else
        {
            for (int i = 0; i < getNumAgents(); ++i)
            {
                agents[i].GetComponent <Rigidbody>().isKinematic = true;
            }
        }
    }
Ejemplo n.º 24
0
    void FixedUpdate()
    {
        int agentAmount = Simulator.Instance.getNumAgents();

        for (int i = 0; i < agentAmount; i++)
        {
            Vector2  agentVec2    = Simulator.Instance.getAgentPosition(i);
            Vector2  station      = rvoGameObject[i].GetComponent <RVOAgent>().GetStation();
            RVOAgent agent        = rvoGameObject[i].GetComponent <RVOAgent>();
            Vector2  prefVelocity = RVOWithUnity.CalcPrefVelocity(agentVec2, station, agent);
            Simulator.Instance.setAgentPrefVelocity(i, prefVelocity);
        }
        Simulator.Instance.doStep();
    }
Ejemplo n.º 25
0
    void OnAnimatorMove()
    {
        if (this.GetComponent <interact>().onConversation)
        {
            return;
        }
        // Update postion to agent position
        RVO.Vector2 agentPosRVO = Simulator.Instance.getAgentPosition(agentId);
        Vector3     agentPos    = toUnityVector(agentPosRVO);

        transform.position = agentPos;
        this.GetComponent <NavMeshAgent>().nextPosition = agentPos + transform.forward;
        //if(shouldMove) transform.position = agentPos;
    }
Ejemplo n.º 26
0
    public static bool isTwoSide(Vector2 line, Vector2 sideA, Vector2 sideB)
    {
        float detA = RVOMath.det(sideA, line);
        float detB = RVOMath.det(sideB, line);

        if ((detA > eps && detB > eps) || (detA < -eps && detB < -eps))
        {
            return(false);
        }
        else
        {
            return(true);
        }
    }
Ejemplo n.º 27
0
    // Start is called before the first frame update
    void Start()
    {
        RVO.Simulator.Instance.setTimeStep(0.03f);
        RVO.Simulator.Instance.setAgentDefaults(neighborDist, maxNeighbors, timeHorizon, timeHorizonObst,
                                                radius, maxSpeed, new RVO.Vector2(velocity.x, velocity.y));

        if (AddObstacleTest)
        {
            float ObstacleExtend             = ObstacleRadius + 0.5f;
            List <RVO.Vector2> obstacleVerts = new List <RVO.Vector2>()
            {
                new RVO.Vector2(ObstacleCenterPos.x - ObstacleExtend, ObstacleCenterPos.z - ObstacleExtend),
                new RVO.Vector2(ObstacleCenterPos.x - ObstacleExtend, ObstacleCenterPos.z + ObstacleExtend),
                new RVO.Vector2(ObstacleCenterPos.x + ObstacleExtend, ObstacleCenterPos.z + ObstacleExtend),
                new RVO.Vector2(ObstacleCenterPos.x + ObstacleExtend, ObstacleCenterPos.z - ObstacleExtend),
            };
            RVO.Simulator.Instance.addObstacle(obstacleVerts);
        }



        float angleStep = Mathf.PI * 2 / robotCount;

        robots        = new Transform[robotCount];
        robotAgentIds = new int[robotCount];
        goals         = new RVO.Vector2[robotCount];

        for (int i = 0; i < robotCount; ++i)
        {
            GameObject go  = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            Color      clr = new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f));
            go.GetComponent <MeshRenderer>().material.SetColor("_Color", clr);
            robots[i]               = go.transform;
            robots[i].localScale    = new Vector3(radius, radius, radius);
            robots[i].localPosition = Vector3.zero;

            Vector3 initPos = new Vector3(circleRadius * Mathf.Cos(i * angleStep), 0,
                                          circleRadius * Mathf.Sin(i * angleStep));
            robots[i].position = initPos;

            goals[i] = new RVO.Vector2(-initPos.x, -initPos.z);

            robotAgentIds[i] = RVO.Simulator.Instance.addAgent(new RVO.Vector2(initPos.x, initPos.z));
            //RVO.Simulator.Instance.setAgentMaxSpeed(robotAgentIds[i], UnityEngine.Random.Range(1f, 2f));
        }

        random = new System.Random();
        Invoke("Run", 2);
    }
Ejemplo n.º 28
0
// Use this for initialization
    void Start()
    {
        agents = new List <Transform>();                                                                                //Initializing the list of Unity Agents
        setupScenario();                                                                                                //Calling setupScenario
        this.gameObject.transform.localScale = new Vector3(7, 1, 7);                                                    // The script is attached to a plane, so this will change the size of the plane
        this.gameObject.transform.position   = new Vector3(0, 0, 0);                                                    //This will change the position of this plane
        Application.targetFrameRate          = 30;                                                                      // This will fix the maximum framerate of the scene

        for (int i = 0; i < getNumAgents(); i++)                                                                        // loop -> For each agent
        {
            RVO.Vector2 position = getPosition(i);                                                                      // Getting the position of the agent in the simulator
            addAgent(agent, new Vector3(position.x(), 1.5f, position.y()));                                             //Instantiating  the Unity Agent at the correct position
            agents[i].localScale = new Vector3(sim_.getAgentRadius(i), sim_.getAgentRadius(i), sim_.getAgentRadius(i)); //Adapting the size of the agent to the radius we set in the simulator
        }
    }
Ejemplo n.º 29
0
    void checkArrivedCorner()
    {
        if (corner >= agent.path.corners.Length)
        {
            return;
        }
        RVO.Vector2 agentPosRVO  = Simulator.Instance.getAgentPosition(agentId);
        Vector3     agentPos     = toUnityVector(agentPosRVO);
        float       distToCorner = Vector3.Distance(agentPos, agent.path.corners[corner]);

        // if arrived at next corner
        if (distToCorner <= minDistCorner)
        {
            corner++;
        }
    }
Ejemplo n.º 30
0
    void updateRVO()
    {
        if (corner >= agent.path.corners.Length)
        {
            return;
        }
        RVO.Vector2 agentLoc   = Simulator.Instance.getAgentPosition(agentId);
        RVO.Vector2 goalVector = toRVOVector(agent.path.corners[corner]) - agentLoc;

        if (RVOMath.absSq(goalVector) > 1.0f)
        {
            goalVector = RVOMath.normalize(goalVector);
        }

        Simulator.Instance.setAgentPrefVelocity(agentId, goalVector);
    }
Ejemplo n.º 31
0
    // 大球
    void CreateGameObject(Vector3 position, GameObject spherePrefab, float radius)
    {
        Simulator.Instance.setAgentDefaults(10.0f, 10, 1f, 1.0f, radius / 2f, speed, new RVO.Vector2(0.0f, 0.0f));
        // orca
        RVO.Vector2 p     = new RVO.Vector2(position.x, position.z);
        int         index = Simulator.Instance.addAgent(p);

        Simulator.Instance.setAgentMass(index, 1.1f);
        // 目标点
        goals.Add(p);
        // 物体
        GameObject g = GameObject.Instantiate(mSpherePrefab01);

        g.transform.localScale = new Vector3(radius, radius, radius);
        mSpheres.Add(g);
        mSphereScritps.Add(g.AddComponent <Sphere>());
    }