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 with the specified forward and upwards directions.
public static Quaternion LookRotation(Vector3 eye, Vector3 target, Vector3 up)
Parameters
eyeVector3The postion of the observer. i.e. 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 = Quaternion.LookRotation(cameraPosition, targetPosition, Vector3.UnitY);
LookRotation(ref Vector3, ref Vector3, ref Vector3, out Quaternion)
Creates a rotation with the specified forward and upwards directions.
public static void LookRotation(ref Vector3 eye, ref Vector3 target, ref Vector3 up, out Quaternion result)
Parameters
eyeVector3The postion of the observer. i.e. 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.
This used to build the quaternion with the single-branch trace formula,
w = sqrt(1 + m11 + m22 + m33) / 2, which is only valid while that sum is positive. A
camera orbited to the far side of its target is a 180 degree rotation, where the sum is
exactly -1: w came out as zero, the reciprocal that follows it divided by zero, and the
quaternion's components became 0 * infinity, which is NaN. Assigning that to a
TransformComponent.Rotation poisoned the entity's matrix, and any position integrated
through that matrix afterwards became NaN as well - a camera that could not be
recovered without resetting its transform. Delegating to
RotationMatrix(Matrix) picks up the branch for each sign of the
trace, which is what makes every orientation safe rather than merely most of them.
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
Reulting euler rotation, with 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.
resultVector3Reulting euler rotation, with XYZ order.