Math Module¶
The Math module provides a platform-agnostic numerical abstraction layer (Scalar) that automatically selects the most efficient representation (float or Fixed16) based on the target hardware's capabilities (FPU presence).
Scalar¶
Namespace: pixelroot32::math
Scalar is the fundamental numeric type used throughout the engine for physics, positioning, and logic.
- On FPU platforms (ESP32, S3):
Scalaris an alias forfloat. - On non-FPU platforms (C3, S2):
Scalaris an alias forFixed16.
Helper Functions¶
-
Scalar toScalar(float value)Converts a floating-point literal or variable toScalar. Usage:Scalar speed = toScalar(2.5f); -
Scalar toScalar(int value)Converts an integer toScalar. -
int toInt(Scalar value)Converts aScalarback to an integer (truncating decimals). -
float toFloat(Scalar value)Converts aScalartofloat. Warning: Use sparingly on non-FPU platforms. -
Scalar abs(Scalar v)Returns the absolute value. -
Scalar sqrt(Scalar v)Returns the square root. Warning: Expensive operation. Prefer squared distances for comparisons. -
Scalar min(Scalar a, Scalar b)Returns the smaller of two values. -
Scalar max(Scalar a, Scalar b)Returns the larger of two values. -
Scalar clamp(Scalar v, Scalar minVal, Scalar maxVal)Clamps a value between a minimum and maximum. -
Scalar lerp(Scalar a, Scalar b, Scalar t)Linearly interpolates betweenaandbbyt(wheretis 0.0 to 1.0). -
Scalar sin(Scalar x)Returns the sine of the anglex(in radians). -
Scalar cos(Scalar x)Returns the cosine of the anglex(in radians). -
Scalar atan2(Scalar y, Scalar x)Returns the arc tangent of y/x (in radians). -
Scalar sign(Scalar x)Returns the sign of x (-1, 0, or 1). -
bool is_equal_approx(Scalar a, Scalar b)Returns true if a and b are approximately equal. -
bool is_zero_approx(Scalar x)Returns true if x is approximately zero.
Constants¶
-
Scalar kPiValue of PI (3.14159...). -
Scalar kDegToRadMultiplier to convert degrees to radians (PI / 180). -
Scalar kRadToDegMultiplier to convert radians to degrees (180 / PI).
Vector2¶
Namespace: pixelroot32::math
A 2D vector structure composed of two Scalar components.
Members¶
Scalar xScalar y
Methods¶
-
Vector2(Scalar x, Scalar y)Constructor. -
Scalar lengthSquared() constReturns the squared magnitude of the vector. Preferred overlength()for comparisons. -
Scalar length() constReturns the magnitude of the vector. -
Vector2 normalized() constReturns a normalized (unit length) version of the vector. -
Scalar dot(const Vector2& other) constReturns the dot product with another vector. -
Scalar cross(const Vector2& other) constReturns the cross product with another vector (2D analog). -
Scalar angle() constReturns the angle of the vector in radians. -
Scalar angle_to(const Vector2& to) constReturns the angle to another vector in radians. -
Scalar angle_to_point(const Vector2& to) constReturns the angle from this point to another point. -
Vector2 direction_to(const Vector2& to) constReturns the normalized direction vector pointing to the target. -
Scalar distance_to(const Vector2& to) constReturns the distance to another point. -
Scalar distance_squared_to(const Vector2& to) constReturns the squared distance to another point. -
Vector2 limit_length(Scalar max_len) constReturns the vector with its length limited tomax_len. -
Vector2 clamp(Vector2 min, Vector2 max) constReturns the vector clamped between min and max vectors. -
Vector2 lerp(const Vector2& to, Scalar weight) constLinear interpolation between this vector andto. -
Vector2 rotated(Scalar phi) constReturns the vector rotated byphiradians. -
Vector2 move_toward(const Vector2& to, Scalar delta) constMoves the vector towardtoby a maximum ofdeltadistance. -
Vector2 slide(const Vector2& n) constReturns the component of the vector along the sliding plane defined by normaln. -
Vector2 reflect(const Vector2& n) constReturns the vector reflected across the plane defined by normaln. -
Vector2 project(const Vector2& b) constReturns the projection of this vector onto vectorb. -
Vector2 abs() constReturns a new vector with absolute values of components. -
Vector2 sign() constReturns a new vector with sign of components. -
bool is_normalized() constReturns true if the vector is normalized. -
bool is_zero_approx() constReturns true if the vector is approximately zero. -
bool is_equal_approx(const Vector2& other) constReturns true if the vector is approximately equal toother.