Skip to content

Commit a419006

Browse files
committed
cleanup rect raycast
1 parent 92b45be commit a419006

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/include/sndx/collision/rect.hpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ namespace sndx::collision {
153153
return glm::mix(getP1(), getP2(), Precision(0.5));
154154
}
155155

156+
[[nodiscard]]
157+
constexpr Vec getCenter(uint8_t axis) const noexcept {
158+
return glm::mix(getP1()[axis], getP2()[axis], Precision(0.5));
159+
}
160+
161+
[[nodiscard]]
162+
constexpr const Rect<VectorT>& getBounds() const noexcept {
163+
return *this;
164+
}
156165

157166
/* Collision Related Methods */
158167

@@ -197,10 +206,25 @@ namespace sndx::collision {
197206
return glm::length(glm::max(q, Precision(0.0))) + glm::min(glm::compMax(q), Precision(0.0));
198207
}
199208

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();
209+
struct RaycastResult {
210+
const Rect<VectorT>* rect = nullptr;
211+
Precision near = 0.0f, far = std::numeric_limits<Precision>::max();
212+
213+
[[nodiscard]]
214+
constexpr bool hit() const noexcept {
215+
return rect != nullptr;
216+
}
217+
218+
[[nodiscard]]
219+
constexpr float distance() const noexcept {
220+
return near;
221+
}
222+
};
223+
using result_type = RaycastResult;
224+
225+
[[nodiscard]] // cull is ignored
226+
constexpr RaycastResult raycast(const Vec& from, const Vec& dir, bool = false) const noexcept {
227+
RaycastResult out{};
204228

205229
for (typename Vec::length_type i = 0; i < dimensionality(); ++i) {
206230
auto inv = Precision(1.0) / dir[i];
@@ -210,16 +234,16 @@ namespace sndx::collision {
210234
if (inv < Precision(0.0))
211235
std::swap(near, far);
212236

213-
nearHit = std::max(near, nearHit);
214-
farHit = std::min(far, farHit);
237+
out.near = std::max(near, out.near);
238+
out.far = std::min(far, out.far);
215239

216-
if (farHit < nearHit)
217-
return false;
240+
if (out.far < out.near)
241+
return out;
218242
}
219243

220-
return true;
244+
out.rect = this;
245+
return out;
221246
}
222-
223247
};
224248

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

0 commit comments

Comments
 (0)