From 66e436d0f2cf3c33105d8a5bce43bf64d5e72255 Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 25 Jan 2024 11:25:29 +0100 Subject: Mostly functioning world collisions --- src/Math/AABB.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/Math/AABB.cpp') 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); } -- cgit 1.4.1