1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-16 18:01:40 +00:00

Make changes requested by sfan5

* Use `float` overload of `std::fabs` to not necessitate promotion.
* Word test descriptions to describe intent instead of method.
This commit is contained in:
Josiah VanderZee 2025-03-29 08:03:05 -05:00
parent 702e7dda90
commit cd5fcd5c6c
No known key found for this signature in database
GPG key ID: C7BB8573A4ABC4B9

View file

@ -11,7 +11,7 @@
#include "irrlichttypes.h" #include "irrlichttypes.h"
const double EPSILON = 0.001; const float EPSILON = 0.001f;
class TestCollision : public TestBase { class TestCollision : public TestBase {
public: public:
@ -78,19 +78,17 @@ TEST_CASE("Test axis aligned collision with unit cube.", "[collision]")
// The following set of tests is for boxes translated in the -X direction // The following set of tests is for boxes translated in the -X direction
// from the static cube, possibly with additional offsets. // from the static cube, possibly with additional offsets.
SECTION("Given a unit cube translated by -2 units on the x-axis, " SECTION("When the box moves towards the target on the X axis, "
"when it moves 1 unit per step in the +x direction for 1 step, " "then they should collide on the X axis.")
"then it should collide on the X axis within epsilon of 1 step.")
{ {
aabb3f m{bx-2.0f, by, bz, bx-1.0f, by+1.0f, bz+1.0f}; aabb3f m{bx-2.0f, by, bz, bx-1.0f, by+1.0f, bz+1.0f};
f32 dtime = 1.0f; f32 dtime = 1.0f;
CHECK(0 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime)); CHECK(0 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 1.0) < EPSILON); CHECK(std::fabs(dtime - 1.0f) < EPSILON);
} }
SECTION("Given a unit cube translated by -2 units on the x-axis, " SECTION("When the box moves away from the target on the X axis, "
"when it moves 1 unit per step in the -x direction for 1 step, " "then they should never collide.")
"then it should never collide.")
{ {
aabb3f m{bx-2.0f, by, bz, bx-1.0f, by+1.0f, bz+1.0f}; aabb3f m{bx-2.0f, by, bz, bx-1.0f, by+1.0f, bz+1.0f};
f32 dtime = 1.0f; f32 dtime = 1.0f;
@ -98,86 +96,76 @@ TEST_CASE("Test axis aligned collision with unit cube.", "[collision]")
} }
SECTION("Given a unit cube translated by -2 units on the x-axis " SECTION("Given the box and the target do not overlap on the X or Y axes, "
"and 1.5 units on the y-axis, " "when the box moves away on the X axis, "
"when it moves 1 unit per step in the +x direction for 1 step, " "then they should never collide."
"then it should never collide.")
{ {
aabb3f m{bx-2.0f, by+1.5f, bz, bx-1.0f, by+2.5f, bz+1.0f}; aabb3f m{bx-2.0f, by+1.5f, bz, bx-1.0f, by+2.5f, bz+1.0f};
f32 dtime = 1.0f; f32 dtime = 1.0f;
CHECK(-1 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime)); CHECK(-1 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime));
} }
SECTION("Given a 0.5x2x1 cube translated by -2 units on the x-axis " SECTION("Given the box and the target do not overlap on the X or Y axes, "
"and -1.5 units on the y-axis, " "when the box moves at the right speeds on the X and Y axes, "
"when it moves 0.5 units per step in the +x direction " "then they should collide on the X axis.")
"and 0.1 units per step in the +y direction for 3 steps, "
"then it should collide on the X axis within epsilon of 3 steps.")
{ {
aabb3f m{bx-2.0f, by-1.5f, bz, bx-1.5f, by+0.5f, bz+1.0f}; aabb3f m{bx-2.0f, by-1.5f, bz, bx-1.5f, by+0.5f, bz+1.0f};
f32 dtime = 3.0f; f32 dtime = 3.0f;
CHECK(0 == axisAlignedCollision(s, m, v3f{0.5f, 0.1f, 0}, &dtime)); CHECK(0 == axisAlignedCollision(s, m, v3f{0.5f, 0.1f, 0}, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 3.0) < EPSILON); CHECK(std::fabs(dtime - 3.0f) < EPSILON);
} }
// The following set of tests is for boxes translated in the +X direction // The following set of tests is for boxes translated in the +X direction
// from the static cube, possibly with additional offsets. They are not // from the static cube, possibly with additional offsets. They are not
// all mirror images of the tests for the -X direction. // all mirror images of the tests for the -X direction.
SECTION("Given a unit cube translated by +2 units on the x-axis, " SECTION("When the box moves towards the target on the X axis, "
"when it moves 1 unit per step in the -x direction for 1 step, " "then they should collide on the X axis.")
"then it should collide on the X axis within epsilon of 1 step.")
{ {
aabb3f m{bx+2.0f, by, bz, bx+3.0f, by+1.0f, bz+1.0f}; aabb3f m{bx+2.0f, by, bz, bx+3.0f, by+1.0f, bz+1.0f};
f32 dtime = 1.0f; f32 dtime = 1.0f;
CHECK(0 == axisAlignedCollision(s, m, v3f{-1.0f, 0.0f, 0.0f}, &dtime)); CHECK(0 == axisAlignedCollision(s, m, v3f{-1.0f, 0.0f, 0.0f}, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 1.0) < EPSILON); CHECK(std::fabs(dtime - 1.0f) < EPSILON);
} }
SECTION("Given a unit cube translated by +2 units on the x-axis, " SECTION("When the box moves away from the target on the X axis, "
"when it moves 1 unit per step in the +x direction for 1 step, " "then they should never collide.")
"then it should never collide.")
{ {
aabb3f m{bx+2.0f, by, bz, bx+3.0f, by+1.0f, bz+1.0f}; aabb3f m{bx+2.0f, by, bz, bx+3.0f, by+1.0f, bz+1.0f};
f32 dtime = 1.0f; f32 dtime = 1.0f;
CHECK(-1 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime)); CHECK(-1 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime));
} }
SECTION("Given a unit cube translated by +2 units on the x-axis " SECTION("Given the box and the target do not overlap on the X or Z axes, "
"and 1.5 units on the z-axis, " "when the box moves away on the X axis, "
"when it moves 1 unit per step in the -x direction for 1 step, " "then they should never collide."
"then it should never collide.")
{ {
aabb3f m{bx+2.0f, by, bz+1.5f, bx+3.0f, by+1.0f, bz+3.5f}; aabb3f m{bx+2.0f, by, bz+1.5f, bx+3.0f, by+1.0f, bz+3.5f};
f32 dtime = 1.0f; f32 dtime = 1.0f;
CHECK(-1 == axisAlignedCollision(s, m, v3f{-1.0f, 0.0f, 0.0f}, &dtime)); CHECK(-1 == axisAlignedCollision(s, m, v3f{-1.0f, 0.0f, 0.0f}, &dtime));
} }
SECTION("Given a 0.5x1x1 cube translated by +2 units on the x-axis " SECTION("Given the box and the target do not overlap on the X or Y axes, "
"and -1.5 units on the y-axis, " "when the box moves at the right speeds on the X and Y axes, "
"when it moves 0.5 units per step in the -x direction " "then they should collide on the Y axis.")
"and 0.2 units per step in the +y direction for 3 steps, "
"then it should collide on the Y axis within epsilon of 2.5 steps.")
{ {
// This test is interesting because the Y-faces are the first to collide. // This test is interesting because the Y-faces are the first to collide.
aabb3f m{bx+2.0f, by-1.5f, bz, bx+2.5f, by-0.5f, bz+1.0f}; aabb3f m{bx+2.0f, by-1.5f, bz, bx+2.5f, by-0.5f, bz+1.0f};
f32 dtime = 2.51f; f32 dtime = 2.51f;
// y velocity is 0.200000003 precisely // y velocity is 0.200000003 precisely
CHECK(1 == axisAlignedCollision(s, m, v3f{-0.5f, 0.2f, 0}, &dtime)); CHECK(1 == axisAlignedCollision(s, m, v3f{-0.5f, 0.2f, 0}, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 2.5) < EPSILON); CHECK(std::fabs(dtime - 2.5f) < EPSILON);
} }
SECTION("Given a 0.5x1x1 cube translated by +2 units on the x-axis " SECTION("Given the box and the target do not overlap on the X or Y axes, "
"and -1.5 units on the y-axis, " "when the box moves at the right speeds on the X and Y axes, "
"when it moves 0.5 units per step in the -x direction " "then they should collide on the X axis.")
"and 0.3 units per step in the +y direction for 3 steps, "
"then it should collide on the X axis within epsilon of 2.0 steps.")
{ {
aabb3f m{bx+2.0f, by-1.5f, bz, bx+2.5f, by-0.5f, bz+1.0f}; aabb3f m{bx+2.0f, by-1.5f, bz, bx+2.5f, by-0.5f, bz+1.0f};
f32 dtime = 2.1f; f32 dtime = 2.1f;
// y velocity is 0.300000012 precisely // y velocity is 0.300000012 precisely
CHECK(0 == axisAlignedCollision(s, m, v3f{-0.5f, 0.3f, 0}, &dtime)); CHECK(0 == axisAlignedCollision(s, m, v3f{-0.5f, 0.3f, 0}, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 2.0) < EPSILON); CHECK(std::fabs(dtime - 2.0f) < EPSILON);
} }
} }
@ -201,7 +189,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]")
v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f}; v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f};
f32 dtime = 1.0f; f32 dtime = 1.0f;
CHECK(0 == axisAlignedCollision(s, m, v, &dtime)); CHECK(0 == axisAlignedCollision(s, m, v, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 0.9) < EPSILON); CHECK(std::fabs(dtime - 0.9f) < EPSILON);
} }
SECTION("Collides on Y axis near (+X,+Y,+Z) corner.") SECTION("Collides on Y axis near (+X,+Y,+Z) corner.")
@ -210,7 +198,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]")
v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f}; v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f};
f32 dtime = 1.0f; f32 dtime = 1.0f;
CHECK(1 == axisAlignedCollision(s, m, v, &dtime)); CHECK(1 == axisAlignedCollision(s, m, v, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 0.9) < EPSILON); CHECK(std::fabs(dtime - 0.9f) < EPSILON);
} }
SECTION("Collides on Z axis near (+X,+Y,+Z) corner.") SECTION("Collides on Z axis near (+X,+Y,+Z) corner.")
@ -219,7 +207,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]")
v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f}; v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f};
f32 dtime = 1.0f; f32 dtime = 1.0f;
CHECK(2 == axisAlignedCollision(s, m, v, &dtime)); CHECK(2 == axisAlignedCollision(s, m, v, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 0.9) < EPSILON); CHECK(std::fabs(dtime - 0.9f) < EPSILON);
} }
SECTION("Collides on X axis near (-X,-Y,-Z) corner.") SECTION("Collides on X axis near (-X,-Y,-Z) corner.")
@ -228,7 +216,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]")
v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f}; v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f};
f32 dtime = 17.0f; f32 dtime = 17.0f;
CHECK(0 == axisAlignedCollision(s, m, v, &dtime)); CHECK(0 == axisAlignedCollision(s, m, v, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 16.1) < EPSILON); CHECK(std::fabs(dtime - 16.1f) < EPSILON);
} }
SECTION("Collides on Y axis near (-X,-Y,-Z) corner.") SECTION("Collides on Y axis near (-X,-Y,-Z) corner.")
@ -237,7 +225,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]")
v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f}; v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f};
f32 dtime = 17.0f; f32 dtime = 17.0f;
CHECK(1 == axisAlignedCollision(s, m, v, &dtime)); CHECK(1 == axisAlignedCollision(s, m, v, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 16.1) < EPSILON); CHECK(std::fabs(dtime - 16.1f) < EPSILON);
} }
SECTION("Collides on Z axis near (-X,-Y,-Z) corner.") SECTION("Collides on Z axis near (-X,-Y,-Z) corner.")
@ -246,7 +234,7 @@ TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]")
v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f}; v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f};
f32 dtime = 17.0f; f32 dtime = 17.0f;
CHECK(2 == axisAlignedCollision(s, m, v, &dtime)); CHECK(2 == axisAlignedCollision(s, m, v, &dtime));
CHECK(std::fabs(static_cast<double>(dtime) - 16.1) < EPSILON); CHECK(std::fabs(dtime - 16.1f) < EPSILON);
} }
} }