Exemplo n.º 1
0
        private void InitParams()
        {
            m_Params                     = new CrowdAgentParams();
            m_Params.maxSpeed            = m_MaxSpeed;
            m_Params.radius              = m_Radius;
            m_Params.height              = m_Height;
            m_Params.maxAcceleration     = m_MaxAcc;
            m_Params.separationWeight    = m_SeparationWeight;
            m_Params.avoidanceType       = m_AvoidanceType;
            m_Params.collisionQueryRange = m_CollisionQueryRange;

            int flags = 0;

            if (m_IsAnticipateTurns)
            {
                flags |= (int)CrowdUpdateFlags.AnticipateTurns;
            }
            if (m_IsObstacleAvoidance)
            {
                flags |= (int)CrowdUpdateFlags.CrowdSeparation;
            }
            if (m_IsOptimizeVis)
            {
                flags |= (int)CrowdUpdateFlags.ObstacleAvoidance;
            }
            if (m_IsOptimizeTopo)
            {
                flags |= (int)CrowdUpdateFlags.OptimizeTopo;
            }

            m_Params.updateFlags = (CrowdUpdateFlags)flags;
        }
Exemplo n.º 2
0
    // Use this for initialization
    void Start()
    {
        var navQuery = FindObjectOfType(typeof(RecastNavMeshQuery)) as RecastNavMeshQuery;

        if (navQuery != null)
        {
            _navMeshQuery = navQuery._navMeshQuery;
            filter        = navQuery.filter;
            crowd         = navQuery.Crowd;

            param = new CrowdAgentParams
            {
                Radius                = Radius,
                Height                = Height,
                MaxAcceleration       = MaxAcceleration,
                MaxSpeed              = MaxSpeed,
                CollisionQueryRange   = CollisionQueryRange,
                PathOptimizationRange = PathOptimizationRange,
                UpdateFlags           = UpdateFlags,
                ObstacleAvoidanceType = ObstacleAvoidanceType,
                SeparationWeight      = SeparationWeight
            };
            AgentId = navQuery.Crowd.AddAgent(new[] { transform.position.x, transform.position.y, transform.position.z }, param);
            ResetTarget();
        }
        else
        {
            Debug.LogError("Scene does not have a Nav Mesh Query, one must be added.");
        }
    }
    public NavAgentGroup CreateAgentGroup(int index, int maxPath, int maxStraightPath
                                          , out byte group)
    {
        group = mGroups[index * 2 + 0];

        CrowdAgentParams cap = new CrowdAgentParams();

        cap.avoidanceType       = mGroups[index * 2 + 1];
        cap.collisionQueryRange = mGroupData[index * GroupStide + QueryOffset];
        cap.height                = mGroupData[index * GroupStide + HeightOffset];
        cap.maxAcceleration       = mGroupData[index * GroupStide + AccelOffset];
        cap.maxSpeed              = mGroupData[index * GroupStide + JogOffset];
        cap.pathOptimizationRange = mGroupData[index * GroupStide + OptiOffset];
        cap.radius                = mGroupData[index * GroupStide + RadiusOffset];
        cap.separationWeight      = mGroupData[index * GroupStide + SepOffset];
        cap.updateFlags           = mUpdateFlags[index];

        NavAgentGroup result = new NavAgentGroup(
            cap, maxPath, maxStraightPath, mPoolData[index * 2 + 0], mPoolData[index * 2 + 1]);

        result.jogSpeed     = mGroupData[index * GroupStide + JogOffset];
        result.maxTurnSpeed = mGroupData[index * GroupStide + TurnOffset];
        result.runSpeed     = mGroupData[index * GroupStide + RunOffset];
        result.walkSpeed    = mGroupData[index * GroupStide + WalkOffset];

        return(result);
    }
Exemplo n.º 4
0
        /// <summary>
        /// Update the planner.
        /// </summary>
        /// <returns>True if successful.</returns>
        public bool Update()
        {
            if ((agent.data.flags & NavFlag.CrowdConfigUpdated) != 0)
            {
                CrowdAgentParams config = agent.crowdConfig;
                config.maxSpeed = 0;
                agent.crowdAgent.SetConfig(config);
                agent.data.flags &= ~NavFlag.CrowdConfigUpdated;
            }

            return(true);
        }
Exemplo n.º 5
0
        public void RevertToAgentGroup()
        {
            crowdConfig     = agentGroup.crowdAgentConfig;
            radiusAt        = agentGroup.radiusAt;
            radiusNear      = agentGroup.radiusNear;
            heightTolerence = agentGroup.heightTolerance;

            if (crowdAgent != null)
            {
                crowdAgent.SetConfig(crowdConfig);
            }
        }
Exemplo n.º 6
0
        public NavAgentGroups(CrowdAgentParams crowdAgentConfig, int maxPathSize, int maxStraightPathSize, int maxPoolCorridors, int maxPoolPaths)
        {
            mMaxStraightPathSize = Mathf.Max(1, maxStraightPathSize);
            mMaxPathSize         = Mathf.Max(1, maxPathSize);

            this.crowdAgentConfig = crowdAgentConfig;
            this.pathBuffer       = new uint[maxPathSize];

            mMaxCorridors = Mathf.Max(0, maxPoolCorridors);
            mCorridors    = new Stack <PathCorridor>(mMaxCorridors);

            mMaxPaths = Mathf.Max(0, maxPoolPaths);
            mPaths    = new Stack <NavPaths>(mMaxPaths);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Draws the basic agent debug information.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This does not include the duplicate agent information such as target and corner
        /// positions.
        /// </para>
        /// </remarks>
        /// <param name="agent">The agent to draw.</param>
        public void DrawBase(CrowdAgent agent)
        {
            Vector3          pos    = agent.Position;
            CrowdAgentParams config = agent.GetConfig();

            DebugDraw.Circle(pos, config.radius, neighborColor);

            DebugDraw.Arrow(pos + Vector3.up * config.height
                            , pos + agent.DesiredVelocity + Vector3.up * config.height
                            , 0, 0.05f, desiredVelocityColor);

            DebugDraw.Arrow(pos + Vector3.up * config.height
                            , pos + agent.Velocity + Vector3.up * config.height
                            , 0, 0.05f, velocityColor);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Starts up the planner.
        /// </summary>
        /// <returns>True if successful.</returns>
        public bool Enter()
        {
            if (agent == null || agent.navGroup.query == null || agent.navGroup.crowd == null)
            {
                return(false);
            }

            NavmeshPoint pos = agent.GetPointSearch(agent.data.position);

            if (pos.polyRef == 0)
            {
                Debug.LogError(string.Format(
                                   "{0}: Could not constrain position to navigation mesh. {1}"
                                   , agent.transform.name, pos.ToString()));

                return(false);
            }

            agent.RemoveFromCrowd();

            CrowdAgentParams config = agent.crowdConfig;

            config.maxSpeed = 0;

            agent.crowdAgent =
                agent.navGroup.crowd.AddAgent(agent.data.plannerGoal.point, config);

            if (agent.crowdAgent == null)
            {
                Debug.LogError(string.Format("{0}: Could not add agent to the crowd."
                                             , agent.transform.name));

                return(false);
            }

            agent.data.flags &= ~NavFlag.CrowdConfigUpdated;

            agent.data.desiredPosition = agent.data.position;
            agent.data.desiredSpeedSq  = 0;
            agent.data.desiredVelocity = Vector3.zero;

            agent.SetCorridorAssets(false);
            agent.SetPathAssets(false);

            return(true);
        }
Exemplo n.º 9
0
        public static NavAgentGroups CreateAgentGroup(int index, int maxPath, int maxStraightPath, out byte group)
        {
            GroupSettings    aSetting = new GroupSettings();
            CrowdAgentParams cap      = new CrowdAgentParams();

            cap.collisionQueryRange = aSetting.colisionQueryRange;
            cap.height                = aSetting.height;
            cap.maxAcceleration       = aSetting.maxAcceleration;
            cap.maxSpeed              = aSetting.maxSpeed;
            cap.pathOptimizationRange = aSetting.pathOptimizationRange;
            cap.radius                = aSetting.radius;
            cap.separationWeight      = aSetting.separationWeight;
            cap.updateFlags           = aSetting.updateFlags;
            NavAgentGroups aGroup = new NavAgentGroups(cap, maxPath, maxStraightPath,
                                                       aSetting.maxPoolCorridors, aSetting.maxPoolPaths);

            aGroup.maxTurnSpeed = aSetting.maxTurnSpeed;

            group = mGroup[index];
            return(aGroup);
        }
Exemplo n.º 10
0
    public void Init(LunaNavmeshQuery navmesh, Transform transform, System.Action idle, System.Action move)
    {
        _navQuery = navmesh;

        _transform = transform;
        _reached   = idle;
        _move      = move;

        if (_navQuery != null)
        {
            _navMeshQuery = _navQuery._navMeshQuery;
            filter        = _navQuery.filter;
            crowd         = _navQuery.Crowd;

            param = new CrowdAgentParams
            {
                Radius                = Radius,
                Height                = Height,
                MaxAcceleration       = MaxAcceleration,
                MaxSpeed              = MaxSpeed,
                CollisionQueryRange   = CollisionQueryRange,
                PathOptimizationRange = PathOptimizationRange,
                UpdateFlags           = UpdateFlags,
                ObstacleAvoidanceType = ObstacleAvoidanceType,
                SeparationWeight      = SeparationWeight
            };

            Vector3 pos = _transform.position;
            AgentId = _navQuery.Crowd.AddAgent(new[] { pos.x, pos.y, pos.z }, param);
            //ResetTarget();

            _crowdAgent = crowd.GetAgent(AgentId);
            float[] resetpos = _crowdAgent.npos;
            _transform.position = new Vector3(resetpos[0], resetpos[1], resetpos[2]);
        }
        else
        {
            UnityEngine.Debug.LogError("Scene does not have a Nav Mesh Query, one must be added.");
        }
    }
Exemplo n.º 11
0
        //管理目标位置,有比较的话需要对这个目标进行变更
        private bool HandleNormalPlanning(bool hasNewGoal)
        {
            if (hasNewGoal || !mGoalInRange && theAgent.IsNear(theAgent.desiredPosition.point, mTarget.point))
            {
                //在不变更移动计划的时候,看看能不能走
                if (!SetLocalTarget())
                {
                    if (theAgent.PlanPath(theAgent.desiredPosition, theAgent.plannerGoal) <= 0)
                    {
                        Debug.LogError(string.Format("{0}: Path replan failed. Position: {1}, Goal: {2}"
                                                     , theAgent.transform.name, theAgent.desiredPosition.ToString(), theAgent.plannerGoal.ToString()));
                        return(false);
                    }

                    return(SetLocalTarget());
                }
            }
            else if (mGoalInRange)
            {
                //这个方法根据CritterAI原有的架构来做的,由于这个部分已经被魔改,所以此处可能需要很慎重处理
                if (mAtGoalState == StateNormal)
                {
                    if (Vector3Util.IsInRange(theAgent.desiredPosition.point
                                              , theAgent.plannerGoal.point, 0.12f, theAgent.heightTolerence))
                    {
                        mAtGoalState = StateInRange;
                        theAgent.RemoveFromCrowd();

                        CrowdAgentParams config = theAgent.crowdConfig;
                        config.maxSpeed = 0;

                        theAgent.crowdAgent = theAgent.navGroup.crowd.AddAgent(theAgent.plannerGoal.point, config);

                        theAgent.flags &= ~NavFlag.CrowdConfigUpdated;
                    }
                }
            }
            return(true);
        }
Exemplo n.º 12
0
    public void Init(RoleTransform transform, LunaNavmeshQuery navmeshQuery)
    {
        _navQuery  = navmeshQuery;
        _transform = transform;


        if (_navQuery != null)
        {
            _navMeshQuery = _navQuery._navMeshQuery;
            filter        = _navQuery.filter;
            crowd         = _navQuery.Crowd;

            param = new CrowdAgentParams
            {
                Radius                = Radius,
                Height                = Height,
                MaxAcceleration       = MaxAcceleration,
                MaxSpeed              = MaxSpeed,
                CollisionQueryRange   = CollisionQueryRange,
                PathOptimizationRange = PathOptimizationRange,
                UpdateFlags           = UpdateFlags,
                ObstacleAvoidanceType = ObstacleAvoidanceType,
                SeparationWeight      = SeparationWeight
            };

            Luna3D.Vector3 pos = _transform.GetPosition();
            AgentId = _navQuery.Crowd.AddAgent(new[] { pos.x, pos.y, pos.z }, param);
            //ResetTarget();

            _crowdAgent = crowd.GetAgent(AgentId);
            float[] resetpos = _crowdAgent.npos;
            _transform.SetPosition(new Vector3(resetpos[0], resetpos[1], resetpos[2]));
        }
        else
        {
            System.Console.WriteLine("Scene does not have a Nav Mesh Query, one must be added.");
        }
    }
Exemplo n.º 13
0
    public void CreateQuery(float fMoveSpeed, float fTurnSpeed)
    {
        this.mTurnSpeed = fTurnSpeed;
        this.mMoveSpeed = fMoveSpeed;
        NavStatus status = NavmeshQuery.Create(navmesh, mMaxQueryNodes, out query);

        if ((status & NavStatus.Sucess) == 0)
        {
            Debug.LogError(
                fileName + ": Aborted initialization. Failed query creation: " + status.ToString());
            mCrowdManager = null;
            return;
        }

        mCrowdManager = CrowdManager.Create(mMaxCrowdAgents, mMaxAgentRadius, navmesh);
        if (mCrowdManager == null)
        {
            Debug.LogError(fileName + ": Aborted initialization. Failed crowd creation.");
        }

        CrowdAvoidanceParams mCrowdParam = CrowdAvoidanceParams.CreateStandardMedium();

        mCrowdManager.SetAvoidanceConfig(0, mCrowdParam);
        mCrowdAgentParams = new CrowdAgentParams();
        mCrowdAgentParams.avoidanceType       = 0;
        mCrowdAgentParams.collisionQueryRange = 3.2f;
        mCrowdAgentParams.height                = 1.8f;
        mCrowdAgentParams.maxAcceleration       = 8.0f;
        mCrowdAgentParams.maxSpeed              = this.mMoveSpeed;
        mCrowdAgentParams.pathOptimizationRange = 12.0f;
        mCrowdAgentParams.radius                = 1.0f;
        mCrowdAgentParams.separationWeight      = 2.0f;
        mCrowdAgentParams.updateFlags           = CrowdUpdateFlags.AnticipateTurns | CrowdUpdateFlags.ObstacleAvoidance | CrowdUpdateFlags.CrowdSeparation | CrowdUpdateFlags.OptimizeVis | CrowdUpdateFlags.OptimizeTopo;

        polyResult        = new uint[300];
        pointResult       = new Vector3[300];
        tileBufferPoints  = new Vector3[3000];
        pointResultBuffer = new Vector3[300];
        polyResultBuffer  = new uint[300];
        NavmeshTile tile  = navmesh.GetTile(0);
        int         count = tile.GetVerts(tileBufferPoints);

        Debug.Log("Tile " + tile.GetTileRef() + " count:" + count);
        if (count > 3000)
        {
            tileBufferPoints = new Vector3[count];
        }
        tileBufferRef = -1;

        //NavmeshPoly[] polys=new NavmeshPoly[3000];
        //int polyCount;
        //polyCount=tile.GetPolys(polys);
        //for (int i = 0; i < polyCount; i++)
        //{
        //    NavmeshPoly poly = polys[i];
        //    //if (poly.Type == NavmeshPolyType.OffMeshConnection)
        //    {
        //        Debug.Log("Poly" + i+"type"+poly.Type.ToString());
        //    }
        //}
    }
Exemplo n.º 14
0
 public static extern int dtcAddAgent(IntPtr crowd
     , [In] ref Vector3 pos
     , ref CrowdAgentParams agentParams
     , ref IntPtr agent
     , ref CrowdAgentCoreState initialState);
Exemplo n.º 15
0
 public static extern void dtcaGetAgentParams(IntPtr agent
                                              , ref CrowdAgentParams config);
Exemplo n.º 16
0
        public override bool Update()
        {
            if (mAtGoalState == StateNormal)
            {
                theAgent.SyncCrowdToDesired();
            }

            //这一块的套路和Simple的套路是差不多的
            bool newPos  = (theAgent.flags & NavFlag.HasNewPosition) != 0;
            bool newGoal = (theAgent.flags & NavFlag.HasNewGoal) != 0;

            NavmeshPoint pt;

            if (newPos)
            {
                //和critterai的dll交互,然后调到recast的dll
                //获得当前的navmesh上面的点位置
                pt = theAgent.GetPointSearch(theAgent.position);
                if (pt.polyRef == 0)
                {
                    Debug.LogWarning(string.Format(
                                         "{0}: Could not constrain new position to the navigation mesh. Ignoring: {1}"
                                         , theAgent.transform.name, pt.ToString()));
                    newPos = false;
                }
                else
                {
                    theAgent.desiredPosition = pt;
                }
            }

            if (newGoal)
            {
                pt = theAgent.GetPointSearch(theAgent.goal);
                if (pt.polyRef == 0)
                {
                    Debug.LogWarning(string.Format(
                                         "{0}: Could not constrain new goal to the navigation mesh. Ignoring: {1}"
                                         , theAgent.transform.name, pt.ToString()));

                    newGoal = false;
                }
                else
                {
                    theAgent.plannerGoal = pt;
                }
            }

            if (mAtGoalState != StateNormal && newGoal || newPos)
            {
                TransitionToNormalGoalState();
            }

            theAgent.flags &= ~(NavFlag.HasNewPosition | NavFlag.HasNewGoal);

            //如果有新坐标,就走这个feedback,这里与SimpleMove不同
            if (newPos)
            {
                return(HandlePositionFeedback());
            }

            if (!HandleNormalPlanning(newGoal))
            {
                return(false);
            }

            if ((theAgent.flags & NavFlag.CrowdConfigUpdated) != 0)
            {
                CrowdAgentParams config = theAgent.crowdConfig;

                if (mAtGoalState != StateNormal)
                {
                    config.maxSpeed = 0;
                }

                theAgent.crowdAgent.SetConfig(config);
                theAgent.flags &= ~NavFlag.CrowdConfigUpdated;
            }

            if (mAtGoalState == StateInRange)
            {
                //手工移动agent到目标
                if (theAgent.IsAtDestination())
                {
                    //到达目标之后,将自身转化为idle
                    mAtGoalState             = StateIdle;
                    theAgent.desiredPosition = theAgent.plannerGoal;
                    theAgent.desiredSpeedSq  = 0;
                    theAgent.desiredVelocity = Vector3.zero;
                }
                else
                {
                    Vector3 pos  = theAgent.desiredPosition.point;
                    Vector3 goal = theAgent.plannerGoal.point;

                    //手动移动agent到目标
                    float desiredSpeed = Mathf.Sqrt(theAgent.desiredSpeedSq);

                    theAgent.desiredPosition.point   = Vector3.MoveTowards(pos, goal, desiredSpeed * NavManager.threadUpdateTimer);
                    theAgent.desiredPosition.polyRef = 0;

                    theAgent.desiredVelocity = (theAgent.desiredPosition.point - pos).normalized * desiredSpeed;
                }
            }

            return(true);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="crowdAgentConfig">
        /// The default agent configuration for  all agents in the group.
        /// </param>
        /// <param name="maxPathSize">The default maximum path size. [Limit: >= 1]</param>
        /// <param name="maxStraightPathSize">
        /// The default maximum straight path size. [Limit: >= 1]
        /// </param>
        /// <param name="maxPoolCorridors">
        /// The maximum number of corridors to allowed in the corridor pool. [Limit: >= 0]
        /// </param>
        /// <param name="maxPoolPaths">
        /// The maximum number of paths to allow in the path pool. [Limit: >= 0]
        /// </param>
        public NavAgentGroup(CrowdAgentParams crowdAgentConfig
            , int maxPathSize, int maxStraightPathSize, int maxPoolCorridors, int maxPoolPaths)
        {
            mMaxStraightPathSize = Mathf.Max(1, maxStraightPathSize);
            mMaxPathSize = Mathf.Max(1, maxPathSize);

            this.crowdAgentConfig = crowdAgentConfig;
            this.pathBuffer = new uint[maxPathSize];

            mMaxCorridors = Mathf.Max(0, maxPoolCorridors);
            mCorridors = new Stack<PathCorridor>(mMaxCorridors);

            mMaxPaths = Mathf.Max(0, maxPoolPaths);
            mPaths = new Stack<NavPath>(mMaxPaths);
        }
Exemplo n.º 18
0
    // Use this for initialization
    void Start()
    {
        var navQuery = FindObjectOfType(typeof(RecastNavMeshQuery)) as RecastNavMeshQuery;
        if (navQuery != null)
        {
            _navMeshQuery = navQuery._navMeshQuery;
            filter = navQuery.filter;
            crowd = navQuery.Crowd;

            param = new CrowdAgentParams
            {
                Radius = Radius,
                Height = Height,
                MaxAcceleration = MaxAcceleration,
                MaxSpeed = MaxSpeed,
                CollisionQueryRange = CollisionQueryRange,
                PathOptimizationRange = PathOptimizationRange,
                UpdateFlags = UpdateFlags,
                ObstacleAvoidanceType = ObstacleAvoidanceType,
                SeparationWeight = SeparationWeight
            };
            AgentId = navQuery.Crowd.AddAgent(new[] { transform.position.x, transform.position.y, transform.position.z }, param);
            ResetTarget();
        }
        else
        {
            Debug.LogError("Scene does not have a Nav Mesh Query, one must be added.");
        }
    }
Exemplo n.º 19
0
        /// <summary>
        /// Update the planner.
        /// </summary>
        /// <returns>True if successful.</returns>
        public bool Update()
        {
            if (mAtGoalState == StateNormal)
            {
                agent.SyncCrowdToDesired();
            }

            bool newPos  = (agent.data.flags & NavFlag.HasNewPosition) != 0;
            bool newGoal = (agent.data.flags & NavFlag.HasNewGoal) != 0;

            NavmeshPoint pt;

            if (newPos)
            {
                pt = agent.GetPointSearch(agent.data.position);
                if (pt.polyRef == 0)
                {
                    // Ignore new position.
                    Debug.LogWarning(string.Format(
                                         "{0}: Could not constrain new position to the navigation mesh. Ignoring: {1}"
                                         , agent.transform.name, pt.ToString()));

                    newPos = false;
                }
                else
                {
                    agent.data.desiredPosition = pt;
                }
            }

            if (newGoal)
            {
                pt = agent.GetPointSearch(agent.data.goal);
                if (pt.polyRef == 0)
                {
                    // Ignore new goal.
                    Debug.LogWarning(string.Format(
                                         "{0}: Could not constrain new goal to the navigation mesh. Ignoring: {1}"
                                         , agent.transform.name, pt.ToString()));

                    newGoal = false;
                }
                else
                {
                    agent.data.plannerGoal = pt;
                }
            }

            if (mAtGoalState != StateNormal && newGoal || newPos)
            {
                TransitionToNormalGoalState();
            }

            agent.data.flags &= ~(NavFlag.HasNewPosition | NavFlag.HasNewGoal);

            if (newPos)
            {
                // Note: This call inherently handles everything, not just the
                // position change.
                return(HandlePositionFeedback());
            }

            if (!HandleNormalPlanning(newGoal))
            {
                // A critical failure has occurred.
                return(false);
            }

            if ((agent.data.flags & NavFlag.CrowdConfigUpdated) != 0)
            {
                CrowdAgentParams config = agent.crowdConfig;

                if (mAtGoalState != StateNormal)
                {
                    config.maxSpeed = 0;
                }

                agent.crowdAgent.SetConfig(config);
                agent.data.flags &= ~NavFlag.CrowdConfigUpdated;
            }

            if (mAtGoalState == StateInRange)
            {
                // Need to manually move the agent to the goal.
                Vector3 pos  = agent.data.desiredPosition.point;
                Vector3 goal = agent.data.plannerGoal.point;

                if (Vector2Util.SloppyEquals(
                        new Vector2(pos.x, pos.z), new Vector2(goal.x, goal.z), 0.005f))
                {
                    // Snap to the goal and, essentially, go into an idle
                    // state.
                    mAtGoalState = StateIdle;
                    agent.data.desiredPosition = agent.data.plannerGoal;
                    agent.data.desiredSpeedSq  = 0;
                    agent.data.desiredVelocity = Vector3.zero;

                    // Debug.Log("Reached goal.  Going idle.");
                }
                else
                {
                    // Need to manually move the agent to the goal.
                    // Assumptions: The distance is short, and the crowd has
                    // already slowed the agent's speed to a minimal value.

                    float desiredSpeed = Mathf.Sqrt(agent.data.desiredSpeedSq);

                    agent.data.desiredPosition.point =
                        Vector3.MoveTowards(pos, goal, desiredSpeed * Time.deltaTime);

                    // While the point will be on the mesh surface, we
                    // can't assume we are already in the goal's polygon.
                    agent.data.desiredPosition.polyRef = 0;

                    agent.data.desiredVelocity =
                        (agent.data.desiredPosition.point - pos).normalized * desiredSpeed;
                }
            }

            return(true);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Manages the crowd agent target, replanning the long distance path if needed.
        /// </summary>
        /// <returns>False on critical failure.</returns>
        private bool HandleNormalPlanning(bool hasNewGoal)
        {
            // Re-targetting will only occur if there is a new goal or getting close to an
            // intermediate target.

            if (hasNewGoal ||
                !mGoalInRange && agent.IsNear(agent.data.desiredPosition.point, mTarget.point))
            {
                // First try to set the target without replanning.
                if (!SetLocalTarget())
                {
                    // This can happen when approaching the target from
                    // outside the planned path, which can happen when
                    // the crowd manager takes a different path.
                    // (Or a new goal was set.)

                    if (agent.PlanPath(agent.data.desiredPosition, agent.data.plannerGoal) <= 0)
                    {
                        // Critical failure.
                        Debug.LogError(string.Format(
                                           "{0}: Path replan failed. Position: {1}, Goal: {2}"
                                           , agent.transform.name
                                           , agent.data.desiredPosition.ToString()
                                           , agent.data.plannerGoal.ToString()));
                        return(false);
                    }

                    // Next call should not not fail since the path is known to be good.
                    return(SetLocalTarget());
                }
            }
            else if (mGoalInRange)
            {
                /*
                 * Need to monitor to see if the goal has been reached.  This can be tricky.
                 * The optimal 'at' and 'near' range ranges are usually too restrictive and too
                 * large respectively. So a fixed value, based on experience, is used instead.
                 * (OK to make this value configurable.)
                 *
                 * Assumption: Other parts of the code detect if a re-evaluation of position
                 * is needed.
                 */
                if (mAtGoalState == StateNormal)
                {
                    if (Vector3Util.IsInRange(agent.data.desiredPosition.point
                                              , agent.data.plannerGoal.point
                                              , 0.12f, agent.data.heightTolerence))
                    {
                        // Remove from the crowd and add back, exactly at the goal with a zero max
                        // speed.  Basically, puts a static obstacle into the crowd.

                        mAtGoalState = StateInRange;
                        agent.RemoveFromCrowd();

                        CrowdAgentParams config = agent.crowdConfig;
                        config.maxSpeed = 0;

                        agent.crowdAgent =
                            agent.navGroup.crowd.AddAgent(agent.data.plannerGoal.point, config);

                        agent.data.flags &= ~NavFlag.CrowdConfigUpdated;

                        // Debug.Log("Transition to 'at goal' task.");
                    }
                }
            }
            return(true);
        }
Exemplo n.º 21
0
 public static extern int dtcAddAgent(IntPtr crowd
                                      , [In] ref Vector3 pos
                                      , ref CrowdAgentParams agentParams
                                      , ref IntPtr agent
                                      , ref CrowdAgentCoreState initialState);
Exemplo n.º 22
0
        /// <summary>
        /// Reverts the agent's group related configuration settings to the values in 
        /// <see cref="agentGroup"/>.
        /// </summary>
        /// <remarks>Handles updates to the crowd agent. So the 
        /// <see cref="NavFlags.CrowdConfigUpdated"/> flag will not be set.
        /// </remarks>
        public void RevertToAgentGroup()
        {
            crowdConfig = agentGroup.crowdAgentConfig;

            data.radiusAt = agentGroup.radiusAt;
            data.radiusNear = agentGroup.radiusNear;
            data.heightTolerence = agentGroup.heightTolerance;

            if (crowdAgent != null)
                crowdAgent.SetConfig(crowdConfig);
        }
Exemplo n.º 23
0
 public static extern void dtcUpdateAgentParameters(IntPtr crowd
                                                    , int index
                                                    , ref CrowdAgentParams agentParams);
Exemplo n.º 24
0
 public static extern void dtcUpdateAgentParameters(IntPtr crowd
     , int index
     , ref CrowdAgentParams agentParams);
 public static extern void dtcaGetAgentParams(IntPtr agent
     , ref CrowdAgentParams config);