Botcraft 1.21.4
Loading...
Searching...
No Matches
AABB.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4
5namespace Botcraft
6{
7 AABB::AABB(const Vector3<double>& center_, const Vector3<double>& half_size_)
8 {
9 center = center_;
10 half_size = half_size_;
11 }
12
14 {
15
16 }
17
19 {
20 return center - half_size;
21 }
22
24 {
25 return center + half_size;
26 }
27
29 {
30 return center;
31 }
32
34 {
35 return half_size;
36 }
37
39 {
40 const Vector3<double> min = GetMin();
41 const Vector3<double> max = GetMax();
42 Vector3<double> output;
43 // Loop through each axis
44 // If > than max, then we are on the max face for axis i
45 // If < than min, then we are on the min face for axis i
46 // Else we are somewhere on one of the other faces (or fully inside the AABB)
47 for (size_t i = 0; i < 3; ++i)
48 {
49 output[i] = std::clamp(pos[i], min[i], max[i]);
50 }
51
52 return output;
53 }
54
55 double AABB::GetVolume() const
56 {
57 return 8.0 * half_size.x * half_size.y * half_size.z;
58 }
59
60 bool AABB::Collide(const AABB& b) const
61 {
62 bool x = std::abs(center.x - b.center.x) <= (half_size.x + b.half_size.x);
63 bool y = std::abs(center.y - b.center.y) <= (half_size.y + b.half_size.y);
64 bool z = std::abs(center.z - b.center.z) <= (half_size.z + b.half_size.z);
65
66 return x && y && z;
67 }
68
69 bool AABB::Intersect(const Vector3<double>& origin, const Vector3<double>& direction) const
70 {
71 double tmin = -std::numeric_limits<float>::max();
72 double tmax = std::numeric_limits<float>::max();
73
74 Vector3<double> t1 = center - half_size - origin;
75 Vector3<double> t2 = center + half_size - origin;
76
77 for (int i = 0; i < 3; ++i)
78 {
79 if (direction[i] != 0.0)
80 {
81 double ti1 = t1[i] / direction[i];
82 double ti2 = t2[i] / direction[i];
83
84 tmin = std::max(tmin, std::min(ti1, ti2));
85 tmax = std::min(tmax, std::max(ti1, ti2));
86 }
87 }
88
89 return tmin <= tmax;
90 }
91
92 AABB& AABB::Inflate(const double d)
93 {
94 half_size += d;
95 return *this;
96 }
97
99 {
100 center += t;
101 return *this;
102 }
103
104 bool AABB::operator<(const AABB& other) const
105 {
106 return center < other.center ||
107 (center == other.center && half_size < other.half_size);
108 }
109
110 bool AABB::operator==(const AABB& other) const
111 {
112 return center == other.center && half_size == other.half_size;
113 }
114} // Botcraft
Vector3< double > GetClosestPoint(const Vector3< double > &pos) const
Get the closest point in the AABB from a given point.
Definition AABB.cpp:38
const Vector3< double > & GetHalfSize() const
Definition AABB.cpp:33
bool Intersect(const Vector3< double > &origin, const Vector3< double > &direction) const
Definition AABB.cpp:69
Vector3< double > GetMin() const
Definition AABB.cpp:18
double GetVolume() const
Get the volume of this AABB.
Definition AABB.cpp:55
bool operator==(const AABB &other) const
Definition AABB.cpp:110
AABB & Translate(const Vector3< double > &t)
Definition AABB.cpp:98
bool operator<(const AABB &other) const
Definition AABB.cpp:104
const Vector3< double > & GetCenter() const
Definition AABB.cpp:28
bool Collide(const AABB &b) const
Definition AABB.cpp:60
Vector3< double > half_size
Definition AABB.hpp:48
Vector3< double > center
Definition AABB.hpp:47
AABB & Inflate(const double d)
Definition AABB.cpp:92
Vector3< double > GetMax() const
Definition AABB.cpp:23
AABB(const Vector3< double > &center_, const Vector3< double > &half_size_)
Definition AABB.cpp:7