summary refs log tree commit diff
path: root/src/Math/AABB.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Math/AABB.cpp')
-rw-r--r--src/Math/AABB.cpp13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/Math/AABB.cpp b/src/Math/AABB.cpp
index 08c9903..b16960e 100644
--- a/src/Math/AABB.cpp
+++ b/src/Math/AABB.cpp
@@ -3,10 +3,11 @@
 #include "AABB.hpp"
 #include "Ray.hpp"
 
-// Returns a pushout vector for avoiding collision with `against`.
+// Returns a velocity vectors that avoid collision with `against`, while sliding along it.
+// If no collision is detected, returns the original velocity.
 // Algorithm is kind of based on "https://www.gamedev.net/tutorials/_/technical/game-programming/swept-aabb-collision-detection-and-response-r3084/",
 // but very different (and mine's better :3).
-Vec3 AABB::pushout(Vector<3> v, AABB against) const {
+AABB::CollisionResponse AABB::collision_response(Vector<3> v, AABB against) const {
     auto origin = center();
     Ray ray{origin, v};
 
@@ -14,7 +15,7 @@ Vec3 AABB::pushout(Vector<3> v, AABB against) const {
 
     auto expanded_target = against.sum(*this);
     auto raycast = ray.cast(expanded_target, v_magnitude);
-    if (!raycast.hit) return {};
+    if (!raycast.hit) return {v};
 
     // Slide along the collision plane.
 
@@ -25,6 +26,8 @@ Vec3 AABB::pushout(Vector<3> v, AABB against) const {
     // Project the remaining velocity onto the plane, to which the normal is perpendicular.
     auto projected_velocity = v_remaining - v_remaining.project_onto(raycast.normal);
 
-    auto resulting_point = raycast.point + projected_velocity;
-    return resulting_point - (v + origin);
+    return {
+        .v_to_collision = raycast.point - origin,
+        .v_slide = projected_velocity,
+    };
 }