Table of Contents

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

value float

The 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

value float

The 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

value float

The 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

start Color

Start color.

end Color

End color.

amount float

Value between 0 and 1 indicating the weight of end; values outside that range are clamped to it.

easingFunction EasingFunction

The 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

start Color

Start color.

end Color

End color.

amount float

Value between 0 and 1 indicating the weight of end; values outside that range are clamped to it.

easingFunction EasingFunction

The function used to ease the interpolation.

result Color

When 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

start Vector2

Start vector.

end Vector2

End vector.

amount float

Value between 0 and 1 indicating the weight of end; values outside that range are clamped to it.

easingFunction EasingFunction

The 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

start Vector2

Start vector.

end Vector2

End vector.

amount float

Value between 0 and 1 indicating the weight of end; values outside that range are clamped to it.

easingFunction EasingFunction

The function used to ease the interpolation.

result Vector2

When 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

start Vector3

Start vector.

end Vector3

End vector.

amount float

Value between 0 and 1 indicating the weight of end; values outside that range are clamped to it.

easingFunction EasingFunction

The 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

start Vector3

Start vector.

end Vector3

End vector.

amount float

Value between 0 and 1 indicating the weight of end; values outside that range are clamped to it.

easingFunction EasingFunction

The function used to ease the interpolation.

result Vector3

When 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

start Vector4

Start vector.

end Vector4

End vector.

amount float

Value between 0 and 1 indicating the weight of end; values outside that range are clamped to it.

easingFunction EasingFunction

The 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

start Vector4

Start vector.

end Vector4

End vector.

amount float

Value between 0 and 1 indicating the weight of end; values outside that range are clamped to it.

easingFunction EasingFunction

The function used to ease the interpolation.

result Vector4

When 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

start float

Start value.

end float

End value.

amount float

Value between 0 and 1 indicating the weight of end; values outside that range are clamped to it.

easingFunction EasingFunction

The function used to ease the interpolation.

Returns

float

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

eye Vector3

The postion of the observer. i.e. camera

target Vector3

The location of the object to look-at.

up Vector3

The 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

eye Vector3

The postion of the observer. i.e. camera

target Vector3

The location of the object to look-at.

up Vector3

The vector that defines which direction is up.

result Quaternion

The 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

normal Vector3

The normal vector.

tangent Vector3

The tangent vector.

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

value float

The 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

rotationEulerXYZ Vector3

The 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

rotationEulerXYZ Vector3

The euler rotation, with XYZ order.

result Quaternion

Resulting quaternion rotation

ToRotationEulerXYZ(Quaternion)

Convert Quaternion to rotation Euler angles.

public static Vector3 ToRotationEulerXYZ(this Quaternion rotation)

Parameters

rotation Quaternion

The 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

rotation Quaternion

The rotation.

result Vector3

Reulting euler rotation, with XYZ order.