From dd66b97a3743a2bdec78f366f2d18e30167ce63e Mon Sep 17 00:00:00 2001 From: CobaltXII Date: Wed, 2 Jan 2019 18:38:23 -0500 Subject: [PATCH] Implemented void do_collision_detection_and_response --- src/inc/hitbox.hpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/inc/hitbox.hpp b/src/inc/hitbox.hpp index 7be7d14..f7fea19 100644 --- a/src/inc/hitbox.hpp +++ b/src/inc/hitbox.hpp @@ -1,3 +1,5 @@ +#include + // An axis-aligned bounding box. struct hitbox @@ -83,4 +85,55 @@ inline float hitbox_z_depth(hitbox a, hitbox b, float eps = 0.00128f) { return b.z - (a.z + a.zr) - eps; } +} + +// Do collision detection and response on one dynamic hitbox with an array of +// several static hitboxes while applying velocity to the dynamic hitbox. + +void do_collision_detection_and_response(hitbox& object, std::vector obstacles, float& vx, float& vy, float& vz, int precision = 64) +{ + for (int p = 0; p < precision; p++) + { + object.x += vx / precision; + + for (int i = 0; i < obstacles.size(); i++) + { + hitbox obstacle = obstacles[i]; + + if (hitbox_intersect(object, obstacle)) + { + object.x += hitbox_x_depth(object, obstacle); + + vx = 0.0f; + } + } + + object.y += vy / precision; + + for (int i = 0; i < obstacles.size(); i++) + { + hitbox obstacle = obstacles[i]; + + if (hitbox_intersect(object, obstacle)) + { + object.y += hitbox_y_depth(object, obstacle); + + vy = 0.0f; + } + } + + object.z += vz / precision; + + for (int i = 0; i < obstacles.size(); i++) + { + hitbox obstacle = obstacles[i]; + + if (hitbox_intersect(object, obstacle)) + { + object.z += hitbox_z_depth(object, obstacle); + + vz = 0.0f; + } + } + } } \ No newline at end of file