Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
MathUtils.h
Go to the documentation of this file.
1
11#pragma once
12#include <glm/glm.hpp>
13#include <vector>
14#include <optional>
16
17struct ObjectSnapshot;
18
27namespace Math {
54 inline std::optional<float> intersectTriangle(
55 const Ray& ray,
56 const glm::vec3& v0,
57 const glm::vec3& v1,
58 const glm::vec3& v2
59 ) {
60 constexpr float EPSILON = std::numeric_limits<float>::epsilon();
61
62 glm::vec3 edge1 = v1 - v0;
63 glm::vec3 edge2 = v2 - v0;
64
65 glm::vec3 pvec = glm::cross(ray.dir, edge2);
66 float det = glm::dot(edge1, pvec);
67
68 // If the determinant is near zero, ray lies in plane of triangle or is backfacing
69 if (det > -EPSILON && det < EPSILON) return std::nullopt;
70
71 float invDet = 1.0f / det;
72
73 glm::vec3 tvec = ray.origin - v0;
74 float u = glm::dot(tvec, pvec) * invDet;
75 if (u < 0.0f || u > 1.0f) return std::nullopt;
76
77 glm::vec3 qvec = glm::cross(tvec, edge1);
78 float v = glm::dot(ray.dir, qvec) * invDet;
79 if (v < 0.0f || u + v > 1.0f) return std::nullopt;
80
81 // compute t to find out where the intersection point is on the line
82 float t = glm::dot(edge2, qvec) * invDet;
83 if (t < EPSILON) return std::nullopt; // intersection is behind the ray origin
84
85 return t;
86 }
87
118 double mouseX,
119 double mouseY,
120 int fbWidth,
121 int fbHeight,
122 const glm::mat4 &view,
123 const glm::mat4 &projection
124 ) {
125 float x = (2.0f * mouseX) / fbWidth - 1.0f;
126 float y = 1.0f - (2.0f * mouseY) / fbHeight;
127
128 glm::vec4 clip = glm::vec4(x, y, -1.0f, 1.0f);
129 glm::mat4 invProj = glm::inverse(projection);
130 glm::vec4 eye = invProj * clip;
131 eye.z = -1.0f;
132 eye.w = 0.0f;
133
134 glm::mat4 invView = glm::inverse(view);
135 glm::vec4 worldDir4 = invView * eye;
136
137 return glm::normalize(glm::vec3(worldDir4));
138 }
139
146 struct HitResult {
148 float distance;
149 };
150
173 inline std::optional<HitResult> findFirstHit(
174 const std::vector<IPickable*>& objects,
175 const Ray& ray,
176 IPickable* priority = nullptr
177 ) {
178 float distance = std::numeric_limits<float>::infinity();
179 IPickable* best = nullptr;
180
181 for (IPickable* obj : objects) {
182 auto t = obj->intersectsRay(ray);
183 if (!t)
184 continue;
185
186 // If this is the priority object, take it immediately
187 if (obj == priority) {
188 distance = *t;
189 return HitResult{obj, distance};
190 }
191
192 if (*t < distance) {
193 distance = *t;
194 best = obj;
195 }
196 }
197
198 return HitResult{best, distance};
199 }
200}
Interface for objects that support mouse-based selection and interaction.
Abstract interface for objects that can be selected and interacted with using the mouse.
Definition IPickable.h:28
virtual std::optional< float > intersectsRay(const Math::Ray &ray) const =0
Tests if a ray intersects this object's geometry.
Mathematical and geometric utility functions.
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
float distance
Distance from the ray origin to the intersection point. Infinity if no hit.
Definition MathUtils.h:148
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
IPickable * object
Pointer to the hit object, or nullptr if no intersection.
Definition MathUtils.h:147
std::optional< HitResult > findFirstHit(const std::vector< IPickable * > &objects, const Ray &ray, IPickable *priority=nullptr)
Finds the first object intersected by a ray.
Definition MathUtils.h:173
Represents the result of a ray-object intersection test.
Definition MathUtils.h:146
Represents a ray in 3D space.
Definition Ray.h:11