Skip to content

Commit 92b45be

Browse files
committed
AABB ray cast
surprise tool to help you later
1 parent f540e4d commit 92b45be

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/include/sndx/collision/rect.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,30 @@ namespace sndx::collision {
196196
auto q = glm::abs(tpoint) - size;
197197
return glm::length(glm::max(q, Precision(0.0))) + glm::min(glm::compMax(q), Precision(0.0));
198198
}
199+
200+
[[nodiscard]]
201+
constexpr bool calcRayIntersection(const Vec& from, const Vec& dir, Precision& nearHit, Precision& farHit) const noexcept {
202+
nearHit = Precision(0.0);
203+
farHit = std::numeric_limits<Precision>::max();
204+
205+
for (typename Vec::length_type i = 0; i < dimensionality(); ++i) {
206+
auto inv = Precision(1.0) / dir[i];
207+
auto near = (getP1()[i] - from[i]) * inv;
208+
auto far = (getP2()[i] - from[i]) * inv;
209+
210+
if (inv < Precision(0.0))
211+
std::swap(near, far);
212+
213+
nearHit = std::max(near, nearHit);
214+
farHit = std::min(far, farHit);
215+
216+
if (farHit < nearHit)
217+
return false;
218+
}
219+
220+
return true;
221+
}
222+
199223
};
200224

201225
template <size_t n, typename InternalT = float, glm::qualifier Qualifier = glm::qualifier::defaultp>

0 commit comments

Comments
 (0)