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, SteeringAgent2D target, Vector2 offset, float slowingDistance = 1f) { Vector2 worldOffsetPos = target.transform.TransformPoint(offset); Vector2 toOffset = worldOffsetPos - agent.Rigidbody2D.position; float lookAheadTime = toOffset.magnitude / (agent.MaxSpeed + target.GetComponent <Rigidbody2D>().velocity.magnitude); return(Arrive2D.GetVelocity(agent, worldOffsetPos + target.GetComponent <Rigidbody2D>().velocity *lookAheadTime, slowingDistance)); }
public static Vector2 GetVelocity(SteeringAgent2D agent, SteeringAgent2D target, float hidingDistanceFromObstacle, float slowingDistance = 1f, float panicDistance = -1) { if (panicDistance > 0) { var sqrDistance = (agent.Rigidbody2D.position - target.GetComponent <Rigidbody2D>().position).magnitude; if (sqrDistance > panicDistance) { return(Vector2.zero); } } float distToClosest = float.MaxValue; Vector2 bestHidingSpot = Vector2.zero; foreach (var obstacle in World2D.Instance.Obstacles) { var hidingSpot = GetHidingPosition(obstacle.transform.position, obstacle.radius, target.GetComponent <Rigidbody2D>().position, hidingDistanceFromObstacle); var dist = (hidingSpot - agent.Rigidbody2D.position).magnitude; if (dist < distToClosest) { distToClosest = dist; bestHidingSpot = hidingSpot; } } if (distToClosest == float.MaxValue) { return(Evade2D.GetVelocity(agent, target)); } return(Arrive2D.GetVelocity(agent, bestHidingSpot, slowingDistance)); }
public static Vector2 GetVelocity(SteeringAgent2D agent, SteeringAgent2D pursuer, float panicDistance = -1) { if (pursuer == null) { return(Vector2.zero); } var toPursuer = pursuer.GetComponent <Rigidbody2D>().position - agent.Rigidbody2D.position; if (panicDistance != -1 && toPursuer.sqrMagnitude > panicDistance * panicDistance) { return(Vector2.zero); } //Not considered ahead so we predict where the evader will be float lookAheadTime = toPursuer.magnitude / (agent.MaxSpeed + pursuer.GetComponent <Rigidbody2D>().velocity.magnitude); return(Flee2D.GetVelocity(agent, pursuer.GetComponent <Rigidbody2D>().position + pursuer.GetComponent <Rigidbody2D>().velocity *lookAheadTime)); }
public void UpdateEntity(SteeringAgent2D entity, Vector2 previousPosition) { Vector2 currentPosition = entity.GetComponent <Rigidbody2D>().position; int prevIndex = IndexFromPosition(previousPosition); int curIndex = IndexFromPosition(currentPosition); if (prevIndex != curIndex) { Cells[prevIndex].Members.Remove(entity); Cells[curIndex].Members.Add(entity); } }
public static Vector2 GetVelocity(SteeringAgent2D thisAgent, SteeringAgent2D agentA, SteeringAgent2D agentB, float slowingDistance = 1f) { if (agentA == null || agentB == null) { return(Vector2.zero); } if (slowingDistance < 0) { throw new InvalidOperationException("The slowing distance can't be negative."); } Vector2 midPoint = (agentA.GetComponent <Rigidbody2D>().position + agentB.GetComponent <Rigidbody2D>().position) / 2; float timeToReachMidPoint = (thisAgent.Rigidbody2D.position - midPoint).magnitude / thisAgent.MaxSpeed; //Position in the future Vector2 AFuturePos = agentA.GetComponent <Rigidbody2D>().position + agentA.GetComponent <Rigidbody2D>().velocity *timeToReachMidPoint; Vector2 BFuturePos = agentB.GetComponent <Rigidbody2D>().position + agentB.GetComponent <Rigidbody2D>().velocity *timeToReachMidPoint; midPoint = (AFuturePos + BFuturePos) / 2; return(Arrive2D.GetVelocity(thisAgent, midPoint, slowingDistance)); }