Skip to content

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): Scalar is an alias for float.
  • On non-FPU platforms (C3, S2): Scalar is an alias for Fixed16.

Helper Functions

  • Scalar toScalar(float value) Converts a floating-point literal or variable to Scalar. Usage: Scalar speed = toScalar(2.5f);

  • Scalar toScalar(int value) Converts an integer to Scalar.

  • int toInt(Scalar value) Converts a Scalar back to an integer (truncating decimals).

  • float toFloat(Scalar value) Converts a Scalar to float. 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 between a and b by t (where t is 0.0 to 1.0).

  • Scalar sin(Scalar x) Returns the sine of the angle x (in radians).

  • Scalar cos(Scalar x) Returns the cosine of the angle x (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 kPi Value of PI (3.14159...).

  • Scalar kDegToRad Multiplier to convert degrees to radians (PI / 180).

  • Scalar kRadToDeg Multiplier to convert radians to degrees (180 / PI).

Vector2

Namespace: pixelroot32::math

A 2D vector structure composed of two Scalar components.

Members

  • Scalar x
  • Scalar y

Methods

  • Vector2(Scalar x, Scalar y) Constructor.

  • Scalar lengthSquared() const Returns the squared magnitude of the vector. Preferred over length() for comparisons.

  • Scalar length() const Returns the magnitude of the vector.

  • Vector2 normalized() const Returns a normalized (unit length) version of the vector.

  • Scalar dot(const Vector2& other) const Returns the dot product with another vector.

  • Scalar cross(const Vector2& other) const Returns the cross product with another vector (2D analog).

  • Scalar angle() const Returns the angle of the vector in radians.

  • Scalar angle_to(const Vector2& to) const Returns the angle to another vector in radians.

  • Scalar angle_to_point(const Vector2& to) const Returns the angle from this point to another point.

  • Vector2 direction_to(const Vector2& to) const Returns the normalized direction vector pointing to the target.

  • Scalar distance_to(const Vector2& to) const Returns the distance to another point.

  • Scalar distance_squared_to(const Vector2& to) const Returns the squared distance to another point.

  • Vector2 limit_length(Scalar max_len) const Returns the vector with its length limited to max_len.

  • Vector2 clamp(Vector2 min, Vector2 max) const Returns the vector clamped between min and max vectors.

  • Vector2 lerp(const Vector2& to, Scalar weight) const Linear interpolation between this vector and to.

  • Vector2 rotated(Scalar phi) const Returns the vector rotated by phi radians.

  • Vector2 move_toward(const Vector2& to, Scalar delta) const Moves the vector toward to by a maximum of delta distance.

  • Vector2 slide(const Vector2& n) const Returns the component of the vector along the sliding plane defined by normal n.

  • Vector2 reflect(const Vector2& n) const Returns the vector reflected across the plane defined by normal n.

  • Vector2 project(const Vector2& b) const Returns the projection of this vector onto vector b.

  • Vector2 abs() const Returns a new vector with absolute values of components.

  • Vector2 sign() const Returns a new vector with sign of components.

  • bool is_normalized() const Returns true if the vector is normalized.

  • bool is_zero_approx() const Returns true if the vector is approximately zero.

  • bool is_equal_approx(const Vector2& other) const Returns true if the vector is approximately equal to other.