Skip to content

Commit

Permalink
Unit test fixes / improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kno10 committed Dec 8, 2024
1 parent 7656245 commit 1442a18
Showing 1 changed file with 73 additions and 11 deletions.
84 changes: 73 additions & 11 deletions src/unittest/test_collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void TestCollision::testAxisAlignedCollision()
}
{
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz-1);
aabb3f m(bx-2, by+1.5, bz, bx-1, by+2.5, bz+1);
v3f v(1, 0, 0);
f32 dtime = 1.0f;
UASSERT(axisAlignedCollision(s, m, v, &dtime) == -1);
Expand Down Expand Up @@ -134,16 +134,18 @@ void TestCollision::testAxisAlignedCollision()
{
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
v3f v(-0.5, 0.2, 0);
f32 dtime = 2.5f;
v3f v(-0.5, 0.2, 0); // 0.200000003 precisely
v3f a(0., 0., 0.);
f32 dtime = 2.51f;
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 1); // Y, not X!
UASSERT(fabs(dtime - 2.500) < 0.001);
}
{
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
aabb3f m(bx+2, by-1.5, bz, bx+2.5, by-0.5, bz+1);
v3f v(-0.5, 0.3, 0);
f32 dtime = 2.0f;
v3f v(-0.5, 0.3, 0); // 0.300000012 precisely
v3f a(0., 0., 0.);
f32 dtime = 2.1f;
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 0);
UASSERT(fabs(dtime - 2.000) < 0.001);
}
Expand Down Expand Up @@ -179,7 +181,8 @@ void TestCollision::testAxisAlignedCollision()
aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.3, by-2.29, bz-2.29);
v3f v(1./7, 1./7, 1./7);
f32 dtime = 17.0f;
v3f a(0., 0., 0.);
f32 dtime = 17.1f;
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 0);
UASSERT(fabs(dtime - 16.1) < 0.001);
}
Expand Down Expand Up @@ -224,18 +227,16 @@ void TestCollision::testCollisionMoveSimple(IGameDef *gamedef)
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 1.0f,
&pos, &speed, accel);

UASSERT(!res.touching_ground || !res.collides || !res.standing_on_object);
UASSERT(!res.touching_ground && !res.collides && !res.standing_on_object);
UASSERT(res.collisions.empty());
// FIXME: it's easy to tell that this should be y=1.5f, but our code does it wrong.
// It's unclear if/how this will be fixed.
UASSERTEQ_V3F(pos, fpos(4, 2, 4));
UASSERTEQ_V3F(pos, fpos(4, 1.5, 4));
UASSERTEQ_V3F(speed, fpos(0, 1, 0));

/* standing on ground */
pos = fpos(0, 0.5f, 0);
speed = fpos(0, 0, 0);
accel = fpos(0, -9.81f, 0);
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 0.04f,
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 0.05f,
&pos, &speed, accel);

UASSERT(res.collides);
Expand All @@ -251,6 +252,67 @@ void TestCollision::testCollisionMoveSimple(IGameDef *gamedef)
UASSERTEQ(v3s16, ci.node_p, v3s16(0, 0, 0));
}

/* jumping on ground */
pos = fpos(0, 0.5f, 0);
speed = fpos(0, 2.0f, 0);
accel = fpos(0, -9.81f, 0);
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 0.2f,
&pos, &speed, accel);
UASSERT(!res.collides && !res.touching_ground && !res.standing_on_object);

pos = fpos(0, 0.5f, 0);
speed = fpos(0, 2.0f, 0);
accel = fpos(0, -9.81f, 0);
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 0.5f,
&pos, &speed, accel);

UASSERT(res.collides);
UASSERT(res.touching_ground);
UASSERT(!res.standing_on_object);
UASSERTEQ_V3F(pos, fpos(0, 0.5f, 0));
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
UASSERT(res.collisions.size() == 1);
{
auto &ci = res.collisions.front();
UASSERTEQ(int, ci.type, COLLISION_NODE);
UASSERTEQ(int, ci.axis, COLLISION_AXIS_Y);
UASSERTEQ(v3s16, ci.node_p, v3s16(0, 0, 0));
}

/* moving over ground, no gravity */
pos = fpos(0, 0.5f, 0);
speed = fpos(1.0f, 0.0f, 0);
accel = fpos(0, 0.0f, 0);
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 1.0f,
&pos, &speed, accel);

UASSERT(!res.collides);
UASSERT(res.touching_ground);
UASSERT(!res.standing_on_object);
UASSERTEQ_V3F(pos, fpos(1.0f, 0.5f, 0));
UASSERTEQ_V3F(speed, fpos(1.0f, 0, 0));
UASSERT(res.collisions.empty());

/* moving over ground, with gravity */
pos = fpos(0, 0.5f, 0);
speed = fpos(1.0f, 0.0f, 0);
accel = fpos(0, -9.81f, 0);
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 1.0f,
&pos, &speed, accel);

UASSERT(res.collides);
UASSERT(res.touching_ground);
UASSERT(!res.standing_on_object);
UASSERTEQ_V3F(pos, fpos(1.0f, 0.5f, 0));
UASSERTEQ_V3F(speed, fpos(1.0f, 0, 0));
UASSERT(res.collisions.size() == 1);
{ // first collision on y axis zeros speed and acceleration.
auto &ci = res.collisions.front();
UASSERTEQ(int, ci.type, COLLISION_NODE);
UASSERTEQ(int, ci.axis, COLLISION_AXIS_Y);
UASSERTEQ(v3s16, ci.node_p, v3s16(0, 0, 0));
}

/* not moving never collides */
pos = fpos(0, -100, 0);
speed = fpos(0, 0, 0);
Expand Down

0 comments on commit 1442a18

Please sign in to comment.