diff options
| author | Mel <einebeere@gmail.com> | 2023-11-21 02:28:00 +0100 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2023-11-21 02:28:00 +0100 |
| commit | ca8b16620ec207f2b32edd1f5d46f7b0bfb0a14c (patch) | |
| tree | 02746322c4229431e36b892f11f07a81ce21e439 /src/Math/AABB.hpp | |
| parent | 272e6a63df7369e5afcb16c5a6c14f7dd6a75893 (diff) | |
| download | meowcraft-ca8b16620ec207f2b32edd1f5d46f7b0bfb0a14c.tar.zst meowcraft-ca8b16620ec207f2b32edd1f5d46f7b0bfb0a14c.zip | |
Clumsy non-swept AABB collision system for Player
Diffstat (limited to 'src/Math/AABB.hpp')
| -rw-r--r-- | src/Math/AABB.hpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/Math/AABB.hpp b/src/Math/AABB.hpp index 11d26a8..19aa81c 100644 --- a/src/Math/AABB.hpp +++ b/src/Math/AABB.hpp @@ -8,6 +8,31 @@ struct AABB { AABB(Vector<3> min, Vector<3> max) : min(min), max(max) {} explicit AABB(Vector<3> max) : max(max) {} + Vector<3> size() const { return max - min; } + Vector<3> center() const { return (min + max) / 2; } + + Bool contains(Vector<3> point) const { + return point.x() >= min.x() && point.x() <= max.x() + && point.y() >= min.y() && point.y() <= max.y() + && point.z() >= min.z() && point.z() <= max.z(); + } + + Bool intersects_on_x(AABB other) const { + return min.x() <= other.max.x() && max.x() >= other.min.x(); + } + + Bool intersects_on_y(AABB other) const { + return min.y() <= other.max.y() && max.y() >= other.min.y(); + } + + Bool intersects_on_z(AABB other) const { + return min.z() <= other.max.z() && max.z() >= other.min.z(); + } + + Bool collides(AABB other) const { + return intersects_on_x(other) && intersects_on_y(other) && intersects_on_z(other); + } + std::array<Vector<3>, 8> corners() const { return {{ {min.x(), min.y(), min.z()}, @@ -23,5 +48,12 @@ struct AABB { AABB offset(Vector<3> by) const { return {min + by, max + by}; } + AABB sum(AABB with) const { + auto new_size = size() + with.size(); + return {center() - new_size / 2, center() + new_size / 2}; + } + + AABB cast_box(Vector<3> v, AABB against) const; + Vector<3> min, max; }; |
