rename Intersects to distance

This commit is contained in:
2024-03-22 16:14:56 +01:00
parent c875fa1dee
commit 441131a2f5
4 changed files with 24 additions and 15 deletions

View File

@@ -22,14 +22,8 @@ inline AABB operator+(const AABB& aabb, const Vec3f& offset) {
return result; return result;
} }
/** float Distance(const Ray& ray, const AABB& aabb);
* @brief returns whether the provided ray intersects with the bounding box
*
* @param ray
* @param aabb
* @return true
* @return false
*/
bool Intersects(const Ray& ray, const AABB& aabb); bool Intersects(const Ray& ray, const AABB& aabb);
} // namespace maths } // namespace maths

View File

@@ -1,6 +1,8 @@
#include "blitz/maths/Physics.h" #include "blitz/maths/Physics.h"
#include "blitz/maths/Vector.h" #include "blitz/maths/Vector.h"
#include <cmath>
namespace blitz { namespace blitz {
namespace maths { namespace maths {
@@ -11,7 +13,7 @@ namespace maths {
* or if any coordinate in `ray` is `NAN` or `inf` or if any coordinate in * or if any coordinate in `ray` is `NAN` or `inf` or if any coordinate in
* `aabb` is `NAN` the result is unspecified. * `aabb` is `NAN` the result is unspecified.
* */ * */
bool Intersects(const Ray& ray, const AABB& aabb) { float Distance(const Ray& ray, const AABB& aabb) {
// This function calculates smallest interval I = ] a, b [, for strictly positive a and b // This function calculates smallest interval I = ] a, b [, for strictly positive a and b
// such that for all t in I, for all l, r, o, d corresponding // such that for all t in I, for all l, r, o, d corresponding
@@ -38,12 +40,23 @@ bool Intersects(const Ray& ray, const AABB& aabb) {
l = Min(l, r); l = Min(l, r);
r = Max(l, r); r = Max(l, r);
float tmin = ReduceMax(l);
float tmax = ReduceMin(r);
// Since Min propagates NANs and ReduceMin doesn't, and since NAN !< <any float> // Since Min propagates NANs and ReduceMin doesn't, and since NAN !< <any float>
// the inequality becomes ignored for coordinates where a NAN is involved // the inequality becomes ignored for coordinates where a NAN is involved
// (as a result of 0.0 / 0.0). If all coordinates are NAN, this means // (as a result of 0.0 / 0.0). If all coordinates are NAN, this means
// that the box is reduced to a point and the ray has direction 0, // that the box is reduced to a point and the ray has direction 0,
// in which case this returns false // in which case this returns -1
return fmaxf(ReduceMax(l), 0.0f) < ReduceMin(r); if (tmin <= tmax) {
return std::fmaxf(tmin, 0.0f);
}
return -1.0f;
}
bool Intersects(const Ray& ray, const AABB& aabb) {
return Distance(ray, aabb) >= 0.0f;
} }
} // namespace maths } // namespace maths

View File

@@ -41,7 +41,7 @@ void ServerGame::CheckShoot(game::PlayerID shooter, Vec3f position, float yaw, f
}; };
for (const auto& [playerId, player] : GetPlayers()) { for (const auto& [playerId, player] : GetPlayers()) {
if (playerId != shooter && maths::Intersects(shootRay, playerStaticAABB + player.GetPosition()) > 0.0f) { if (playerId != shooter && maths::Distance(shootRay, playerStaticAABB + player.GetPosition()) > 0.0f) {
utils::LOG("[Server] " + player.GetName() + " a été touché !"); utils::LOG("[Server] " + player.GetName() + " a été touché !");
} }
} }

View File

@@ -10,21 +10,23 @@
using namespace blitz; using namespace blitz;
using namespace maths; using namespace maths;
#define let auto #define let auto // sexy boiiii
static void test_basic() { static void test_basic() {
let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} }; let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {-3.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f} }; let ray = Ray { {-3.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f} };
blitz_test_assert(Intersects(ray, box)); blitz_test_assert(Distance(ray, box));
} }
static void test_shifted() { static void test_shifted() {
let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} }; let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {-3.0f, 0.0f, 100.0f}, {1.0f, 0.0f, 0.0f} }; let ray = Ray { {-3.0f, 0.0f, 100.0f}, {1.0f, 0.0f, 0.0f} };
blitz_test_assert(!Intersects(ray, box)); blitz_test_assert(!Distance(ray, box));
} }
int main(int argc, const char* args[]) { int main(int argc, const char* args[]) {