Exemplo n.º 1
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);
            }
        }
    }
Exemplo n.º 3
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);
        }
    }
Exemplo n.º 4
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)));
        }
    }
Exemplo n.º 5
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));
    }
Exemplo n.º 6
0
    // Update is called once per frame
    void Update()
    {
        if (sid >= 0)
        {
            //更改前进的正方向
            //position
            Vector2 pos = Simulator.Instance.getAgentPosition(sid);
            //direction
            Vector2 vel = Simulator.Instance.getAgentPrefVelocity(sid);
            //position
            transform.position = new Vector3(pos.x, transform.position.y, pos.y);
            //direction
            if (Math.Abs(vel.x) > 0.01f && Math.Abs(vel.y) > 0.01f)
            {
                transform.forward = new Vector3(vel.x, 0, vel.y).normalized;
            }
        }


        //if (Input.GetMouseButton(0))
        //{
        //    Debug.LogError("GetMouseButton(0)+左击");
        //}
        //else if (Input.GetMouseButton(1))
        //{
        //    Debug.LogError("GetMouseButton(1)+右击");
        //}



        if (!Input.GetMouseButton(1))
        {
            Simulator.Instance.setAgentPrefVelocity(sid, KInt2.zero);
            return;
        }
        //点击鼠标的右键进入到以下的逻辑
        //鼠标点位 和 角色的位置点
        // 改进的vector2
        KInt2 goalVector = GameMainManager.Instance.mousePosition - Simulator.Instance.getAgentPosition(sid);//GameMainManager.Instance.mousePosition

        //归一化
        if (RVOMath.absSq(goalVector) > 1)
        {
            goalVector = RVOMath.normalize(goalVector);
        }

        Simulator.Instance.setAgentPrefVelocity(sid, goalVector);

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

        Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) + dist * new KInt2((float)Math.Cos(angle), (float)Math.Sin(angle)));
    }
Exemplo n.º 7
0
        public Vector2 GetGoalVector(int agentId, Vector2 agentPos)
        {
            Vector2 goalVector = goals[agentId] - agentPos;

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

            return(goalVector);
        }
Exemplo n.º 8
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);
    }
Exemplo n.º 9
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);
    }
Exemplo n.º 10
0
        private Vector2 getPreferredVelocities(int id, Vector2 location)
        {
            Vector2 goalVector = goals[id] - location;

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

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

            /* Perturb a little to avoid deadlocks due to perfect symmetry. */
            goalVector = goalVector + dist * new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));

            return(goalVector);
        }
Exemplo n.º 11
0
    // Update is called once per frame
    void Update()
    {
        if (sid >= 0)
        {
            Vector2 pos = (Vector2)(VInt2)Simulator.Instance.getAgentPosition(sid);
            Vector2 vel = (Vector2)(VInt2)Simulator.Instance.getAgentPrefVelocity(sid);
            transform.position = new Vector3(pos.x, transform.position.y, pos.y);
            if (Math.Abs(vel.x) > 0.01f && Math.Abs(vel.y) > 0.01f)
            {
                transform.forward = new Vector3(vel.x, 0, vel.y).normalized;
            }
        }

        Simulator.Instance.setAgentPrefVelocity(sid, VInt2.zero);

        //KInt2 goalVector = GameMainManager.Instance.mousePosition - Simulator.Instance.getAgentPosition(sid);//GameMainManager.Instance.mousePosition
        VInt2 goalVector = (VInt2)targetpos - Simulator.Instance.getAgentPosition(sid);//GameMainManager.Instance.mousePosition

        /*if (((VInt2) goalVector).sqrMagnitudeLong < 1000)
         * {
         *  return;
         * }*/
        if (RVOMath.absSq((KInt2)goalVector) > 1)
        {
            goalVector = (VInt2)RVOMath.normalize((KInt2)goalVector);
        }
        else
        {
            return;
        }

        Simulator.Instance.setAgentPrefVelocity(sid, goalVector);

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

        /*float angle = (float) m_random.NextDouble()*2.0f*(float) Math.PI;
         * float dist = (float) m_random.NextDouble()*0.0001f;
         *
         * Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) +
         *                                           dist*
         *                                           new KInt2((float) Math.Cos(angle), (float) Math.Sin(angle)));*/
        /*Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) +
         *                                           (VInt2)(dist*
         *                                           new KInt2((float) Math.Cos(angle), (float) Math.Sin(angle))));*/
    }
    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < Simulator.Instance.getNumAgents(); i++)
        {
            RVO.Vector2 agentLoc   = Simulator.Instance.getAgentPosition(i);
            RVO.Vector2 goalVector = toRVOVector(goals[i]) - agentLoc;

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

            Simulator.Instance.setAgentPrefVelocity(i, goalVector);

            RVOAgents[i].transform.localPosition = toUnityVector(agentLoc);
        }
        Simulator.Instance.doStep();
    }
Exemplo n.º 13
0
    void SetPreferredVelocities()
    {
        /*
         * Set the preferred velocity to be a vector of unit magnitude
         * (speed) in the direction of the goal.
         */
        Vector3 g = Vector3.zero;

        for (int i = 0; i < Simulator.Instance.getNumAgents(); ++i)
        {
            RVO.Vector2 goalVector = goals[i] - Simulator.Instance.getAgentPosition(i);

            if (RVOMath.absSq(goalVector) > 1.0f)
            {
                goalVector = RVOMath.normalize(goalVector);
            }
            Simulator.Instance.setAgentPrefVelocity(i, goalVector);
        }
    }
Exemplo n.º 14
0
    void setPreferredVelocities()
    {
        for (int i = 0; i < Simulator.Instance.getNumAgents(); ++i)
        {
            RVO.Vector2 goalVector = goals[i] - Simulator.Instance.getAgentPosition(i);

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

            Simulator.Instance.setAgentPrefVelocity(i, goalVector);

            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 RVO.Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)));
        }
    }
Exemplo n.º 15
0
    public void Init()
    {
        Simulator.Instance.setTimeStep(0.15f);
        Simulator.Instance.setAgentDefaults(neibourDist, maxNeibours, timeHorizon, obstacleTimeHorizon, radius, maxSpeed, new RVO.Vector2(0.0f, 0.0f));
        for (int i = 0; i < agentCount; i++)
        {
            RVO.Vector2 pos = circleRadius *
                              new RVO.Vector2((float)Math.Cos(i * 2.0f * Math.PI / agentCount),
                                              (float)Math.Sin(i * 2.0f * Math.PI / agentCount));
            Simulator.Instance.addAgent(pos);
            dirs.Add(RVOMath.normalize(-Simulator.Instance.getAgentPosition(i) - Simulator.Instance.getAgentPosition(i)));
            goals.Add(-Simulator.Instance.getAgentPosition(i));

            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            go.transform.position = new Vector3(pos.x(), 120, pos.y());
            agents.Add(go);
        }
        ConvertToObstacle();
        inited = true;
    }
Exemplo n.º 16
0
    // Update is called once per frame
    void Update()
    {
        if (sid >= 0)
        {
            Vector3 pos = Simulator.Instance.getAgentPosition(sid);
            Vector3 vel = Simulator.Instance.getAgentPrefVelocity(sid);
            transform.position = new Vector3(pos.x, transform.position.y, pos.y);
            if (Math.Abs(vel.x) > 0.01f && Math.Abs(vel.y) > 0.01f)
            {
                transform.forward = new Vector3(vel.x, 0, vel.y).normalized;
            }
            Debug.Log("Priority " + Simulator.Instance.getAgentPriority(sid));
            if (Simulator.Instance.getAgentPriority(sid) > 1)
            {
                Debug.Log("Count " + Simulator.Instance.getAgentNumAgentNeighbors(sid));
            }
        }

        if (!Input.GetMouseButton(1))
        {
            Simulator.Instance.setAgentPrefVelocity(sid, new Vector3(0, 0));
            return;
        }

        Vector3 goalVector = GameMainManager.Instance.mousePosition - Simulator.Instance.getAgentPosition(sid);

        if (RVOMath.absSq(goalVector) > 1.0f)
        {
            goalVector = RVOMath.normalize(goalVector) * Simulator.Instance.getAgentMaxSpeed(sid);
        }

        Simulator.Instance.setAgentPrefVelocity(sid, goalVector);

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

        //Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) +
        //                                             dist*
        //                                             new Vector3((float) Math.Cos(angle), (float) Math.Sin(angle)));
    }
Exemplo n.º 17
0
        /// <summary>
        /// 根据agent当前位置与目的地计算设置最佳速度矢量,每次更新位置需要调用
        /// </summary>
        static 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)
            {
                RVO.Vector2 goalVector;

                //给agent的属性赋值,方便计算path
                RVO.Vector2 pos = Simulator.Instance.getAgentPosition(i);
                _agents[i].positionNow = new Simulate.Vector2(pos.x_, pos.y_);

                if (_agents[i].navPoints.Count > 0)
                {
                    goalVector = new RVO.Vector2(_agents[i].navPoints[0].x_, _agents[i].navPoints[0].y_) - Simulator.Instance.getAgentPosition(i);
                }
                else
                {
                    goalVector = new RVO.Vector2(0, 0);
                }
                if (RVOMath.absSq(goalVector) > 1.0f)
                {
                    goalVector = RVOMath.normalize(goalVector);
                }

                //乘以一个系数让其接近人行走速度
                goalVector *= 1.6f;//人快走的速度

                Simulator.Instance.setAgentPrefVelocity(i, goalVector);

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

                Simulator.Instance.setAgentPrefVelocity(i, Simulator.Instance.getAgentPrefVelocity(i) +
                                                        dist * new RVO.Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)));
            }
        }
Exemplo n.º 18
0
    // Update is called once per frame
    void Update()
    {
        if (sid >= 0)
        {
            Vector2 pos = Simulator.Instance.getAgentPosition(sid);
            Vector2 vel = Simulator.Instance.getAgentPrefVelocity(sid);
            transform.position = new Vector3(pos.x(), transform.position.y, pos.y());
            if (Math.Abs(vel.x()) > 0.01f && Math.Abs(vel.y()) > 0.01f)
            {
                transform.forward = new Vector3(vel.x(), 0, vel.y()).normalized;
            }
        }

        if (!Input.GetMouseButton(1))
        {
            Simulator.Instance.setAgentPrefVelocity(sid, new Vector2(0, 0));
            return;
        }

        Vector2 goalVector = GameMainManager.Instance.mousePosition - Simulator.Instance.getAgentPosition(sid);

        //Debug.Log($"--vec1--{RVOMath.absSq(goalVector)}");
        if (RVOMath.absSq(goalVector) > 1.0f)
        {
            goalVector = RVOMath.normalize(goalVector);
        }
        //Debug.Log($"--vec2--{RVOMath.absSq(goalVector)}");
        Simulator.Instance.setAgentPrefVelocity(sid, goalVector);

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

        Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) +
                                                dist *
                                                new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)));
    }
Exemplo n.º 19
0
    // Update is called once per frame
    void Update()
    {
        if (sid >= 0)
        {
            Vector2 pos = Simulator.Instance.getAgentPosition(sid);
            Vector2 vel = Simulator.Instance.getAgentPrefVelocity(sid);
            transform.position = new Vector3(pos.x, transform.position.y, pos.y);

            /*if (Math.Abs(vel.x) > 0.01f && Math.Abs(vel.y) > 0.01f)
             *  transform.forward = new Vector3(vel.x, 0, vel.y).normalized;*/
        }

        if (!Input.GetMouseButton(1))
        {
            Simulator.Instance.setAgentPrefVelocity(sid, VInt2.zero);
            return;
        }

        VInt2 goalVector = (VInt2)WWZTest.Instance.mousePosition - Simulator.Instance.getAgentPosition(sid);//GameMainManager.Instance.mousePosition

        if (RVOMath.absSq((KInt2)goalVector) > 1)
        {
            goalVector = (VInt2)RVOMath.normalize((KInt2)goalVector);
        }

        Simulator.Instance.setAgentPrefVelocity(sid, goalVector);

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

        /*float angle = (float) m_random.NextDouble()*2.0f*(float) Math.PI;
         * float dist = (float) m_random.NextDouble()*0.0001f;
         *
         * Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) +
         *                                           dist*
         *                                           new KInt2((float) Math.Cos(angle), (float) Math.Sin(angle)));*/
    }
Exemplo n.º 20
0
    // Update is called once per frame
    void Update()
    {
        if (sid >= 0)
        {
            Vector3 pos = Simulator.Instance.getAgentPosition(sid);
            Vector3 vel = Simulator.Instance.getAgentPrefVelocity(sid);
            transform.position = new Vector3(pos.x, transform.position.y, pos.y);
            if (Math.Abs(vel.x) > 0.01f && Math.Abs(vel.y) > 0.01f)
            {
                transform.forward = new Vector3(vel.x, 0, vel.y).normalized;
            }
        }

        GetComponentInChildren <TextMesh>().text = "n: " + Simulator.Instance.getAgentNumAgentNeighbors(sid);
        GetComponentInChildren <LineRenderer>().SetPositions(new Vector3[] { transform.position, goal });

        Vector3 goalTf  = new Vector3(goal.x, goal.z, goal.y);
        Vector3 goalVec = goalTf - Simulator.Instance.getAgentPosition(sid);

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

        Simulator.Instance.setAgentPrefVelocity(sid, goalVec * GameMainManager.Instance.AgentMaxSpeed);

        float angle_ = (float)mrandom.NextDouble() * 2.0f * (float)Math.PI;
        float dist_  = (float)mrandom.NextDouble() * 0.0001f;

        Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) +
                                                dist_ *
                                                new Vector3((float)Math.Cos(angle_), (float)Math.Sin(angle_)));



        //foreach (var c in orcaObj.GetComponentsInChildren<LineRenderer>())
        //{
        //    Destroy(c.gameObject);
        //}

        //var oLines = Simulator.Instance.getAgentOrcaLines(sid);

        //foreach (Line line in oLines)
        //{
        //    GameObject go = new GameObject();
        //    LineRenderer r = go.AddComponent<LineRenderer>();
        //    go.transform.parent = orcaObj.transform;
        //    var p1 = orcaObj.transform.position + line.point - line.direction * 20.0f;
        //    var p2 = orcaObj.transform.position + line.point + line.direction * 20.0f;
        //    p1.z = p1.y;
        //    p1.y = 0.0f;
        //    p2.z = p2.y;
        //    p2.y = 0.0f;
        //    r.SetPositions(new Vector3[] { p1, p2 });
        //}



        return;

        if (!Input.GetMouseButton(1))
        {
            Simulator.Instance.setAgentPrefVelocity(sid, new Vector3(0, 0));
            return;
        }

        Vector3 goalVector = GameMainManager.Instance.mousePosition - Simulator.Instance.getAgentPosition(sid);

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

        Simulator.Instance.setAgentPrefVelocity(sid, goalVector);

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

        Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) +
                                                dist *
                                                new Vector3((float)Math.Cos(angle), (float)Math.Sin(angle)));
    }
Exemplo n.º 21
0
    public static Vector2 CalcPrefVelocity(Vector2 agentPosition, Vector2 goalPosisiton, RVOAgent agent)
    {
        Vector2 prefDistance = goalPosisiton - agentPosition;

        if (agent.GetIsStop() == true)
        {
            return(new Vector2(0f, 0f));
        }
        if (agent.isPlayer == true)
        {
            /* Is inertial model used */
            Vector2 nowVelocity   = Simulator.Instance.getAgentVelocity(agent.GetIndex());
            float   nowSpeed      = RVOMath.abs(nowVelocity);
            Vector2 prefVelocity  = RVOMath.normalize(prefDistance) * agent.prefSpeed;//nowSpeed -> prefSpeed
            Vector2 finalVelocity = new Vector2(0, 0);
            if (nowSpeed < agent.minSpeedToTurn)
            {
                /* Not fast enough to turn */
                Vector2 advanceDirection = GetDirection(agent);

                if (agent.prefSpeed > nowSpeed) //Speed up fitting
                {
                    finalVelocity = nowVelocity + advanceDirection * agent.acceleration;
                }
                else //Speed down fitting
                {
                    if (nowSpeed - agent.prefSpeed < agent.acceleration)
                    {
                        finalVelocity = prefVelocity;
                    }
                    else
                    {
                        finalVelocity = nowVelocity - advanceDirection * agent.acceleration;
                    }
                }
            }
            else
            {
                /* turn */
                float direction = RVOMath.det(nowVelocity, prefVelocity);
                if (direction > eps) //left
                {
                    Vector2 rightVerticalVelocity = new Vector2(-nowVelocity.y_, nowVelocity.x_);
                    rightVerticalVelocity = RVOMath.normalize(rightVerticalVelocity) * agent.speed * agent.angularSpeed;
                    if (RVOMath.det(nowVelocity + rightVerticalVelocity, prefVelocity) < eps) //success
                    {
                        finalVelocity = RVOMath.normalize(prefVelocity) * nowSpeed;
                    }
                    else
                    {
                        finalVelocity = RVOMath.normalize(nowVelocity + rightVerticalVelocity) * nowSpeed;
                    }
                }
                if (direction >= -eps && direction <= eps) //same direction
                {
                    finalVelocity = RVOMath.normalize(prefVelocity) * nowSpeed;
                }
                if (direction < -eps) //right
                {
                    Vector2 leftVerticalVelocity = new Vector2(nowVelocity.y_, -nowVelocity.x_);
                    leftVerticalVelocity = RVOMath.normalize(leftVerticalVelocity) * agent.speed * agent.angularSpeed;
                    if (RVOMath.det(nowVelocity + leftVerticalVelocity, prefVelocity) > -eps) //success
                    {
                        finalVelocity = RVOMath.normalize(prefVelocity) * nowSpeed;
                    }
                    else
                    {
                        finalVelocity = RVOMath.normalize(nowVelocity + leftVerticalVelocity) * nowSpeed;
                    }
                }
                //Debug.Log("!!!" + RVOMath.abs(finalVelocity).ToString());
                /* acc and slow */
                if (agent.prefSpeed - nowSpeed > eps) //acc
                {
                    if (IsVectorNear(nowVelocity, prefVelocity, 0.05f))
                    {
                        finalVelocity = finalVelocity + RVOMath.normalize(finalVelocity) * Mathf.Min(agent.acceleration, agent.prefSpeed - nowSpeed);
                    }
                }
                else //slow
                {
                    finalVelocity = finalVelocity - RVOMath.normalize(finalVelocity) * Mathf.Min(agent.acceleration, nowSpeed - agent.prefSpeed);
                }
                // Debug.Log("!!!"  + RVOMath.abs(finalVelocity).ToString());
            }
            Vector2 ditherVelocity = GetDitherVelocity(ditherSize);
            if (RVOMath.abs(finalVelocity) < eps)
            {
                return(finalVelocity);
            }
            else
            {
                return(finalVelocity + ditherVelocity);
            }
        }
        else
        {
            Vector2 ditherVelocity = GetDitherVelocity(ditherSize);
            Vector2 prefVelocity   = RVOMath.normalize(prefDistance);
            return(prefVelocity * agent.prefSpeed + ditherVelocity);
        }
    }
Exemplo n.º 22
0
    void Update()
    {
        if (isOk && agentIndex != -1)
        {
            /* dist */
            transform.position = RVOWithUnity.Vec2ToVec3(Simulator.Instance.getAgentPosition(agentIndex));
            /* turn */
            if (RVOMath.abs(Simulator.Instance.getAgentVelocity(agentIndex)) > 0.0f)
            {
                Vector3 direction = RVOWithUnity.Vec2ToVec3(Simulator.Instance.getAgentVelocity(agentIndex));
                float   angle     = Vector3.Angle(Vector3.right, direction);
                if (Vector3.Cross(Vector3.right, direction).y < 0)
                {
                    angle = 360 - angle;
                }
                transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.x, angle, transform.rotation.z));
            }

            if (!isStop)
            {
                /* Velocity */
                velocity     = Simulator.Instance.getAgentVelocity(agentIndex);
                velocityVec3 = RVOWithUnity.Vec2ToVec3(velocity);
                speed        = RVOMath.abs(velocity);
                /* Update Station */
                station = pathNodes[nowNode];
                Vector2 stationVector2      = RVOWithUnity.Vec3ToVec2(station);
                Vector2 transformVector2    = RVOWithUnity.Vec3ToVec2(transform.position);
                Vector2 distVector2         = stationVector2 - transformVector2;
                Vector2 lastStationVector2  = RVOWithUnity.Vec3ToVec2(pathNodes[nowNode - 1]);
                Vector2 lastToNowNormalize  = RVOMath.normalize(stationVector2 - lastStationVector2);
                Vector2 verticalDistVector2 = new Vector2(-lastToNowNormalize.y_, lastToNowNormalize.x_);

                //turnDist = (speed / angularSpeed) / changeStation;
                if (RVOWithUnity.isTwoSide(verticalDistVector2, distVector2, lastToNowNormalize))
                {
                    nowNode++;
                    slowStationNum = Mathf.Max(0, slowStationNum - 1);
                    //Debug.Log(RVOWithUnity.Vec3ToVec2(pathNodes[nowNode]));
                    if (nowNode > pathNodes.Count - 1)
                    {
                        isStop    = true;
                        prefSpeed = 0.0f;
                    }
                    else
                    {
                        station = pathNodes[nowNode];
                    }
                    if (slowStationNum == 0)
                    {
                        prefSpeed = maxSpeed;
                    }
                }
                else
                {
                    float slowDist = Mathf.Abs(speed * speed - minSpeedToTurn * minSpeedToTurn) / (acceleration * slowStation * 2);
                    if (RVOMath.abs(distVector2) >= slowDist && slowStationNum == 0)
                    {
                        prefSpeed = maxSpeed;
                    }
                    if (RVOMath.abs(distVector2) < slowDist)
                    {
                        /* Calc the slowScale */
                        if (nowNode + 1 > pathNodes.Count - 1)
                        {
                            prefSpeed = minSpeedToTurn;
                            turnDist  = 0.1f;
                        }
                        else
                        {
                            int     nextTurnNode  = nowNode + 1;
                            Vector2 nextStation   = RVOWithUnity.Vec3ToVec2(pathNodes[nextTurnNode]);
                            Vector2 lastStation   = stationVector2;
                            Vector2 nextDirection = nextStation - lastStation;
                            float   turnAngleDist = Vector3.Angle(velocityVec3, RVOWithUnity.Vec2ToVec3(nextDirection));
                            if (nowNode + 1 <= pathNodes.Count - 1 && slowStationNum == 0)
                            {
                                slowStationNum++;
                                float turnAngle = turnAngleDist;
                                //if (gameObject.name == "boat")
                                //  Debug.Log(lastStation + "/|/" + nextStation  + "/|/" + speed * ignoreStepFactor);
                                while (RVOMath.abs(nextDirection) < speed * ignoreStepFactor && nextTurnNode + 1 <= pathNodes.Count - 1)
                                {
                                    nextTurnNode++;
                                    slowStationNum++;
                                    lastStation = nextStation;
                                    nextStation = RVOWithUnity.Vec3ToVec2(pathNodes[nextTurnNode]);
                                    Vector2 lastDirection = nextDirection;
                                    nextDirection = nextStation - lastStation;
                                    //Debug.Log(nextStation + " " + lastStation);
                                    turnAngle += Vector3.Angle(RVOWithUnity.Vec2ToVec3(lastDirection), RVOWithUnity.Vec2ToVec3(nextDirection));
                                }
                                prefSpeed = minSpeedToTurn + (maxSpeed - minSpeedToTurn) * Mathf.Max(turningFactor - turnAngle, turningFactor / 10.0f) / turningFactor;
                            }
                            turnDist = Mathf.Tan(turnAngleDist * Mathf.Deg2Rad / 2) * (prefSpeed * prefSpeed / angularSpeed) / 200.0f;
                        }
                    }
                    if (RVOMath.abs(distVector2) < turnDist)
                    {
                        nowNode++;
                        slowStationNum = Mathf.Max(0, slowStationNum - 1);
                        if (nowNode > pathNodes.Count - 1)
                        {
                            isStop    = true;
                            prefSpeed = 0.0f;
                        }
                        else
                        {
                            station = pathNodes[nowNode];
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 23
0
    // Update is called once per frame
    void Update()
    {
        //寻路未完成则不执行移动
        if (!isPathFound)
        {
            return;
        }
        //到最后一个寻路点之前,都会执行
        if (curPathId < paths.Count)
        {
            if (sid >= 0)
            {
                VInt3   pos = (VInt3)Simulator.Instance.getAgentPosition(sid);
                Vector2 vel = (Vector2)(VInt2)Simulator.Instance.getAgentPrefVelocity(sid);

                /*if (PathfindingUtility.isCollide(transform.position, pos, out hit))
                 * {
                 *  pos = hit.point;
                 * }*/
                transform.position = new Vector3(pos.x / 1000f, transform.position.y, pos.z / 1000f);
                if (Math.Abs(vel.x) > 0.01f && Math.Abs(vel.y) > 0.01f)
                {
                    transform.forward = new Vector3(vel.x, 0, vel.y).normalized;
                }
            }

            Simulator.Instance.setAgentPrefVelocity(sid, VInt2.zero);

            //KInt2 goalVector = GameMainManager.Instance.mousePosition - Simulator.Instance.getAgentPosition(sid);//GameMainManager.Instance.mousePosition
            VInt2 goalVector = (VInt2)targetpos - Simulator.Instance.getAgentPosition(sid);//GameMainManager.Instance.mousePosition

            /*if (((VInt2) goalVector).sqrMagnitudeLong < 1000)
             * {
             *  return;
             * }*/
            if (RVOMath.absSq((KInt2)goalVector) > 1)
            {
                goalVector = (VInt2)RVOMath.normalize((KInt2)goalVector);
            }
            else
            {
                //已经到达当前的寻路终点,将目标替换成下一个点,当前路径点id也+1
                if (curPathId < paths.Count - 1)
                {
                    curPathId++;
                    targetpos = paths[curPathId];
                }
                return;
            }

            Simulator.Instance.setAgentPrefVelocity(sid, goalVector);

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

            //Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) + new VInt2((int) (Math.Cos(angle) * 1000), (int) (Math.Sin(angle)) * 1000) * (int)(dist * 1000));

            /*Simulator.Instance.setAgentPrefVelocity(sid, Simulator.Instance.getAgentPrefVelocity(sid) +
             *                                           (VInt2)(dist*
             *                                           new KInt2((float) Math.Cos(angle), (float) Math.Sin(angle))));*/
        }
    }
Exemplo n.º 24
0
    void FixedUpdate()
    {
        if (stopSimulation)
        {
            foreach (GameObject agent in rvoGameObj)
            {
                Destroy(agent);
            }
            rvoGameObj.Clear();
            agentPositions.Clear();
            return;
        }

        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;

                Vector3 agent_lossyScale = rvoGameObj[i].transform.lossyScale;

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

                Simulator.Instance.setAgentPrefVelocity(i, station);
                agentPositions[i] = Simulator.Instance.getAgentPosition(i);


                if (isCovidMode)
                {
                    if (!rvoGameObj[i].GetComponent <RVOAgent>().destinazioneRaggiunta())
                    {
                        Simulator.Instance.setAgentRadius(i, 0f);
                        Simulator.Instance.setAgentMaxSpeed(i, 0f);
                        Simulator.Instance.setAgentVelocity(i, new RVO.Vector2(0, 0));
                        Simulator.Instance.setAgentPrefVelocity(i, new RVO.Vector2(0, 0));
                    }
                    else
                    {
                        //..
                        //Simulator.Instance.setAgentDefaults( 2f, 15, 1.0f, 10.0f, getRadiusByDistancing( 1f ), 2.3f, new RVO.Vector2( 0.0f, 0.0f ) );
                        Simulator.Instance.setAgentRadius(i, getRadiusByDistancing(0.5f));
                        Simulator.Instance.setAgentMaxSpeed(i, 2.3f);
                        rvoGameObj[i].GetComponent <CapsuleCollider>().radius = 0.1f;
                    }
                }
                else
                {
                    if (Simulator.Instance.getAgentNumAgentNeighbors(i) > 3)
                    {
                        Simulator.Instance.setAgentRadius(i, 0.3f);
                        rvoGameObj[i].GetComponent <CapsuleCollider>().radius = 0.1f;
                    }
                    else
                    {
                        Simulator.Instance.setAgentRadius(i, 0.56f);
                        rvoGameObj[i].GetComponent <CapsuleCollider>().radius = 0.3f;
                    }
                }
            }
            Simulator.Instance.doStep();
        }
        catch (System.Exception ex)
        {
            //Debug.Log( ex.StackTrace );
        }
    }