private void CrowdContact(Collider contact) { //print(_crowdCollider != _rayhit.collider); //print("contact"); if (contact == null) { return; } //New Head if (_currentCrowdCollider != contact) { //print("new colldier"); OnTouchHeadEvent.Invoke(); //If going forward, and was not previously on a head if (_currentCrowdCollider == null) { //print("new colldier, reset timer"); _slowDownTimer = 0; } // Might be performance intensive _inContactComponent = contact.GetComponent <CrowdAgent>(); if (_inContactComponent == null) { return; } _inContactComponent.GetFriends(); _currentCrowdCollider = contact; } _slowDownTimer += Time.deltaTime; //print(_slowDownTimer); }
/// <summary> /// Draws agent local boundary information. /// </summary> /// <param name="agent">The agent to draw.</param> public void DrawLocalBoundary(CrowdAgent agent) { agent.GetBoundary(boundary); if (boundary.segmentCount == 0) { return; } DebugDraw.XMarker(boundary.center , 0.1f, boundaryColor); DebugDraw.SimpleMaterial.SetPass(0); GL.Begin(GL.LINES); GL.Color(boundaryColor); for (int i = 0; i < boundary.segmentCount; i++) { int p = i * 2; GL.Vertex(boundary.segments[p]); GL.Vertex(boundary.segments[p + 1]); } GL.End(); }
internal bool AddAgent(CrowdMoveAgent agent) { if (m_IsAppQuit || agent == null || m_Mgr == null) { return(false); } var param = agent.GetParams(); var pt = agent.transform.position; NavmeshPoint pp; Vector3 extends = agent.Extends; if (!NavMeshMap.GetNavmeshPoint(pt, extends, out pp, agent.GetFilter())) { return(false); } agent.transform.position = pp.point; CrowdAgent a = m_Mgr.AddAgent(pp.point, param); agent._SetAgent(a); agent._SetId(NewGlobalId()); var node = agent.ListNode; var list = node.List; if (list != m_AgentList) { if (list != null) { list.Remove(node); } m_AgentList.AddLast(node); } return(true); }
/// <summary> /// Adds the agent to the crowd if it is not already in the crowd. [Owner: Planner] /// </summary> /// <remarks> /// <para> /// Notes: /// </para> /// <ul> /// <li>Does not submit a move request to the crowd.</li> /// <li>Handles the <see cref="NavFlags.CrowdConfigUpdated"/> flag.</li> /// <li>Checks <paramref name="position"/> against the active agent. If it is different, /// the agent will be repositioned. (Removed/re-added.) /// </li> /// </ul> /// </remarks> /// <param name="position">The position to use when adding the agent. (Usually a known, /// good navigation mesh position.) /// </param> /// <returns> /// The result of the add. (Or the current agent reference if no add was needed.) /// </returns> public CrowdAgent AddToCrowd(Vector3 position) { if (navGroup.crowd == null) { return(null); } if (crowdAgent == null) { crowdAgent = navGroup.crowd.AddAgent(position, crowdConfig); } else { if (crowdAgent.Position != position) { RemoveFromCrowd(); crowdAgent = navGroup.crowd.AddAgent(position, crowdConfig); } else if ((data.flags | NavFlag.CrowdConfigUpdated) != 0) { crowdAgent.SetConfig(crowdConfig); } } data.flags &= ~NavFlag.CrowdConfigUpdated; return(crowdAgent); }
void CreateMovingBarrels(DynamicNavigationMesh navMesh) { var cache = GetSubsystem <ResourceCache>(); Node barrel = scene.CreateChild("Barrel"); StaticModel model = barrel.CreateComponent <StaticModel>(); model.Model = cache.Get <Model>("Models/Cylinder.mdl"); Material material = cache.Get <Material>("Materials/StoneTiled.xml"); model.SetMaterial(material); material.SetTexture(TextureUnit.TU_DIFFUSE, cache.Get <Texture2D>("Textures/TerrainDetail2.dds")); model.CastShadows = true; for (int i = 0; i < 20; ++i) { Node clone = barrel.Clone(CreateMode.REPLICATED); float size = 0.5f + NextRandom(1.0f); clone.Scale = new Vector3(size / 1.5f, size * 2.0f, size / 1.5f); clone.Position = navMesh.FindNearestPoint(new Vector3(NextRandom(80.0f) - 40.0f, size * 0.5f, NextRandom(80.0f) - 40.0f), Vector3.One); CrowdAgent agent = clone.CreateComponent <CrowdAgent>(); agent.Radius = clone.Scale.X * 0.5f; agent.Height = size; agent.NavigationQuality = NavigationQuality.NAVIGATIONQUALITY_LOW; } barrel.Remove(); }
/// <summary> /// Removes the agent from the crowd. (Safe to call if the agent is not in the crowd.) /// </summary> public void RemoveFromCrowd() { if (crowdAgent != null) { navGroup.crowd.RemoveAgent(crowdAgent); crowdAgent = null; } }
public static void DrawNavigation(CrowdAgentNavigator navigator, CrowdAgent agent) { if (EditorApplication.isPaused) { return; } Debug.DrawLine(agent.worldPosition, navigator.requestedDestination, Color.green, Time.deltaTime * 2); }
private void ClearAgent() { if (m_Agent != null) { if (m_Mgr != null) { m_Mgr.RemoveAgent(this); } m_Agent = null; } }
/// <summary> /// Draws all agent debug information. /// </summary> /// <param name="agent">The agent to draw.</param> public void DrawAll(CrowdAgent agent) { agent.GetCornerData(corners); agent.GetCorridor(corridor); // Order matters. NavDebug.Draw(navmesh, corridor); DrawNeighbors(agent); DrawLocalBoundary(agent); NavDebug.Draw(corners); DrawBase(agent); }
/// <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); }
public void SetVelocityShader(CrowdAgentVelocityShaderDelegate shader) { var eventCallback = new CrowdAgentVelocityShaderFnDelegate((agent, timeStep, desiredVelocity, desiredSpeed) => { unsafe { var pDesiredSpeed = (float *)desiredSpeed.ToPointer(); var pDesiredVelocity = (Vector3 *)desiredVelocity.ToPointer(); shader(CrowdAgent.wrap(agent, true), timeStep, ref *pDesiredVelocity, ref *pDesiredSpeed); } }); IntPtr handle = GCHandle.ToIntPtr(GCHandle.Alloc(eventCallback)); IntPtr callback = Marshal.GetFunctionPointerForDelegate(eventCallback); Urho3D_CrowdManager_SetVelocityShader(swigCPtr, callback, handle); }
public void EvaluateVelocity(float velocity) { //_factoredThreshold = // runnerMaxVelocity * velocityThreshold; //print(velocity); if (useProximityEffects) { _finalVelocityThreshold = velocityThreshold + tresholdCrowdDensityCurve.Value.Evaluate(_friendsNear / maxFriendsForEffect); } else { _finalVelocityThreshold = velocityThreshold; } //print("Received Velocity: " + velocity); _dGivenVelocity = velocity; _dInContact = true; if (velocity < _finalVelocityThreshold) { if (useProximityEffects) { foreach (Collider c in _friendSearchAoE.HitsAndAffect.Keys) { CrowdAgent a = c.GetComponent <CrowdAgent>(); if (a) { a._allowRunner = false; a.OnExpireRunnerEvent.Invoke(); a.StartCoroutine(ResetCollider()); } } } _allowRunner = false; } else { _allowRunner = true; } StartCoroutine(ResetCollider()); }
private NavStatus generateNavPath(NavmeshPoint start, NavmeshPoint end) { if (mCrowdAgent == null) { mCrowdAgent = mCrowdManager.AddAgent(start.point, mCrowdAgentParams); mCrowdAgent.RequestMoveTarget(end); } else { if (mCrowdAgent.Position != end.point) { mCrowdManager.RemoveAgent(mCrowdAgent); mCrowdAgent = mCrowdManager.AddAgent(start.point, mCrowdAgentParams); mCrowdAgent.RequestMoveTarget(end); } } //return status; return(NavStatus.Sucess); }
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."); } }
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."); } }
/// <summary> /// Draws agent neighbor information. /// </summary> /// <param name="agent">The agent to draw.</param> public void DrawNeighbors(CrowdAgent agent) { int neighborCount = agent.NeighborCount; if (neighborCount == 0) { return; } agent.GetNeighbors(neighbors); for (int i = 0; i < neighborCount; i++) { CrowdAgent n = agent.GetNeighbor(neighbors[i]); if (n == null) { // Not sure why this happens. Bug in CrowdAgent? continue; } DebugDraw.Arrow(agent.Position, n.Position, 0, 0.05f, neighborColor); DebugDraw.Circle(n.Position, agent.GetConfig().radius, neighborColor); } }
/// <summary> /// Draws agent local boundary information. /// </summary> /// <param name="agent">The agent to draw.</param> public void DrawLocalBoundary(CrowdAgent agent) { agent.GetBoundary(boundary); if (boundary.segmentCount == 0) return; DebugDraw.XMarker(boundary.center , 0.1f, boundaryColor); DebugDraw.SimpleMaterial.SetPass(0); GL.Begin(GL.LINES); GL.Color(boundaryColor); for (int i = 0; i < boundary.segmentCount; i++) { int p = i * 2; GL.Vertex(boundary.segments[p]); GL.Vertex(boundary.segments[p + 1]); } GL.End(); }
/// <summary> /// Draws agent neighbor information. /// </summary> /// <param name="agent">The agent to draw.</param> public void DrawNeighbors(CrowdAgent agent) { int neighborCount = agent.NeighborCount; if (neighborCount == 0) return; agent.GetNeighbors(neighbors); for (int i = 0; i < neighborCount; i++) { CrowdAgent n = agent.GetNeighbor(neighbors[i]); if (n == null) // Not sure why this happens. Bug in CrowdAgent? continue; DebugDraw.Arrow(agent.Position, n.Position, 0, 0.05f, neighborColor); DebugDraw.Circle(n.Position, agent.GetConfig().radius, neighborColor); } }
internal void _SetAgent(CrowdAgent agent) { m_Agent = agent; }
/// <summary> /// Adds the agent to the crowd if it is not already in the crowd. [Owner: Planner] /// </summary> /// <remarks> /// <para> /// Notes: /// </para> /// <ul> /// <li>Does not submit a move request to the crowd.</li> /// <li>Handles the <see cref="NavFlags.CrowdConfigUpdated"/> flag.</li> /// <li>Checks <paramref name="position"/> against the active agent. If it is different, /// the agent will be repositioned. (Removed/re-added.) /// </li> /// </ul> /// </remarks> /// <param name="position">The position to use when adding the agent. (Usually a known, /// good navigation mesh position.) /// </param> /// <returns> /// The result of the add. (Or the current agent reference if no add was needed.) /// </returns> public CrowdAgent AddToCrowd(Vector3 position) { if (navGroup.crowd == null) return null; if (crowdAgent == null) { crowdAgent = navGroup.crowd.AddAgent(position, crowdConfig); } else { if (crowdAgent.Position != position) { RemoveFromCrowd(); crowdAgent = navGroup.crowd.AddAgent(position, crowdConfig); } else if ((data.flags | NavFlag.CrowdConfigUpdated) != 0) crowdAgent.SetConfig(crowdConfig); } data.flags &= ~NavFlag.CrowdConfigUpdated; return crowdAgent; }