Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
Math Namespace Reference

Mathematical and geometric utility functions. More...

Data Structures

struct  HitResult
 Represents the result of a ray-object intersection test. More...
 
struct  Ray
 Represents a ray in 3D space. More...
 

Functions

std::optional< float > intersectTriangle (const Ray &ray, const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2)
 Tests ray-triangle intersection using Möller-Trumbore algorithm.
 
glm::vec3 screenToWorldRayDirection (double mouseX, double mouseY, int fbWidth, int fbHeight, const glm::mat4 &view, const glm::mat4 &projection)
 Converts screen coordinates to a world-space ray direction.
 
std::optional< HitResultfindFirstHit (const std::vector< IPickable * > &objects, const Ray &ray, IPickable *priority=nullptr)
 Finds the first object intersected by a ray.
 

Detailed Description

Mathematical and geometric utility functions.

Provides ray intersection tests, coordinate transformations, and interpolation utilities used throughout the rendering and physics systems.


Data Structure Documentation

◆ Math::HitResult

struct Math::HitResult

Represents the result of a ray-object intersection test.

Contains information about which object was hit by a ray and the distance from the ray origin to the intersection point.

Definition at line 146 of file MathUtils.h.

Collaboration diagram for Math::HitResult:
[legend]
Data Fields
float distance Distance from the ray origin to the intersection point. Infinity if no hit.
IPickable * object Pointer to the hit object, or nullptr if no intersection.

◆ Math::Ray

struct Math::Ray

Represents a ray in 3D space.

Used for mouse picking, collision detection, and line-of-sight tests. The ray is defined parametrically as: \(P(t) = \texttt{origin} + t \cdot \texttt{dir}\)

Definition at line 11 of file Ray.h.

Data Fields
vec3 dir Direction vector (should be normalized)
vec3 origin Starting point of the ray in world space.

Function Documentation

◆ findFirstHit()

std::optional< HitResult > Math::findFirstHit ( const std::vector< IPickable * > &  objects,
const Ray ray,
IPickable priority = nullptr 
)
inline

Finds the first object intersected by a ray.

Performs ray intersection tests against all objects in the provided vector and returns the closest hit as a HitResult, which contains both the object and the distance to the intersection. An optional priority object can be provided; if it is intersected by the ray, it will be returned immediately without testing the remaining objects.

Parameters
objectsList of pickable objects to test against.
rayRay to test (origin and direction).
priorityOptional object to prioritize; if hit, it takes precedence.
Returns
std::optional<HitResult>, or std::nullopt if no object is hit.:
Note
If the priority object is hit, the function returns immediately.
Objects are tested in list order; consider spatial acceleration structures for large scenes.
The function is O(n) in the number of objects.
Not thread-safe if the objects list is modified concurrently.
See also
HitResult

Definition at line 173 of file MathUtils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ intersectTriangle()

std::optional< float > Math::intersectTriangle ( const Ray ray,
const glm::vec3 &  v0,
const glm::vec3 &  v1,
const glm::vec3 &  v2 
)
inline

Tests ray-triangle intersection using Möller-Trumbore algorithm.

This is a fast, stable algorithm that computes barycentric coordinates and checks if the intersection point lies within the triangle.

Parameters
rayRay to test (origin + direction)
v0First triangle vertex
v1Second triangle vertex
v2Third triangle vertex
Returns
Distance along ray to intersection, or std::nullopt if no intersection
Note
Intersection point = ray.origin + ray.dir * outT
Ray direction should be normalized for outT to represent actual distance

Example:

Ray ray = {camera.position, rayDir};
if (auto t = intersectTriangle(ray, v0, v1, v2)) {
glm::vec3 hitPoint = ray.origin + ray.dir * (*t);
std::cout << "Hit at distance: " << *t << std::endl;
}
glm::vec3 origin
Starting point of the ray in world space.
Definition Ray.h:12
glm::vec3 dir
Direction vector (should be normalized)
Definition Ray.h:13
std::optional< float > intersectTriangle(const Ray &ray, const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2)
Tests ray-triangle intersection using Möller-Trumbore algorithm.
Definition MathUtils.h:54
Represents a ray in 3D space.
Definition Ray.h:11
See also
https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm

Definition at line 54 of file MathUtils.h.

◆ screenToWorldRayDirection()

glm::vec3 Math::screenToWorldRayDirection ( double  mouseX,
double  mouseY,
int  fbWidth,
int  fbHeight,
const glm::mat4 &  view,
const glm::mat4 &  projection 
)
inline

Converts screen coordinates to a world-space ray direction.

Transforms 2D mouse/screen coordinates into a 3D ray emanating from the camera position. This is essential for mouse picking and interaction.

Parameters
mouseXScreen X coordinate in pixels (0 = left edge)
mouseYScreen Y coordinate in pixels (0 = top edge)
fbWidthFramebuffer width in pixels
fbHeightFramebuffer height in pixels
viewCamera view matrix (world-to-camera transform)
projectionCamera projection matrix (camera-to-clip transform)
Returns
Normalized direction vector in world space
Note
The returned direction is normalized and ready to use with Ray::dir
Mouse Y is typically inverted in screen space (0 = top)

Example usage:

glm::vec3 rayDir = screenToWorldRayDirection(
mouseX, mouseY,
windowWidth, windowHeight,
camera.getViewMatrix(),
camera.getProjMatrix()
);
Ray ray = {camera.position, rayDir};
glm::vec3 screenToWorldRayDirection(double mouseX, double mouseY, int fbWidth, int fbHeight, const glm::mat4 &view, const glm::mat4 &projection)
Converts screen coordinates to a world-space ray direction.
Definition MathUtils.h:117

Definition at line 117 of file MathUtils.h.