Class MathUtility
Collection of optimized math utility functions for WheelController3D. Provides garbage-free matrix and quaternion operations to minimize allocations in the physics loop. All methods use aggressive inlining for maximum performance.
Namespace: NWH.WheelController3D
Assembly: NWH.WheelController.dll
Syntax
public static class MathUtility
Remarks
These utilities avoid creating new objects or structs, instead modifying existing values by reference. This is critical for performance when called every physics frame for multiple wheels. Methods are marked with AggressiveInlining to eliminate function call overhead.
Methods
| Edit this page View SourceAbs(float)
Branchless absolute value.
Declaration
public static float Abs(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value | Input value. |
Returns
| Type | Description |
|---|---|
| float | Absolute value of |
ApplyRotationToVector(ref Quaternion, ref Vector3)
Applies a quaternion rotation to a vector, modifying the vector in place. Equivalent to vector = rotation * vector but without creating a new Vector3.
Declaration
public static void ApplyRotationToVector(ref Quaternion rotation, ref Vector3 vector)
Parameters
| Type | Name | Description |
|---|---|---|
| Quaternion | rotation | Rotation to apply |
| Vector3 | vector | Vector to rotate (modified in place) |
ApplyVelocity(Matrix4x4, Vector3, Vector3, float)
Integrates linear and angular velocity into a transformation matrix over a timestep. Used for predicting future positions/orientations based on current velocities.
Declaration
public static Matrix4x4 ApplyVelocity(Matrix4x4 matrix, Vector3 velocity, Vector3 angularVelocity, float deltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix4x4 | matrix | Starting transformation matrix |
| Vector3 | velocity | Linear velocity in m/s |
| Vector3 | angularVelocity | Angular velocity in rad/s |
| float | deltaTime | Time step in seconds |
Returns
| Type | Description |
|---|---|
| Matrix4x4 | New transformation matrix after applying velocities |
Clamp01(float)
Clamps a value to the [0, 1] range.
Declaration
public static float Clamp01(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value | Input value. |
Returns
| Type | Description |
|---|---|
| float |
|
CopyMatrix(ref Matrix4x4, ref Matrix4x4)
Copies all values from one matrix to another without creating new objects. Faster than destination = source when matrices are already allocated.
Declaration
public static void CopyMatrix(ref Matrix4x4 source, ref Matrix4x4 destination)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix4x4 | source | Source matrix to copy from |
| Matrix4x4 | destination | Destination matrix to copy to |
CreateAngleAxisRotationMatrix(float, ref Vector3, ref Matrix4x4)
Creates a rotation matrix from an angle and axis without allocating memory. Equivalent to Matrix4x4.Rotate(Quaternion.AngleAxis(...)) but with zero allocations.
Declaration
public static void CreateAngleAxisRotationMatrix(float angle, ref Vector3 axis, ref Matrix4x4 result)
Parameters
| Type | Name | Description |
|---|---|---|
| float | angle | Rotation angle in degrees |
| Vector3 | axis | Rotation axis (will be normalized internally) |
| Matrix4x4 | result | Output matrix to populate with the rotation |
Cross(float, float, float, float, float, float, out float, out float, out float)
Inline cross product with float components. Writes result to out parameter.
Declaration
public static void Cross(float ax, float ay, float az, float bx, float by, float bz, out float rx, out float ry, out float rz)
Parameters
| Type | Name | Description |
|---|---|---|
| float | ax | |
| float | ay | |
| float | az | |
| float | bx | |
| float | by | |
| float | bz | |
| float | rx | |
| float | ry | |
| float | rz |
Cross(ref Vector3, ref Vector3, out Vector3)
Inline cross product. Writes result to out parameter, avoiding Vector3 allocation.
Declaration
public static void Cross(ref Vector3 a, ref Vector3 b, out Vector3 result)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | a | |
| Vector3 | b | |
| Vector3 | result |
Dot(ref Vector3, ref Vector3)
Inline dot product.
Declaration
public static float Dot(ref Vector3 a, ref Vector3 b)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | a | |
| Vector3 | b |
Returns
| Type | Description |
|---|---|
| float |
ExtractRotationFromMatrix(ref Matrix4x4, out Quaternion)
Extracts a Quaternion rotation from a transformation matrix without allocating memory. Equivalent to matrix.rotation but writes directly to an existing Quaternion reference.
Declaration
public static void ExtractRotationFromMatrix(ref Matrix4x4 matrix, out Quaternion rotation)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix4x4 | matrix | Source transformation matrix |
| Quaternion | rotation | Output quaternion to populate with the extracted rotation |
FastPow(float, float)
Fast power function using exp(y * ln(x)). ~30% faster than Mathf.Pow for typical physics exponents (0.8-2.0).
Declaration
public static float FastPow(float x, float y)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | |
| float | y |
Returns
| Type | Description |
|---|---|
| float |
InvertAffineNoScale(in Matrix4x4, ref Matrix4x4)
Computes the inverse of an affine TRS matrix with scale=1. Faster than general Matrix4x4.inverse since rotation block is orthonormal (inverse = transpose).
Declaration
public static void InvertAffineNoScale(in Matrix4x4 src, ref Matrix4x4 dst)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix4x4 | src | Source matrix (must be TRS with scale=1) |
| Matrix4x4 | dst | Destination matrix for the inverse |
MultiplyMatricesNoAlloc(ref Matrix4x4, ref Matrix4x4, ref Matrix4x4)
Multiplies two matrices and stores the result in a third matrix without allocating memory. Equivalent to result = lhs * rhs but modifies result in place.
Declaration
public static void MultiplyMatricesNoAlloc(ref Matrix4x4 lhs, ref Matrix4x4 rhs, ref Matrix4x4 result)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix4x4 | lhs | Left matrix |
| Matrix4x4 | rhs | Right matrix |
| Matrix4x4 | result | Output matrix to store lhs * rhs |
MultiplyQuaternions(ref Quaternion, ref Quaternion)
Multiplies two quaternions and returns the result. Alternative to Quaternion operator * that may reduce overhead in performance-critical paths.
Declaration
public static Quaternion MultiplyQuaternions(ref Quaternion a, ref Quaternion b)
Parameters
| Type | Name | Description |
|---|---|---|
| Quaternion | a | First quaternion (left side of multiplication) |
| Quaternion | b | Second quaternion (right side of multiplication) |
Returns
| Type | Description |
|---|---|
| Quaternion | Result of a * b |
MultiplyQuaternionsNoAlloc(ref Quaternion, ref Quaternion, ref Quaternion)
Multiplies two quaternions and writes result to existing quaternion (avoids return value copy).
Declaration
public static void MultiplyQuaternionsNoAlloc(ref Quaternion a, ref Quaternion b, ref Quaternion result)
Parameters
| Type | Name | Description |
|---|---|---|
| Quaternion | a | |
| Quaternion | b | |
| Quaternion | result |
RotateVector(ref Quaternion, ref Vector3, out Vector3)
Rotate vector by quaternion inline. Equivalent to q * v.
Declaration
public static void RotateVector(ref Quaternion q, ref Vector3 v, out Vector3 result)
Parameters
| Type | Name | Description |
|---|---|---|
| Quaternion | q | |
| Vector3 | v | |
| Vector3 | result |
Scale(ref Vector3, ref Vector3, out Vector3)
Inline component-wise scale (Vector3.Scale equivalent).
Declaration
public static void Scale(ref Vector3 a, ref Vector3 b, out Vector3 result)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | a | |
| Vector3 | b | |
| Vector3 | result |
Sign(float)
Sign of a value. Returns +1 for zero.
Declaration
public static float Sign(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value | Input value. |
Returns
| Type | Description |
|---|---|
| float | +1 when |
UpdateAngleAxisQuaternion(float, ref Vector3, ref Quaternion)
Updates an existing quaternion with angle-axis values without allocating memory. Equivalent to result = Quaternion.AngleAxis(...) but modifies in place.
Declaration
public static void UpdateAngleAxisQuaternion(float angle, ref Vector3 axis, ref Quaternion result)
Parameters
| Type | Name | Description |
|---|---|---|
| float | angle | Rotation angle in degrees |
| Vector3 | axis | Rotation axis (should be normalized) |
| Quaternion | result | Quaternion to update with the angle-axis rotation |
ValueNoise2D(float, float)
Deterministic 2D value noise in [0, 1]. Bilinear interpolation of hashed lattice values with smoothstep weights. Same input always yields the same output.
Declaration
public static float ValueNoise2D(float x, float y)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | |
| float | y |
Returns
| Type | Description |
|---|---|
| float |