Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
esengine::math Namespace Reference

Math constants and utility functions. More...

Functions

float toRadians (float degrees)
 Converts degrees to radians.
float toDegrees (float radians)
 Converts radians to degrees.
glm::vec3 toRadians (const glm::vec3 &degrees)
 Converts a vec3 of degrees to radians.
glm::vec3 toDegrees (const glm::vec3 &radians)
 Converts a vec3 of radians to degrees.
template<typename T>
T lerp (const T &a, const T &b, float t)
 Linear interpolation between two values.
template<typename T>
T clamp (const T &value, const T &min, const T &max)
 Clamps a value between min and max.
bool approxEqual (float a, float b, float epsilon=EPSILON)
 Checks if two floats are approximately equal.
bool approxEqual (const glm::vec3 &a, const glm::vec3 &b, float epsilon=EPSILON)
 Checks if two vec3s are approximately equal.
glm::mat4 ortho (float left, float right, float bottom, float top, float nearPlane, float farPlane)
 Creates an orthographic projection matrix.
glm::mat4 perspective (float fov, float aspect, float nearPlane, float farPlane)
 Creates a perspective projection matrix.
glm::mat4 lookAt (const glm::vec3 &eye, const glm::vec3 &center, const glm::vec3 &up)
 Creates a look-at view matrix.
void decompose (const glm::mat4 &matrix, glm::vec3 &position, glm::vec3 &rotation, glm::vec3 &scale)
 Decomposes a transformation matrix into components (Euler angles).
void decompose (const glm::mat4 &matrix, glm::vec3 &position, glm::quat &rotation, glm::vec3 &scale)
 Decomposes a transformation matrix into components (quaternion).
glm::quat eulerToQuat (const glm::vec3 &euler)
 Converts Euler angles (radians) to quaternion.
glm::vec3 quatToEuler (const glm::quat &quat)
 Converts quaternion to Euler angles (radians).
glm::quat eulerDegreesToQuat (const glm::vec3 &degrees)
 Converts Euler angles (degrees) to quaternion.
glm::vec3 quatToEulerDegrees (const glm::quat &quat)
 Converts quaternion to Euler angles (degrees).
glm::mat4 compose (const glm::vec3 &position, const glm::quat &rotation, const glm::vec3 &scale)
 Constructs a transformation matrix from TRS components.
glm::mat4 compose (const glm::vec3 &position, const glm::vec3 &rotation, const glm::vec3 &scale)
 Constructs a transformation matrix from TRS components (Euler angles).

Variables

constexpr float PI = 3.14159265358979323846f
 Pi constant.
constexpr float TWO_PI = 2.0f * PI
 Two times pi (full circle in radians).
constexpr float HALF_PI = 0.5f * PI
 Half pi (quarter circle in radians).
constexpr float DEG_TO_RAD = PI / 180.0f
 Degrees to radians conversion factor.
constexpr float RAD_TO_DEG = 180.0f / PI
 Radians to degrees conversion factor.
constexpr float EPSILON = 1e-6f
 Small epsilon for floating-point comparisons.

Detailed Description

Math constants and utility functions.

Provides common mathematical constants and helper functions for angles, interpolation, matrix operations, etc.

Function Documentation

◆ approxEqual() [1/2]

bool esengine::math::approxEqual ( const glm::vec3 & a,
const glm::vec3 & b,
float epsilon = EPSILON )
inline

Checks if two vec3s are approximately equal.

Parameters
aFirst vector
bSecond vector
epsilonMaximum component difference threshold
Returns
True if all components are approximately equal

Definition at line 181 of file Math.hpp.

References approxEqual(), and EPSILON.

◆ approxEqual() [2/2]

bool esengine::math::approxEqual ( float a,
float b,
float epsilon = EPSILON )
inline

Checks if two floats are approximately equal.

Parameters
aFirst value
bSecond value
epsilonMaximum difference threshold
Returns
True if |a - b| < epsilon

Definition at line 170 of file Math.hpp.

References EPSILON.

Referenced by approxEqual().

◆ clamp()

template<typename T>
T esengine::math::clamp ( const T & value,
const T & min,
const T & max )
inline

Clamps a value between min and max.

Template Parameters
TValue type
Parameters
valueValue to clamp
minMinimum bound
maxMaximum bound
Returns
Clamped value

Definition at line 155 of file Math.hpp.

◆ compose() [1/2]

glm::mat4 esengine::math::compose ( const glm::vec3 & position,
const glm::quat & rotation,
const glm::vec3 & scale )
inline

Constructs a transformation matrix from TRS components.

Parameters
positionTranslation
rotationRotation as quaternion
scaleScale factors
Returns
The combined transformation matrix (T * R * S)
glm::mat4 model = math::compose(position, rotation, scale);
glm::mat4 compose(const glm::vec3 &position, const glm::quat &rotation, const glm::vec3 &scale)
Constructs a transformation matrix from TRS components.
Definition Math.hpp:395

Definition at line 395 of file Math.hpp.

Referenced by esengine::ecs::copyFlatRenderMatrix().

◆ compose() [2/2]

glm::mat4 esengine::math::compose ( const glm::vec3 & position,
const glm::vec3 & rotation,
const glm::vec3 & scale )
inline

Constructs a transformation matrix from TRS components (Euler angles).

Parameters
positionTranslation
rotationRotation as Euler angles (radians)
scaleScale factors
Returns
The combined transformation matrix (T * R * S)

Definition at line 410 of file Math.hpp.

◆ decompose() [1/2]

void esengine::math::decompose ( const glm::mat4 & matrix,
glm::vec3 & position,
glm::quat & rotation,
glm::vec3 & scale )
inline

Decomposes a transformation matrix into components (quaternion).

Parameters
matrixThe transformation matrix to decompose
positionOutput: extracted position
rotationOutput: extracted rotation as quaternion
scaleOutput: extracted scale factors
glm::vec3 pos, scl;
glm::quat rot;
math::decompose(transform, pos, rot, scl);
void decompose(const glm::mat4 &matrix, glm::vec3 &position, glm::vec3 &rotation, glm::vec3 &scale)
Decomposes a transformation matrix into components (Euler angles).
Definition Math.hpp:248

Definition at line 288 of file Math.hpp.

References EPSILON.

◆ decompose() [2/2]

void esengine::math::decompose ( const glm::mat4 & matrix,
glm::vec3 & position,
glm::vec3 & rotation,
glm::vec3 & scale )
inline

Decomposes a transformation matrix into components (Euler angles).

Parameters
matrixThe transformation matrix to decompose
positionOutput: extracted position
rotationOutput: extracted Euler angles (radians)
scaleOutput: extracted scale factors
glm::vec3 pos, rot, scl;
math::decompose(transform, pos, rot, scl);

Definition at line 248 of file Math.hpp.

References EPSILON.

Referenced by esengine::ecs::Transform::ensureDecomposed().

◆ eulerDegreesToQuat()

glm::quat esengine::math::eulerDegreesToQuat ( const glm::vec3 & degrees)
inline

Converts Euler angles (degrees) to quaternion.

Parameters
degreesEuler angles in degrees (pitch, yaw, roll / X, Y, Z)
Returns
Quaternion representing the rotation

Definition at line 367 of file Math.hpp.

References toRadians().

◆ eulerToQuat()

glm::quat esengine::math::eulerToQuat ( const glm::vec3 & euler)
inline

Converts Euler angles (radians) to quaternion.

Parameters
eulerEuler angles in radians (pitch, yaw, roll / X, Y, Z)
Returns
Quaternion representing the rotation
glm::quat rot = math::eulerToQuat(glm::vec3(0, glm::radians(45.0f), 0));
glm::quat eulerToQuat(const glm::vec3 &euler)
Converts Euler angles (radians) to quaternion.
Definition Math.hpp:345

Definition at line 345 of file Math.hpp.

◆ lerp()

template<typename T>
T esengine::math::lerp ( const T & a,
const T & b,
float t )
inline

Linear interpolation between two values.

Template Parameters
TValue type (float, vec2, vec3, vec4, etc.)
Parameters
aStart value
bEnd value
tInterpolation factor (0 = a, 1 = b)
Returns
Interpolated value
float val = math::lerp(0.0f, 10.0f, 0.5f); // 5.0f
glm::vec3 pos = math::lerp(startPos, endPos, t);
T lerp(const T &a, const T &b, float t)
Linear interpolation between two values.
Definition Math.hpp:142

Definition at line 142 of file Math.hpp.

Referenced by esengine::particle::noise::perlin2(), esengine::particle::sampleColorLut(), and esengine::particle::sampleSizeLut().

◆ lookAt()

glm::mat4 esengine::math::lookAt ( const glm::vec3 & eye,
const glm::vec3 & center,
const glm::vec3 & up )
inline

Creates a look-at view matrix.

Parameters
eyeCamera position
centerPoint to look at
upWorld up vector
Returns
View matrix

Definition at line 228 of file Math.hpp.

◆ ortho()

glm::mat4 esengine::math::ortho ( float left,
float right,
float bottom,
float top,
float nearPlane,
float farPlane )
inline

Creates an orthographic projection matrix.

Parameters
leftLeft boundary
rightRight boundary
bottomBottom boundary
topTop boundary
nearNear plane distance
farFar plane distance
Returns
Orthographic projection matrix

Definition at line 201 of file Math.hpp.

Referenced by esengine::renderer_renderMaterialPreview(), and esengine::renderer_renderMeshPreview().

◆ perspective()

glm::mat4 esengine::math::perspective ( float fov,
float aspect,
float nearPlane,
float farPlane )
inline

Creates a perspective projection matrix.

Parameters
fovField of view in radians
aspectAspect ratio (width / height)
nearNear plane distance
farFar plane distance
Returns
Perspective projection matrix

Definition at line 216 of file Math.hpp.

◆ quatToEuler()

glm::vec3 esengine::math::quatToEuler ( const glm::quat & quat)
inline

Converts quaternion to Euler angles (radians).

Parameters
quatThe quaternion to convert
Returns
Euler angles in radians (pitch, yaw, roll / X, Y, Z)
glm::vec3 euler = math::quatToEuler(rotation);
glm::vec3 quatToEuler(const glm::quat &quat)
Converts quaternion to Euler angles (radians).
Definition Math.hpp:358

Definition at line 358 of file Math.hpp.

◆ quatToEulerDegrees()

glm::vec3 esengine::math::quatToEulerDegrees ( const glm::quat & quat)
inline

Converts quaternion to Euler angles (degrees).

Parameters
quatThe quaternion to convert
Returns
Euler angles in degrees (pitch, yaw, roll / X, Y, Z)

Definition at line 376 of file Math.hpp.

References toDegrees().

◆ toDegrees() [1/2]

glm::vec3 esengine::math::toDegrees ( const glm::vec3 & radians)
inline

Converts a vec3 of radians to degrees.

Parameters
radiansEuler angles in radians
Returns
Euler angles in degrees

Definition at line 120 of file Math.hpp.

References RAD_TO_DEG.

◆ toDegrees() [2/2]

float esengine::math::toDegrees ( float radians)
inline

Converts radians to degrees.

Parameters
radiansAngle in radians
Returns
Angle in degrees

Definition at line 102 of file Math.hpp.

References RAD_TO_DEG.

Referenced by quatToEulerDegrees().

◆ toRadians() [1/2]

glm::vec3 esengine::math::toRadians ( const glm::vec3 & degrees)
inline

Converts a vec3 of degrees to radians.

Parameters
degreesEuler angles in degrees
Returns
Euler angles in radians

Definition at line 111 of file Math.hpp.

References DEG_TO_RAD.

◆ toRadians() [2/2]

float esengine::math::toRadians ( float degrees)
inline

Converts degrees to radians.

Parameters
degreesAngle in degrees
Returns
Angle in radians

Definition at line 93 of file Math.hpp.

References DEG_TO_RAD.

Referenced by eulerDegreesToQuat().

Variable Documentation

◆ DEG_TO_RAD

float esengine::math::DEG_TO_RAD = PI / 180.0f
constexpr

Degrees to radians conversion factor.

Definition at line 76 of file Math.hpp.

Referenced by esengine::particle::sampleShape(), esengine::GpuParticles::submit(), toRadians(), and toRadians().

◆ EPSILON

float esengine::math::EPSILON = 1e-6f
constexpr

Small epsilon for floating-point comparisons.

Definition at line 82 of file Math.hpp.

Referenced by approxEqual(), approxEqual(), decompose(), and decompose().

◆ HALF_PI

float esengine::math::HALF_PI = 0.5f * PI
constexpr

Half pi (quarter circle in radians).

Definition at line 73 of file Math.hpp.

Referenced by esengine::particle::sampleShape().

◆ PI

float esengine::math::PI = 3.14159265358979323846f
constexpr

Pi constant.

Definition at line 67 of file Math.hpp.

Referenced by esengine::particle::sampleShape().

◆ RAD_TO_DEG

float esengine::math::RAD_TO_DEG = 180.0f / PI
constexpr

Radians to degrees conversion factor.

Definition at line 79 of file Math.hpp.

Referenced by toDegrees(), and toDegrees().

◆ TWO_PI

float esengine::math::TWO_PI = 2.0f * PI
constexpr

Two times pi (full circle in radians).

Definition at line 70 of file Math.hpp.

Referenced by esengine::particle::coneDirection(), esengine::particle::noise::grad2(), and esengine::particle::sampleShape().