public override Vector3 CalculateNeighborContribution(Vehicle other) { Vector3 steering = Vector3.zero; // add in steering contribution // (opposite of the offset direction, divided once by distance // to normalize, divided another time to get 1/d falloff) Vector3 offset = other.Position - Vehicle.Position; var offsetSqrMag = offset.sqrMagnitude; if (offsetSqrMag <= _maximumSeparationSquared) { steering = (offset / -offsetSqrMag); if (_multiplierInsideComfortDistance != 1 && offsetSqrMag < _comfortDistanceSquared) { steering *= _multiplierInsideComfortDistance; } if (_vehicleRadiusImpact > 0) { steering *= other.ScaledRadius * Vehicle.ScaledRadius * _vehicleRadiusImpact; } } return steering; }
public override Vector3 CalculateNeighborContribution(Vehicle other) { // accumulate sum of forces leading us towards neighbor's positions var distance = other.Position - Vehicle.Position; var sqrMag = distance.sqrMagnitude; if (sqrMag < _comfortDistanceSquared) { distance = Vector3.zero; } else { distance *= 1 / sqrMag; } return distance; }
public override Vector3 CalculateNeighborContribution(Vehicle other) { // accumulate sum of neighbors' velocities return other.Velocity; }
protected virtual void Awake() { _vehicle = this.GetComponent<Vehicle>(); ReportedArrival = true; // Default to true to avoid unnecessary notifications }
public float PredictNearestApproachTime(Vehicle other) { return PredictNearestApproachTime(other, Velocity); }
/// <summary> /// Predicts the time until nearest approach between this and another vehicle /// </summary> /// <returns> /// The nearest approach time. /// </returns> /// <param name='other'> /// Other vehicle to compare against /// </param> public float PredictNearestApproachTime(Vehicle other, Vector3 myVelocity) { // imagine we are at the origin with no velocity, // compute the relative velocity of the other vehicle Vector3 otherVelocity = other.Velocity; Vector3 relVelocity = otherVelocity - myVelocity; float relSpeed = relVelocity.magnitude; // for parallel paths, the vehicles will always be at the same distance, // so return 0 (aka "now") since "there is no time like the present" if (relSpeed == 0) return 0; // Now consider the path of the other vehicle in this relative // space, a line defined by the relative position and velocity. // The distance from the origin (our vehicle) to that line is // the nearest approach. // Take the unit tangent along the other vehicle's path Vector3 relTangent = relVelocity / relSpeed; // find distance from its path to origin (compute offset from // other to us, find length of projection onto path) Vector3 relPosition = Position - other.Position; float projection = Vector3.Dot(relTangent, relPosition); return projection / relSpeed; }
/// <summary> /// Calculates if a vehicle is in the neighborhood of another /// </summary> /// <param name="other"> /// Another vehicle to check against<see cref="Vehicle"/> /// </param> /// <param name="minDistance"> /// Minimum distance <see cref="System.Single"/> /// </param> /// <param name="maxDistance"> /// Maximum distance <see cref="System.Single"/> /// </param> /// <param name="cosMaxAngle"> /// Cosine of the maximum angle between vehicles (for performance)<see cref="System.Single"/> /// </param> /// <returns> /// True if within the neighborhood, or false if otherwise<see cref="System.Boolean"/> /// </returns> /// <remarks>Originally SteerLibrary.inBoidNeighborhood</remarks> public bool IsInNeighborhood(Vehicle other, float minDistance, float maxDistance, float cosMaxAngle) { bool result = false; if (other != this) { Vector3 offset = other.Position - Position; float distanceSquared = offset.sqrMagnitude; // definitely in neighborhood if inside minDistance sphere if (distanceSquared < (minDistance * minDistance)) { result = true; } else { // definitely not in neighborhood if outside maxDistance sphere if (distanceSquared > (maxDistance * maxDistance)) { result = false; } else { // otherwise, test angular offset from forward axis Vector3 unitOffset = offset / (float) Mathf.Sqrt (distanceSquared); float forwardness = Vector3.Dot(Transform.forward, unitOffset); result = forwardness > cosMaxAngle; } } } return result; }
/// <summary> /// Returns the distance from the this vehicle to another /// </summary> /// <returns> /// The distance between both vehicles' positions. If negative, they are overlapping. /// </returns> /// <param name='other'> /// Vehicle to compare against. /// </param> public float DistanceFromPerimeter(Vehicle other) { var diff = Position - other.Position; return diff.magnitude - ScaledRadius - other.ScaledRadius; }
/// <summary> /// Given the time until nearest approach (predictNearestApproachTime) /// determine position of each vehicle at that time, and the distance /// between them /// </summary> /// <returns> /// Distance between positions /// </returns> /// <param name='other'> /// Other vehicle to compare against /// </param> /// <param name='time'> /// Time to estimate. /// </param> /// <param name='ourPosition'> /// Our position. /// </param> /// <param name='hisPosition'> /// The other vehicle's position. /// </param> /// <param name='ourForward'> /// Forward vector to use instead of the vehicle's. /// </param> public float ComputeNearestApproachPositions(Vehicle other, float time, ref Vector3 ourPosition, ref Vector3 hisPosition, float ourSpeed, Vector3 ourForward) { Vector3 myTravel = ourForward * ourSpeed * time; Vector3 otherTravel = other.Transform.forward * other.Speed * time; ourPosition = Position + myTravel; hisPosition = other.Position + otherTravel; return Vector3.Distance(ourPosition, hisPosition); }
/// <summary> /// Given the time until nearest approach (predictNearestApproachTime) /// determine position of each vehicle at that time, and the distance /// between them /// </summary> /// <returns> /// Distance between positions /// </returns> /// <param name='other'> /// Other vehicle to compare against /// </param> /// <param name='time'> /// Time to estimate. /// </param> /// <param name='ourPosition'> /// Our position. /// </param> /// <param name='hisPosition'> /// The other vehicle's position. /// </param> public float ComputeNearestApproachPositions(Vehicle other, float time, ref Vector3 ourPosition, ref Vector3 hisPosition) { return ComputeNearestApproachPositions(other, time, ref ourPosition, ref hisPosition, Speed, Transform.forward); }
public override Vector3 CalculateNeighborContribution(Vehicle other) { // accumulate sum of neighbor's heading return other.Transform.forward; }