NWH Wheel Controller 3D
Search Results for

    Show / Hide Table of Contents

    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.

    Inheritance
    object
    MathUtility
    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 Source

    Abs(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 value.

    | Edit this page View Source

    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)

    | Edit this page View Source

    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

    | Edit this page View Source

    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

    value clamped to [0, 1].

    | Edit this page View Source

    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

    | Edit this page View Source

    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

    | Edit this page View Source

    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
    | Edit this page View Source

    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
    | Edit this page View Source

    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
    | Edit this page View Source

    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

    | Edit this page View Source

    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
    | Edit this page View Source

    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

    | Edit this page View Source

    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

    | Edit this page View Source

    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

    | Edit this page View Source

    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
    | Edit this page View Source

    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
    | Edit this page View Source

    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
    | Edit this page View Source

    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 value is zero or positive, -1 when negative.

    | Edit this page View Source

    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

    | Edit this page View Source

    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
    • Edit this page
    • View Source
    In this article
    Back to top Copyright © NWH - Vehicle Physics, Aerodynamics, Dynamic Water Physics