Class MathUtilEx
- Namespace
- Stride.CommunityToolkit.Mathematics
- Assembly
- Stride.CommunityToolkit.dll
Some more common utility methods for math operations.
public static class MathUtilEx
- Inheritance
-
MathUtilEx
Methods
CeilingToInt(float)
Gets the smallest integer greater than or equal to the amount.
public static int CeilingToInt(this float value)
Parameters
valuefloatThe value.
Returns
- int
The smallest integer greater than or equal to the amount.
Clamp01(float)
Clamps the value between 0 and 1.
public static float Clamp01(float value)
Parameters
valuefloatThe Value.
Returns
- float
Value clamped between 0 and 1.
FloorToInt(float)
Gets largest integer less than or equal to the amount.
public static int FloorToInt(this float value)
Parameters
valuefloatThe value.
Returns
- int
The largest integer less than or equal to the amount.
Interpolate(Color, Color, float, EasingFunction)
Performs an interpolation between two colors using an easing function.
public static Color Interpolate(Color start, Color end, float amount, EasingFunction easingFunction)
Parameters
startColorStart color.
endColorEnd color.
amountfloatValue between 0 and 1 indicating the weight of
end; values outside that range are clamped to it.easingFunctionEasingFunctionThe function used to ease the interpolation.
Returns
- Color
The interpolation of the two colors.
Remarks
Passing amount a value of 0 will cause start to be returned; a value of 1 will cause end to be returned.
Interpolate(ref Color, ref Color, float, EasingFunction, out Color)
Performs an interpolation between two colors using an easing function.
public static void Interpolate(ref Color start, ref Color end, float amount, EasingFunction easingFunction, out Color result)
Parameters
startColorStart color.
endColorEnd color.
amountfloatValue between 0 and 1 indicating the weight of
end; values outside that range are clamped to it.easingFunctionEasingFunctionThe function used to ease the interpolation.
resultColorWhen the method completes, contains the interpolation of the two colors.
Remarks
Passing amount a value of 0 will cause start to be returned; a value of 1 will cause end to be returned.
Interpolate(Vector2, Vector2, float, EasingFunction)
Performs an interpolation between two vectors using an easing function.
public static Vector2 Interpolate(Vector2 start, Vector2 end, float amount, EasingFunction easingFunction)
Parameters
startVector2Start vector.
endVector2End vector.
amountfloatValue between 0 and 1 indicating the weight of
end; values outside that range are clamped to it.easingFunctionEasingFunctionThe function used to ease the interpolation.
Returns
- Vector2
The interpolation of the two vectors.
Remarks
Passing amount a value of 0 will cause start to be returned; a value of 1 will cause end to be returned.
Interpolate(ref Vector2, ref Vector2, float, EasingFunction, out Vector2)
Performs an interpolation between two vectors using an easing function.
public static void Interpolate(ref Vector2 start, ref Vector2 end, float amount, EasingFunction easingFunction, out Vector2 result)
Parameters
startVector2Start vector.
endVector2End vector.
amountfloatValue between 0 and 1 indicating the weight of
end; values outside that range are clamped to it.easingFunctionEasingFunctionThe function used to ease the interpolation.
resultVector2When the method completes, contains the interpolation of the two vectors.
Remarks
Passing amount a value of 0 will cause start to be returned; a value of 1 will cause end to be returned.
Interpolate(Vector3, Vector3, float, EasingFunction)
Performs an interpolation between two vectors using an easing function.
public static Vector3 Interpolate(Vector3 start, Vector3 end, float amount, EasingFunction easingFunction)
Parameters
startVector3Start vector.
endVector3End vector.
amountfloatValue between 0 and 1 indicating the weight of
end; values outside that range are clamped to it.easingFunctionEasingFunctionThe function used to ease the interpolation.
Returns
- Vector3
The interpolation of the two vectors.
Remarks
Passing amount a value of 0 will cause start to be returned; a value of 1 will cause end to be returned.
Interpolate(ref Vector3, ref Vector3, float, EasingFunction, out Vector3)
Performs an interpolation between two vectors using an easing function.
public static void Interpolate(ref Vector3 start, ref Vector3 end, float amount, EasingFunction easingFunction, out Vector3 result)
Parameters
startVector3Start vector.
endVector3End vector.
amountfloatValue between 0 and 1 indicating the weight of
end; values outside that range are clamped to it.easingFunctionEasingFunctionThe function used to ease the interpolation.
resultVector3When the method completes, contains the interpolation of the two vectors.
Remarks
Passing amount a value of 0 will cause start to be returned; a value of 1 will cause end to be returned.
Interpolate(Vector4, Vector4, float, EasingFunction)
Performs an interpolation between two vectors using an easing function.
public static Vector4 Interpolate(Vector4 start, Vector4 end, float amount, EasingFunction easingFunction)
Parameters
startVector4Start vector.
endVector4End vector.
amountfloatValue between 0 and 1 indicating the weight of
end; values outside that range are clamped to it.easingFunctionEasingFunctionThe function used to ease the interpolation.
Returns
- Vector4
The interpolation of the two vectors.
Remarks
Passing amount a value of 0 will cause start to be returned; a value of 1 will cause end to be returned.
Interpolate(ref Vector4, ref Vector4, float, EasingFunction, out Vector4)
Performs an interpolation between two vectors using an easing function.
public static void Interpolate(ref Vector4 start, ref Vector4 end, float amount, EasingFunction easingFunction, out Vector4 result)
Parameters
startVector4Start vector.
endVector4End vector.
amountfloatValue between 0 and 1 indicating the weight of
end; values outside that range are clamped to it.easingFunctionEasingFunctionThe function used to ease the interpolation.
resultVector4When the method completes, contains the interpolation of the two vectors.
Remarks
Passing amount a value of 0 will cause start to be returned; a value of 1 will cause end to be returned.
Interpolate(float, float, float, EasingFunction)
Performs an interpolation between two values using an easing function.
public static float Interpolate(float start, float end, float amount, EasingFunction easingFunction)
Parameters
startfloatStart value.
endfloatEnd value.
amountfloatValue between 0 and 1 indicating the weight of
end; values outside that range are clamped to it.easingFunctionEasingFunctionThe function used to ease the interpolation.
Returns
Remarks
Passing amount a value of 0 will cause start to be returned; a value of 1 will cause end to be returned.
LookRotation(Vector3, Vector3, Vector3)
Creates a rotation that looks from eye towards target.
public static Quaternion LookRotation(Vector3 eye, Vector3 target, Vector3 up)
Parameters
eyeVector3The position of the observer, such as a camera.
targetVector3The location of the object to look-at.
upVector3The vector that defines which direction is up.
Returns
- Quaternion
The created quaternion rotation
Examples
var cameraRotation = MathUtilEx.LookRotation(cameraPosition, targetPosition, Vector3.UnitY);
LookRotation(ref Vector3, ref Vector3, ref Vector3, out Quaternion)
Creates a rotation that looks from eye towards target.
public static void LookRotation(ref Vector3 eye, ref Vector3 target, ref Vector3 up, out Quaternion result)
Parameters
eyeVector3The position of the observer, such as a camera.
targetVector3The location of the object to look-at.
upVector3The vector that defines which direction is up.
resultQuaternionThe created quaternion rotation
Remarks
The result is always finite and unit length, including for the degenerate inputs: an eye
sitting on the target, a line of sight parallel to up, and a zero-length
up.
MoveTowards(Vector2, Vector2, float)
Moves current towards target along the line between them by at most
maxDelta, landing on the target exactly rather than overshooting it.
public static Vector2 MoveTowards(Vector2 current, Vector2 target, float maxDelta)
Parameters
currentVector2The position now.
targetVector2The position to reach.
maxDeltafloatThe furthest the position may move in this call; a negative value moves it away from the target.
Returns
- Vector2
The target when it is within reach, otherwise
currentmoved bymaxDeltatowards it.
MoveTowards(Vector3, Vector3, float)
Moves current towards target along the line between them by at most
maxDelta, landing on the target exactly rather than overshooting it.
public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDelta)
Parameters
currentVector3The position now.
targetVector3The position to reach.
maxDeltafloatThe furthest the position may move in this call; a negative value moves it away from the target.
Returns
- Vector3
The target when it is within reach, otherwise
currentmoved bymaxDeltatowards it.
MoveTowards(float, float, float)
Moves current towards target by at most maxDelta,
landing on the target exactly rather than overshooting it.
public static float MoveTowards(float current, float target, float maxDelta)
Parameters
currentfloatThe value now.
targetfloatThe value to reach.
maxDeltafloatThe furthest the value may move in this call; a negative value moves it away from the target.
Returns
- float
The target when it is within reach, otherwise
currentmoved bymaxDeltatowards it.
Remarks
A frame-rate independent approach is MoveTowards(value, target, speed * deltaTime): the value arrives at a
constant speed and stops, where a lerp towards the target slows down and never quite arrives.
Orthonormalize(ref Vector3, ref Vector3)
Orthonormalizes 2 vectors.
public static void Orthonormalize(ref Vector3 normal, ref Vector3 tangent)
Parameters
Remarks
Makes vectors normalized and orthogonal to each other. Normalizes normal. Normalizes tangent and makes sure it is orthogonal to normal.
RoundToInt(float)
Gets the integer value nearest to the amount.
public static int RoundToInt(this float value)
Parameters
valuefloatThe value.
Returns
- int
The integer value nearest to the amount.
ToQuaternion(Vector3)
Convert rotation Euler angles to a Quaternion.
public static Quaternion ToQuaternion(this Vector3 rotationEulerXYZ)
Parameters
rotationEulerXYZVector3The euler rotation, with XYZ order.
Returns
- Quaternion
Resulting quaternion rotation
ToQuaternion(ref Vector3, out Quaternion)
Convert rotation Euler angles to a Quaternion.
public static void ToQuaternion(ref Vector3 rotationEulerXYZ, out Quaternion result)
Parameters
rotationEulerXYZVector3The euler rotation, with XYZ order.
resultQuaternionResulting quaternion rotation
ToRotationEulerXYZ(Quaternion)
Convert Quaternion to rotation Euler angles.
public static Vector3 ToRotationEulerXYZ(this Quaternion rotation)
Parameters
rotationQuaternionThe rotation.
Returns
- Vector3
The Euler rotation, in XYZ order.
ToRotationEulerXYZ(ref Quaternion, out Vector3)
Convert Quaternion to rotation Euler angles.
public static void ToRotationEulerXYZ(ref Quaternion rotation, out Vector3 result)
Parameters
rotationQuaternionThe rotation.
resultVector3The Euler rotation, in XYZ order.