summary refs log tree commit diff
path: root/src/Math/AABB.cpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2024-01-25 11:25:29 +0100
committerMel <einebeere@gmail.com>2024-01-25 11:25:29 +0100
commit66e436d0f2cf3c33105d8a5bce43bf64d5e72255 (patch)
tree3ee36001907453336cf96a57d8ec0154a9ae3135 /src/Math/AABB.cpp
parentefd17623627607a26f33dac8f7ef1a1ddc931907 (diff)
downloadmeowcraft-66e436d0f2cf3c33105d8a5bce43bf64d5e72255.tar.zst
meowcraft-66e436d0f2cf3c33105d8a5bce43bf64d5e72255.zip
Mostly functioning world collisions
Diffstat (limited to 'src/Math/AABB.cpp')
-rw-r--r--src/Math/AABB.cpp10
1 files changed, 5 insertions, 5 deletions
diff --git a/src/Math/AABB.cpp b/src/Math/AABB.cpp
index ea48f71..08c9903 100644
--- a/src/Math/AABB.cpp
+++ b/src/Math/AABB.cpp
@@ -3,10 +3,10 @@
 #include "AABB.hpp"
 #include "Ray.hpp"
 
-// Returns new AABB after colliding with `against`.
+// Returns a pushout vector for avoiding collision with `against`.
 // 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).
-AABB AABB::collision_response(Vector<3> v, AABB against) const {
+Vec3 AABB::pushout(Vector<3> v, AABB against) const {
     auto origin = center();
     Ray ray{origin, v};
 
@@ -14,7 +14,7 @@ AABB AABB::collision_response(Vector<3> v, AABB against) const {
 
     auto expanded_target = against.sum(*this);
     auto raycast = ray.cast(expanded_target, v_magnitude);
-    if (!raycast.hit) return offset(v);
+    if (!raycast.hit) return {};
 
     // Slide along the collision plane.
 
@@ -25,6 +25,6 @@ AABB AABB::collision_response(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 result = raycast.point + projected_velocity;
-    return from_center(result, size());
+    auto resulting_point = raycast.point + projected_velocity;
+    return resulting_point - (v + origin);
 }