/// <summary> /// Initializes a new instance of the <see cref="Crowd" /> class. /// </summary> /// <param name="maxAgents">The maximum agents allowed</param> /// <param name="maxAgentRadius">The maximum radius for an agent</param> /// <param name="navMesh">The navigation mesh</param> public Crowd(int maxAgents, float maxAgentRadius, ref TiledNavMesh navMesh) { this.maxAgents = maxAgents; this.maxAgentRadius = maxAgentRadius; this.ext = new Vector3(maxAgentRadius * 2.0f, maxAgentRadius * 1.5f, maxAgentRadius * 2.0f); //initialize proximity grid this.grid = new ProximityGrid<Agent>(maxAgents * 4, maxAgentRadius * 3); //allocate obstacle avoidance query this.obstacleQuery = new ObstacleAvoidanceQuery(6, 8); //initialize obstancle query params this.obstacleQueryParams = new ObstacleAvoidanceQuery.ObstacleAvoidanceParams[AgentMaxObstacleAvoidanceParams]; for (int i = 0; i < this.obstacleQueryParams.Length; i++) { this.obstacleQueryParams[i].VelBias = 0.4f; this.obstacleQueryParams[i].WeightDesVel = 2.0f; this.obstacleQueryParams[i].WeightCurVel = 0.75f; this.obstacleQueryParams[i].WeightSide = 0.75f; this.obstacleQueryParams[i].WeightToi = 2.5f; this.obstacleQueryParams[i].HorizTime = 2.5f; this.obstacleQueryParams[i].GridSize = 33; this.obstacleQueryParams[i].AdaptiveDivs = 7; this.obstacleQueryParams[i].AdaptiveRings = 2; this.obstacleQueryParams[i].AdaptiveDepth = 5; } //allocate temp buffer for merging paths this.maxPathResult = 256; this.pathResult = new PolyId[this.maxPathResult]; this.pathq = new PathQueue(maxPathResult, 4096, ref navMesh); this.agents = new Agent[maxAgents]; this.activeAgents = new Agent[maxAgents]; this.agentAnims = new AgentAnimation[maxAgents]; for (int i = 0; i < maxAgents; i++) { this.agents[i] = new Agent(maxPathResult); } for (int i = 0; i < maxAgents; i++) { this.agentAnims[i].Active = false; } //allocate nav mesh query this.navQuery = new NavMeshQuery(navMesh, 512); }
/// <summary> /// Get the crowd agent's neighbors. /// </summary> /// <param name="pos">Current position</param> /// <param name="height">The height</param> /// <param name="range">The range to search within</param> /// <param name="skip">The current crowd agent</param> /// <param name="result">The neihbors array</param> /// <param name="maxResult">The maximum number of neighbors that can be stored</param> /// <param name="agents">Array of all crowd agents</param> /// <param name="grid">The ProximityGrid</param> /// <returns>The number of neighbors</returns> public int GetNeighbors(Vector3 pos, float height, float range, Agent skip, CrowdNeighbor[] result, int maxResult, Agent[] agents, ProximityGrid<Agent> grid) { int n = 0; const int MAX_NEIS = 32; Agent[] ids = new Agent[MAX_NEIS]; int nids = grid.QueryItems(pos.X - range, pos.Z - range, pos.X + range, pos.Z + range, ids, MAX_NEIS); for (int i = 0; i < nids; i++) { Agent ag = ids[i]; if (ag == skip) continue; //check for overlap Vector3 diff = pos - ag.Position; if (Math.Abs(diff.Y) >= (height + ag.Parameters.Height) / 2.0f) continue; diff.Y = 0; float distSqr = diff.LengthSquared(); if (distSqr > range * range) continue; n = AddNeighbor(ids[i], distSqr, result, n, maxResult); } return n; }