public override void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { base.AddPerception(agent, surroundings); foreach (string tag in filterTags) { surroundings.AddGlobalSearchTag(tag); } }
public void AddPerceptions(SteeringAgent agent, SurroundingsContainer surroundings) { surroundings.Clear(); for (int i = 0; i < behaviors.Length; i++) { behaviors[i].AddPerception(agent, surroundings); } }
void Flock(SurroundingsContainer surroundings) { foreach (SteeringBehavior behavior in activeSettings.Behaviors) { if (!behavior.IsActive) { continue; } behavior.GetSteeringBehaviorVector(out steerCached, this, surroundings); steerCached *= behavior.weight; if (behavior.DrawSteering) { Debug.DrawRay(transform.position, myNeighborhood.transform.TransformDirection(steerCached), behavior.debugColor); } ApplyForce(steerCached); } }
public static HashSet <Agent> GetFilteredAgents(SurroundingsContainer surroundings, SteeringBehavior behavior) { if (!behavior.useTagFilter) { return(surroundings.allAgents); } _filterCache.Clear(); foreach (Agent other in surroundings.allAgents) { for (int i = 0; i < behavior.filterTags.Length; i++) { if (other.CompareTag(behavior.filterTags[i])) { _filterCache.Add(other); break; } } } return(_filterCache); }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { Vector3 sum = Vector3.zero; int count = 0; foreach (Agent other in GetFilteredAgents(surroundings, this)) { if (WithinEffectiveRadius(mine, other)) { float modFactor = 1; sum += (other.Velocity) * modFactor; count++; } } if (count > 0) { sum /= ((float)count); mine.GetSteerVector(out steer, sum); } else { steer = Vector3.zero; } }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { float rayDist = lookAheadSeconds * mine.Velocity.magnitude; Ray myRay = new Ray(mine.WorldPosition, mine.WorldForward); if (!ObstacleInPath(myRay, mine.shape.radius + clearance, rayDist, ref hit, mask)) { steer = Vector3.zero; mine.SetAgentProperty(lastClearDirectionKey, steer); return; } float hitDist = hit.distance; Vector3 lastClearWorldDirection = Vector3.zero; if (mine.HasAgentProperty(lastClearDirectionKey)) { lastClearWorldDirection = mine.GetAgentProperty <Vector3>(lastClearDirectionKey); } if (lastClearWorldDirection == Vector3.zero) { lastClearWorldDirection = myRay.direction; } myRay.direction = lastClearWorldDirection; steer = ObstacleRays(myRay, mine.shape.radius + clearance, rayDist, ref hit, mask); mine.SetAgentProperty(lastClearDirectionKey, steer.normalized); float smooth = (1f - (hitDist / rayDist)); steer = mine.WorldToFlockBoxDirection(steer); steer = steer.normalized * mine.activeSettings.maxForce - mine.Velocity; steer *= smooth; }
public override void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { base.AddPerception(agent, surroundings); surroundings.SetMinPerceptionRadius(effectiveRadius); }
public virtual void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { //steer used as midpoint to prevent garbage steer = Vector3.zero; float count = 0; foreach (Agent other in GetFilteredAgents(surroundings, this)) { if (WithinEffectiveRadius(mine, other)) { steer += (other.Position); count++; } } if (count > 0) { steer /= (count); mine.GetSeekVector(out steer, steer); } else { steer = Vector3.zero; } }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { foreach (Agent a in GetFilteredAgents(surroundings, this)) { //another agent is ahead if (WithinEffectiveRadius(mine, a)) { //use brake force steer = -mine.Velocity; steer = steer.normalized * Mathf.Min(steer.magnitude, mine.activeSettings.maxForce); return; } } steer = Vector3.zero; }
public override void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { base.AddPerception(agent, surroundings); Perception.radius = queueRadius; surroundings.AddPerceptionShape(Perception, (agent.Position + (agent.Forward * queueDistance))); }
public override void AddPerception(SteeringAgent agent, SurroundingsContainer surroundings) { base.AddPerception(agent, surroundings); surroundings.SetMinLookAheadSeconds(lookAheadSeconds); }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { if (!mine.HasAgentProperty(targetIDAttributeName)) { mine.SetAgentProperty(targetIDAttributeName, -1); } int chosenTargetID = mine.GetAgentProperty <int>(targetIDAttributeName); HashSet <Agent> allTargets = GetFilteredAgents(surroundings, this); if (allTargets.Count == 0) { if (HasPursuitTarget(mine)) { DisengagePursuit(mine, chosenTargetID); } steer = Vector3.zero; return; } Agent closestTarget = ClosestPursuableTarget(allTargets, mine); if (!closestTarget || !closestTarget.CanBeCaughtBy(mine)) { if (HasPursuitTarget(mine)) { DisengagePursuit(mine, chosenTargetID); } steer = Vector3.zero; return; } if (closestTarget.agentID != chosenTargetID) { DisengagePursuit(mine, chosenTargetID); EngagePursuit(mine, closestTarget); } Vector3 distance = closestTarget.Position - mine.Position; float est_timeToIntercept = distance.magnitude / mine.activeSettings.maxSpeed; Vector3 predictedInterceptPosition = closestTarget.Position + closestTarget.Velocity * est_timeToIntercept; AttemptCatch(mine, closestTarget); mine.GetSeekVector(out steer, predictedInterceptPosition); }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { if (!mine.HasAgentProperty(targetIDAttributeName)) { mine.SetAgentProperty(targetIDAttributeName, -1); } int chosenTargetID = mine.GetAgentProperty <int>(targetIDAttributeName); HashSet <Agent> allTargets = GetFilteredAgents(surroundings, this); if (allTargets.Count == 0) { if (chosenTargetID != -1) { DisengagePursuit(mine, chosenTargetID); } steer = Vector3.zero; return; } Agent closestTarget = ClosestPursuableTarget(allTargets, mine); if (!closestTarget || !closestTarget.CanBeCaughtBy(mine)) { if (chosenTargetID != -1) { DisengagePursuit(mine, chosenTargetID); } steer = Vector3.zero; return; } if (closestTarget.agentID != chosenTargetID) { DisengagePursuit(mine, chosenTargetID); EngagePursuit(mine, closestTarget); } AttemptCatch(mine, closestTarget); Vector3 desired_velocity = (closestTarget.Position - mine.Position).normalized * mine.activeSettings.maxSpeed; steer = desired_velocity - mine.Velocity; steer = steer.normalized * Mathf.Min(steer.magnitude, mine.activeSettings.maxForce); }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { HashSet <Agent> leaders = GetFilteredAgents(surroundings, this); if (leaders.Count == 0) { mine.RemoveAgentProperty(leaderIDAttributeName); steer = Vector3.zero; return; } Agent closestLeader = SeekBehavior.ClosestPursuableTarget(leaders, mine); mine.SetAgentProperty(leaderIDAttributeName, closestLeader.agentID); //check to see if we should clear the way in front of the leader float scalar = Vector3.Dot(mine.Position - closestLeader.Position, closestLeader.Forward); if (scalar > 0 && scalar < clearAheadDistance)//we are somewhere in front of the leader, potentially in the clear zone ahead of it. { pointOnLeaderPath_cached = closestLeader.Position + closestLeader.Forward * scalar; float insideClearZone = (pointOnLeaderPath_cached - mine.Position).sqrMagnitude / (clearAheadRadius * clearAheadRadius); //0-1 is inside zone, <1 is outside if (insideClearZone <= 1) { steer = (mine.Position - pointOnLeaderPath_cached).normalized * (1f - insideClearZone) * mine.activeSettings.maxForce; return; } } Vector3 desired_velocity = ArriveBehavior.DesiredVelocityForArrival(mine, closestLeader.Position - closestLeader.Forward * followDistance, stoppingRadius); steer = desired_velocity - mine.Velocity; steer = steer.normalized * Mathf.Min(steer.magnitude, mine.activeSettings.maxForce); }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { Vector3 fleeMidpoint = Vector3.zero; float count = 0; foreach (Agent other in GetFilteredAgents(surroundings, this)) { if (WithinEffectiveRadius(mine, other)) { fleeMidpoint += (other.Position); count++; } } if (count > 0) { fleeMidpoint /= (count); mine.GetSteerVector(out steer, (mine.Position - fleeMidpoint)); mine.SetAgentProperty(fleeAttributeName, true); } else { mine.SetAgentProperty(fleeAttributeName, false); steer = Vector3.zero; } }
#pragma warning restore 0414 public abstract void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings);
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { steer = Vector3.zero; }
public void GetSurroundings(Vector3 position, Vector3 velocity, HashSet <int> buckets, SurroundingsContainer surroundings) { if (buckets == null) { buckets = new HashSet <int>(); } else { buckets.Clear(); } if (surroundings.perceptionRadius > 0) { GetBucketsOverlappingSphere(position, surroundings.perceptionRadius, buckets); } if (surroundings.lookAheadSeconds > 0) { GetBucketsOverlappingLine(position, position + velocity * surroundings.lookAheadSeconds, buckets); } if (surroundings.perceptionShapes.Count > 0) { foreach (System.Tuple <Shape, Vector3> s in surroundings.perceptionShapes) { switch (s.Item1.type) { case Shape.ShapeType.POINT: GetBucketsOverlappingSphere(s.Item2, s.Item1.radius, buckets); break; case Shape.ShapeType.SPHERE: GetBucketsOverlappingSphere(s.Item2, s.Item1.radius, buckets); break; case Shape.ShapeType.LINE: //TODO: assumes Line Perception will only be in direction of Velocity GetBucketsOverlappingLine(s.Item2, velocity.normalized * s.Item1.length, buckets); break; case Shape.ShapeType.CYLINDER: //TODO: assumes Cylinder Perception will only be in direction of Velocity GetBucketsOverlappingCylinder(s.Item2, velocity.normalized * s.Item1.length, s.Item1.radius, buckets); break; } } } foreach (int bucket in buckets) { if (bucketToAgents.TryGetValue(bucket, out _bucketContentsCache)) { surroundings.AddAgents(_bucketContentsCache); } } if (surroundings.globalSearchTags.Count > 0) { foreach (string agentTag in surroundings.globalSearchTags) { if (tagToAgents.TryGetValue(agentTag, out _bucketContentsCache)) { surroundings.AddAgents(_bucketContentsCache); } } } }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { float uniqueId = mine.gameObject.GetInstanceID() * .001f; uniqueId = uniqueId * uniqueId; steer = Quaternion.Euler( (Mathf.PerlinNoise((Time.time * wanderIntensity), uniqueId) - .5f) * wanderScope, (Mathf.PerlinNoise((Time.time * wanderIntensity) + uniqueId, uniqueId) - .5f) * wanderScope, (Mathf.PerlinNoise((Time.time * wanderIntensity) + uniqueId * 2, uniqueId) - .5f) * wanderScope ) * mine.Forward * mine.activeSettings.maxForce; }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { steer = Vector3.zero; int count = 0; foreach (Agent other in GetFilteredAgents(surroundings, this)) { if (WithinEffectiveRadius(mine, other)) { Vector3 diff = mine.Position - other.Position; if (diff.sqrMagnitude < .001f) { diff = UnityEngine.Random.insideUnitCircle * .01f; } steer += (diff.normalized / diff.magnitude); count++; } } if (count > 0) { steer /= ((float)count); } if (steer.magnitude > 0) { mine.GetSteerVector(out steer, steer); } }
public override void GetSteeringBehaviorVector(out Vector3 steer, SteeringAgent mine, SurroundingsContainer surroundings) { HashSet <Agent> obstacles = GetFilteredAgents(surroundings, this); if (obstacles.Count == 0) { steer = Vector3.zero; return; } Ray myRay = new Ray(mine.Position, mine.Forward); float rayDist = surroundings.lookAheadSeconds * mine.Velocity.magnitude; bool foundObstacleInPath = false; foreach (Agent obstacle in obstacles) { if (obstacle.RaycastToShape(myRay, mine.shape.radius + clearance, rayDist, out hit)) { if (!foundObstacleInPath || hit.distance < closestHit.distance) { closestHit = hit; mostImmediateObstacle = obstacle; } foundObstacleInPath = true; } } if (!foundObstacleInPath) { steer = Vector3.zero; return; } mostImmediateObstacle.FindNormalToSteerAwayFromShape(myRay, closestHit, mine.shape.radius, ref normal); steer = normal; steer = steer.normalized * mine.activeSettings.maxForce; steer *= (1f - (closestHit.distance / rayDist)); }