public static Vector2 GetVelocity(SteeringAgent2D agent, IEnumerable <SteeringAgent2D> neighbors) { Vector2 centerOfMass = Vector2.zero, steeringForce = Vector2.zero; foreach (var neighbor in neighbors) { if (neighbor == agent) { continue; } if (agent.TargetAgents.Contains(neighbor)) { continue; //Ignore the target of evasion or pursue } centerOfMass += neighbor.Rigidbody2D.position; } int neighborCount = neighbors.Count(); if (neighborCount > 0) { centerOfMass /= (float)neighborCount; steeringForce = Seek2D.GetVelocity(agent, centerOfMass); } return(steeringForce.normalized); }
public override Vector2 GetVelocity() { //return GetVelocity(agent, agent.Neighbors); Profiler.BeginSample("Cohesion2D"); centerOfMass = Vector2.zero; steeringForce = Vector2.zero; foreach (var neighbor in agent.Neighbors) { if (neighbor == agent) { continue; } if (agent.TargetAgents.Contains(neighbor)) { continue; //Ignore the target of evasion or pursue } centerOfMass += neighbor.Rigidbody2D.position; } int neighborCount = agent.Neighbors.Count(); if (neighborCount > 0) { centerOfMass /= (float)neighborCount; steeringForce = Seek2D.GetVelocity(agent, centerOfMass); } Profiler.EndSample(); return(steeringForce.normalized); }
public static Vector2 GetVelocity(SteeringAgent2D agent, SteeringAgent2D evader, float turnCoefficient = 0f) { if (evader == null) { return(Vector2.zero); } var toEvader = evader.GetComponent <Rigidbody2D>().position - agent.Rigidbody2D.position; float relativeHeading = Vector2.Dot(agent.Heading, evader.transform.up); if ((Vector2.Dot(toEvader, agent.transform.up) > 0) && relativeHeading < -0.95) //acos(0.95) = 18 degs { return(Seek2D.GetVelocity(agent, evader.GetComponent <Rigidbody2D>().position)); } //Not considered ahead so we predict where the evader will be float lookAheadTime = toEvader.magnitude / (agent.MaxSpeed + evader.GetComponent <Rigidbody2D>().velocity.magnitude); if (turnCoefficient != 0) { lookAheadTime += TurnaroundTime(agent, evader.GetComponent <Rigidbody2D>().position, turnCoefficient); } return(Seek2D.GetVelocity(agent, evader.GetComponent <Rigidbody2D>().position + evader.GetComponent <Rigidbody2D>().velocity *lookAheadTime)); }
public static Vector2 GetVelocity(SteeringAgent2D agent, NavMesh2D navMesh, float waypointSeekDistance = 0.5f) { if (navMesh == null || !navMesh.CurrentWaypoint.HasValue) { return(Vector2.zero); } var currentWaypoint = navMesh.CurrentWaypoint.Value; if ((agent.Rigidbody2D.position - currentWaypoint).magnitude < waypointSeekDistance) { navMesh.SetNextWaypoint(); } if (!navMesh.IsFinished()) { return(Seek2D.GetVelocity(agent, navMesh.CurrentWaypoint.Value)); } else { return(Arrive2D.GetVelocity(agent, navMesh.CurrentWaypoint.Value, 2f)); } }