public Steering GetFreeSteering(ESteeringType type, Body character, SteeringProperties properties, Location target = null) { if (type == ESteeringType.None) { return(null); } Steering steering = (Steering)_pools[(int)type].GetFreeResource(); steering.Init(character, properties, target); return(steering); }
public static SteeringOutput Flee(Body character, Vector2 target, SteeringProperties properties) { SteeringOutput output = new SteeringOutput(); // First work out the direction output.Linear = character.position; output.Linear -= target; // If there is no direction, do nothing output.Linear = MathHelper.ConstructMovement(output.Linear, properties.maxAcceleration, 0.01f); return(output); }
public static SteeringOutput Arrive(Body character, Vector2 target, SteeringProperties properties) { SteeringOutput output = new SteeringOutput(); // First work out the direction output.Linear = target; output.Linear -= character.position; float squareDistance = output.Linear.SqrMagnitude(); // If there is no direction, do nothing if (squareDistance < properties.targetRadius * properties.targetRadius) { output.Linear = Vector2.zero; output.StopVelocity = true; return(output); } output.Linear.Normalize(); output.Linear *= properties.maxSpeed; // if we are outside the slowRadius, then go maxSpeed (and no changement) // Otherwise calculate a scaled speed if (squareDistance < properties.slowRadius * properties.slowRadius) { output.Linear *= Mathf.Sqrt(squareDistance) / properties.slowRadius; } // acceleration tries to get to the target velocity output.Linear -= character.velocity; if (properties.timeToTarget == 0f) { output.Linear *= properties.maxAcceleration; } else { output.Linear /= properties.timeToTarget; } // If that is too fast, then clip the speed output.Linear = Vector2.ClampMagnitude(output.Linear, properties.maxAcceleration); return(output); }
public void SetProperties(string propertiesName) { _steeringProperties = SteeringPropertiesTable.instance.GetProperties(propertiesName); if (_steeringProperties != null) { if (_steering) { _steering.Init(_body, _steeringProperties, null); } } else { Debug.LogWarning("Can't find the properties in the table. Verify the name : \"" + propertiesName + "\""); } }
public static SteeringOutput Align(Body character, float targetOrientation, SteeringProperties properties) { SteeringOutput output = new SteeringOutput(); // Get the naive direction to the target float rotation = targetOrientation - character.orientationInDegree; rotation = MathHelper.NormalizeAngle(rotation, false); float rotationSize = Mathf.Abs(rotation); // check if we are already there, is yes, return a void output if (rotationSize < properties.targetAngularRadius) { output.StopRotation = true; return(output); } float targetRotation = properties.maxRotation; // if we are inside the slowRadius, then we calculate a scaled rotation if (rotationSize < properties.slowAngularRadius) { targetRotation *= rotationSize / properties.slowAngularRadius; } // the final target rotation combines speed (already in the variable) // and the direction targetRotation *= Mathf.Sign(rotation); // Acceleration tries to get to the target rotation output.AngularInDegree = targetRotation - character.rotation; output.AngularInDegree /= properties.timeToTarget; // Check if acceleration is too great float angularAcceleration = Mathf.Abs(output.AngularInDegree); if (angularAcceleration > properties.maxAngularAcceleration) { output.AngularInDegree /= angularAcceleration; output.AngularInDegree *= properties.maxAngularAcceleration; } return(output); }
public static SteeringOutput Face(Body character, Vector2 target, SteeringProperties properties) { // First work out the direction Vector2 direction = target - character.position; // check if zero direction, and make no change if so if (direction.magnitude < 0.01f) { return(new SteeringOutput()); } // Put the target together float targetOrientation = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; return(Align(character, targetOrientation, properties)); }