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 that looks from eye towards target.

public static Quaternion LookRotation(Vector3 eye, Vector3 target, Vector3 up)

Parameters

eye Vector3

The position of the observer, such as a 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 = 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

eye Vector3

The position of the observer, such as a 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.

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

current Vector2

The position now.

target Vector2

The position to reach.

maxDelta float

The 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 current moved by maxDelta towards 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

current Vector3

The position now.

target Vector3

The position to reach.

maxDelta float

The 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 current moved by maxDelta towards 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

current float

The value now.

target float

The value to reach.

maxDelta float

The 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 current moved by maxDelta towards 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

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

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

rotation Quaternion

The rotation.

result Vector3

The Euler rotation, in XYZ order.