mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
Merge 30a4bf4168
into aba2b6638e
This commit is contained in:
commit
019c419dc3
4 changed files with 381 additions and 324 deletions
|
@ -70,10 +70,19 @@ bool collision_check_intersection(Environment *env, IGameDef *gamedef,
|
|||
const aabb3f &box_0, const v3f &pos_f, ActiveObject *self = nullptr,
|
||||
bool collide_with_objects = true);
|
||||
|
||||
// Helper function:
|
||||
// Checks for collision of a moving aabbox with a static aabbox
|
||||
// Returns -1 if no collision, 0 if X collision, 1 if Y collision, 2 if Z collision
|
||||
// dtime receives time until first collision, invalid if -1 is returned
|
||||
/**
|
||||
* Helper function to check for collision of a moving aabbox with a static one.
|
||||
*
|
||||
* @param staticbox A stationary hitbox.
|
||||
* @param movingbox A moving hitbox.
|
||||
* @param speed The velocity of the moving hitbox.
|
||||
* @param dtime Only collisions that occur within @dtime will be
|
||||
* searched.
|
||||
*
|
||||
* @return Returns -1 if no collision, 0 if X collision, 1 if Y collision,
|
||||
* 2 if Z collision. Sets @a dtime to the time until the first collision,
|
||||
* or to -1 if no collision occurs.
|
||||
*/
|
||||
CollisionAxis axisAlignedCollision(
|
||||
const aabb3f &staticbox, const aabb3f &movingbox,
|
||||
v3f speed, f32 *dtime);
|
||||
|
|
|
@ -32,25 +32,6 @@ content_t t_CONTENT_BRICK;
|
|||
//// TestGameDef
|
||||
////
|
||||
|
||||
class TestGameDef : public DummyGameDef {
|
||||
public:
|
||||
TestGameDef();
|
||||
~TestGameDef() = default;
|
||||
|
||||
void defineSomeNodes();
|
||||
|
||||
bool joinModChannel(const std::string &channel);
|
||||
bool leaveModChannel(const std::string &channel);
|
||||
bool sendModChannelMessage(const std::string &channel, const std::string &message);
|
||||
ModChannel *getModChannel(const std::string &channel)
|
||||
{
|
||||
return m_modchannel_mgr->getModChannel(channel);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
|
||||
};
|
||||
|
||||
|
||||
TestGameDef::TestGameDef() :
|
||||
DummyGameDef(),
|
||||
|
|
|
@ -4,15 +4,21 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include "dummygamedef.h"
|
||||
#include "irrlichttypes_bloated.h"
|
||||
#include "porting.h"
|
||||
#include "filesys.h"
|
||||
#include "mapnode.h"
|
||||
#include "modchannels.h"
|
||||
#include "porting.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
class TestFailedException { // don’t derive from std::exception to avoid accidental catch
|
||||
public:
|
||||
|
@ -105,6 +111,25 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
class TestGameDef : public DummyGameDef {
|
||||
public:
|
||||
TestGameDef();
|
||||
~TestGameDef() = default;
|
||||
|
||||
void defineSomeNodes();
|
||||
|
||||
bool joinModChannel(const std::string &channel);
|
||||
bool leaveModChannel(const std::string &channel);
|
||||
bool sendModChannelMessage(const std::string &channel, const std::string &message);
|
||||
ModChannel *getModChannel(const std::string &channel)
|
||||
{
|
||||
return m_modchannel_mgr->getModChannel(channel);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
|
||||
};
|
||||
|
||||
// A few item and node definitions for those tests that need them
|
||||
extern content_t t_CONTENT_STONE;
|
||||
extern content_t t_CONTENT_GRASS;
|
||||
|
|
|
@ -3,30 +3,14 @@
|
|||
// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
||||
|
||||
#include "test.h"
|
||||
#include "catch.h"
|
||||
#include "collision.h"
|
||||
#include "dummymap.h"
|
||||
#include "environment.h"
|
||||
#include "irrlicht_changes/printing.h"
|
||||
#include "irrlichttypes.h"
|
||||
|
||||
#include "collision.h"
|
||||
|
||||
class TestCollision : public TestBase {
|
||||
public:
|
||||
TestCollision() { TestManager::registerTestModule(this); }
|
||||
const char *getName() { return "TestCollision"; }
|
||||
|
||||
void runTests(IGameDef *gamedef);
|
||||
|
||||
void testAxisAlignedCollision();
|
||||
void testCollisionMoveSimple(IGameDef *gamedef);
|
||||
};
|
||||
|
||||
static TestCollision g_test_instance;
|
||||
|
||||
void TestCollision::runTests(IGameDef *gamedef)
|
||||
{
|
||||
TEST(testAxisAlignedCollision);
|
||||
TEST(testCollisionMoveSimple, gamedef);
|
||||
}
|
||||
const float EPSILON = 0.001f;
|
||||
|
||||
namespace {
|
||||
class TestEnvironment : public Environment {
|
||||
|
@ -51,7 +35,7 @@ namespace {
|
|||
#define UASSERTEQ_F(actual, expected) do { \
|
||||
f32 a = (actual); \
|
||||
f32 e = (expected); \
|
||||
UTEST(fabsf(a - e) <= 0.0001f, "actual: %.5f expected: %.5f", a, e) \
|
||||
CHECK(fabsf(a - e) <= 0.0001f); \
|
||||
} while (0)
|
||||
|
||||
#define UASSERTEQ_V3F(actual, expected) do { \
|
||||
|
@ -60,154 +44,188 @@ namespace {
|
|||
UASSERTEQ_F(va.X, ve.X); UASSERTEQ_F(va.Y, ve.Y); UASSERTEQ_F(va.Z, ve.Z); \
|
||||
} while (0)
|
||||
|
||||
#define fpos(x,y,z) (BS * v3f(x, y, z))
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void TestCollision::testAxisAlignedCollision()
|
||||
TEST_CASE("Test axis aligned collision with unit cube.", "[collision]")
|
||||
{
|
||||
for (s16 bx = -3; bx <= 3; bx++)
|
||||
for (s16 by = -3; by <= 3; by++)
|
||||
for (s16 bz = -3; bz <= 3; bz++) {
|
||||
// X-
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
|
||||
aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
|
||||
v3f v(1, 0, 0);
|
||||
f32 dtime = 1.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 0);
|
||||
UASSERT(fabs(dtime - 1.000) < 0.001);
|
||||
}
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
|
||||
aabb3f m(bx-2, by, bz, bx-1, by+1, bz+1);
|
||||
v3f v(-1, 0, 0);
|
||||
f32 dtime = 1.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == -1);
|
||||
}
|
||||
{
|
||||
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);
|
||||
v3f v(1, 0, 0);
|
||||
f32 dtime = 1.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == -1);
|
||||
}
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
|
||||
aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
|
||||
v3f v(0.5, 0.1, 0);
|
||||
f32 dtime = 3.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 0);
|
||||
UASSERT(fabs(dtime - 3.000) < 0.001);
|
||||
}
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
|
||||
aabb3f m(bx-2, by-1.5, bz, bx-1.5, by+0.5, bz+1);
|
||||
v3f v(0.5, 0.1, 0);
|
||||
f32 dtime = 3.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 0);
|
||||
UASSERT(fabs(dtime - 3.000) < 0.001);
|
||||
}
|
||||
f32 bx = GENERATE(-3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f);
|
||||
f32 by = GENERATE(-3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f);
|
||||
f32 bz = GENERATE(-3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f);
|
||||
|
||||
// X+
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
|
||||
aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
|
||||
v3f v(-1, 0, 0);
|
||||
f32 dtime = 1.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 0);
|
||||
UASSERT(fabs(dtime - 1.000) < 0.001);
|
||||
}
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
|
||||
aabb3f m(bx+2, by, bz, bx+3, by+1, bz+1);
|
||||
v3f v(1, 0, 0);
|
||||
f32 dtime = 1.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == -1);
|
||||
}
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+1, by+1, bz+1);
|
||||
aabb3f m(bx+2, by, bz+1.5, bx+3, by+1, bz+3.5);
|
||||
v3f v(-1, 0, 0);
|
||||
f32 dtime = 1.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == -1);
|
||||
}
|
||||
{
|
||||
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); // 0.200000003 precisely
|
||||
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); // 0.300000012 precisely
|
||||
f32 dtime = 2.1f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 0);
|
||||
UASSERT(fabs(dtime - 2.000) < 0.001);
|
||||
}
|
||||
INFO("Testing with static cube at (" << bx << ", " << by << ", " << bz << ").");
|
||||
|
||||
// TODO: Y-, Y+, Z-, Z+
|
||||
aabb3f s{bx, by, bz, bx+1.0f, by+1.0f, bz+1.0f};
|
||||
|
||||
// misc
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
|
||||
aabb3f m(bx+2.3, by+2.29, bz+2.29, bx+4.2, by+4.2, bz+4.2);
|
||||
v3f v(-1./3, -1./3, -1./3);
|
||||
f32 dtime = 1.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 0);
|
||||
UASSERT(fabs(dtime - 0.9) < 0.001);
|
||||
}
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
|
||||
aabb3f m(bx+2.29, by+2.3, bz+2.29, bx+4.2, by+4.2, bz+4.2);
|
||||
v3f v(-1./3, -1./3, -1./3);
|
||||
f32 dtime = 1.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 1);
|
||||
UASSERT(fabs(dtime - 0.9) < 0.001);
|
||||
}
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
|
||||
aabb3f m(bx+2.29, by+2.29, bz+2.3, bx+4.2, by+4.2, bz+4.2);
|
||||
v3f v(-1./3, -1./3, -1./3);
|
||||
f32 dtime = 1.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 2);
|
||||
UASSERT(fabs(dtime - 0.9) < 0.001);
|
||||
}
|
||||
{
|
||||
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.1f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 0);
|
||||
UASSERT(fabs(dtime - 16.1) < 0.001);
|
||||
}
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
|
||||
aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.3, bz-2.29);
|
||||
v3f v(1./7, 1./7, 1./7);
|
||||
f32 dtime = 17.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 1);
|
||||
UASSERT(fabs(dtime - 16.1) < 0.001);
|
||||
}
|
||||
{
|
||||
aabb3f s(bx, by, bz, bx+2, by+2, bz+2);
|
||||
aabb3f m(bx-4.2, by-4.2, bz-4.2, bx-2.29, by-2.29, bz-2.3);
|
||||
v3f v(1./7, 1./7, 1./7);
|
||||
f32 dtime = 17.0f;
|
||||
UASSERT(axisAlignedCollision(s, m, v, &dtime) == 2);
|
||||
UASSERT(fabs(dtime - 16.1) < 0.001);
|
||||
}
|
||||
// The following set of tests is for boxes translated in the -X direction
|
||||
// from the static cube, possibly with additional offsets.
|
||||
SECTION("When the box moves towards the target on the X axis, "
|
||||
"then they should collide on the X axis.")
|
||||
{
|
||||
aabb3f m{bx-2.0f, by, bz, bx-1.0f, by+1.0f, bz+1.0f};
|
||||
f32 dtime = 1.0f;
|
||||
CHECK(0 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime));
|
||||
CHECK(std::fabs(dtime - 1.0f) < EPSILON);
|
||||
}
|
||||
|
||||
SECTION("When the box moves away from the target on the X axis, "
|
||||
"then they should never collide.")
|
||||
{
|
||||
aabb3f m{bx-2.0f, by, bz, bx-1.0f, by+1.0f, bz+1.0f};
|
||||
f32 dtime = 1.0f;
|
||||
CHECK(-1 == axisAlignedCollision(s, m, v3f{-1.0f, 0.0f, 0.0f}, &dtime));
|
||||
}
|
||||
|
||||
|
||||
SECTION("Given the box and the target do not overlap on the X or Y axes, "
|
||||
"when the box moves away on the X axis, "
|
||||
"then they should never collide.")
|
||||
{
|
||||
aabb3f m{bx-2.0f, by+1.5f, bz, bx-1.0f, by+2.5f, bz+1.0f};
|
||||
f32 dtime = 1.0f;
|
||||
CHECK(-1 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime));
|
||||
}
|
||||
|
||||
SECTION("Given the box and the target do not overlap on the X or Y axes, "
|
||||
"when the box moves at the right speeds on the X and Y axes, "
|
||||
"then they should collide on the X axis.")
|
||||
{
|
||||
aabb3f m{bx-2.0f, by-1.5f, bz, bx-1.5f, by+0.5f, bz+1.0f};
|
||||
f32 dtime = 3.0f;
|
||||
CHECK(0 == axisAlignedCollision(s, m, v3f{0.5f, 0.1f, 0}, &dtime));
|
||||
CHECK(std::fabs(dtime - 3.0f) < EPSILON);
|
||||
}
|
||||
|
||||
// The following set of tests is for boxes translated in the +X direction
|
||||
// from the static cube, possibly with additional offsets. They are not
|
||||
// all mirror images of the tests for the -X direction.
|
||||
|
||||
SECTION("When the box moves towards the target on the X axis, "
|
||||
"then they should collide on the X axis.")
|
||||
{
|
||||
aabb3f m{bx+2.0f, by, bz, bx+3.0f, by+1.0f, bz+1.0f};
|
||||
f32 dtime = 1.0f;
|
||||
CHECK(0 == axisAlignedCollision(s, m, v3f{-1.0f, 0.0f, 0.0f}, &dtime));
|
||||
CHECK(std::fabs(dtime - 1.0f) < EPSILON);
|
||||
}
|
||||
|
||||
SECTION("When the box moves away from the target on the X axis, "
|
||||
"then they should never collide.")
|
||||
{
|
||||
aabb3f m{bx+2.0f, by, bz, bx+3.0f, by+1.0f, bz+1.0f};
|
||||
f32 dtime = 1.0f;
|
||||
CHECK(-1 == axisAlignedCollision(s, m, v3f{1.0f, 0.0f, 0.0f}, &dtime));
|
||||
}
|
||||
|
||||
SECTION("Given the box and the target do not overlap on the X or Z axes, "
|
||||
"when the box moves away on the X axis, "
|
||||
"then they should never collide.")
|
||||
{
|
||||
aabb3f m{bx+2.0f, by, bz+1.5f, bx+3.0f, by+1.0f, bz+3.5f};
|
||||
f32 dtime = 1.0f;
|
||||
CHECK(-1 == axisAlignedCollision(s, m, v3f{-1.0f, 0.0f, 0.0f}, &dtime));
|
||||
}
|
||||
|
||||
SECTION("Given the box and the target do not overlap on the X or Y axes, "
|
||||
"when the box moves at the right speeds on the X and Y axes, "
|
||||
"then they should collide on the Y axis.")
|
||||
{
|
||||
// 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};
|
||||
f32 dtime = 2.51f;
|
||||
// y velocity is 0.200000003 precisely
|
||||
CHECK(1 == axisAlignedCollision(s, m, v3f{-0.5f, 0.2f, 0}, &dtime));
|
||||
CHECK(std::fabs(dtime - 2.5f) < EPSILON);
|
||||
}
|
||||
|
||||
SECTION("Given the box and the target do not overlap on the X or Y axes, "
|
||||
"when the box moves at the right speeds on the X and Y axes, "
|
||||
"then they should collide on the X axis.")
|
||||
{
|
||||
aabb3f m{bx+2.0f, by-1.5f, bz, bx+2.5f, by-0.5f, bz+1.0f};
|
||||
f32 dtime = 2.1f;
|
||||
// y velocity is 0.300000012 precisely
|
||||
CHECK(0 == axisAlignedCollision(s, m, v3f{-0.5f, 0.3f, 0}, &dtime));
|
||||
CHECK(std::fabs(dtime - 2.0f) < EPSILON);
|
||||
}
|
||||
}
|
||||
|
||||
#define fpos(x,y,z) (BS * v3f(x, y, z))
|
||||
|
||||
void TestCollision::testCollisionMoveSimple(IGameDef *gamedef)
|
||||
TEST_CASE("Test axis aligned collision with 2x2x2 cube.", "[collision]")
|
||||
{
|
||||
auto env = std::make_unique<TestEnvironment>(gamedef);
|
||||
g_collision_problems_encountered = false;
|
||||
f32 bx = GENERATE(-3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f);
|
||||
f32 by = GENERATE(-3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f);
|
||||
f32 bz = GENERATE(-3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f);
|
||||
|
||||
INFO("Testing with static cube at (" << bx << ", " << by << ", " << bz << ").");
|
||||
|
||||
aabb3f s{bx, by, bz, bx+2.0f, by+2.0f, bz+2.0f};
|
||||
|
||||
// The following set of tests checks small floating point offsets
|
||||
// colliding at the corner of two boxes, to ensure the function under
|
||||
// test can detect which axis collided first.
|
||||
|
||||
SECTION("Collides on X axis near (+X,+Y,+Z) corner.")
|
||||
{
|
||||
aabb3f m{bx+2.3f, by+2.29f, bz+2.29f, bx+4.2f, by+4.2f, bz+4.2f};
|
||||
v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f};
|
||||
f32 dtime = 1.0f;
|
||||
CHECK(0 == axisAlignedCollision(s, m, v, &dtime));
|
||||
CHECK(std::fabs(dtime - 0.9f) < EPSILON);
|
||||
}
|
||||
|
||||
SECTION("Collides on Y axis near (+X,+Y,+Z) corner.")
|
||||
{
|
||||
aabb3f m{bx+2.29f, by+2.3f, bz+2.29f, bx+4.2f, by+4.2f, bz+4.2f};
|
||||
v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f};
|
||||
f32 dtime = 1.0f;
|
||||
CHECK(1 == axisAlignedCollision(s, m, v, &dtime));
|
||||
CHECK(std::fabs(dtime - 0.9f) < EPSILON);
|
||||
}
|
||||
|
||||
SECTION("Collides on Z axis near (+X,+Y,+Z) corner.")
|
||||
{
|
||||
aabb3f m{bx+2.29f, by+2.29f, bz+2.3f, bx+4.2f, by+4.2f, bz+4.2f};
|
||||
v3f v{-1.0f/3.0f, -1.0f/3.0f, -1.0/3.0f};
|
||||
f32 dtime = 1.0f;
|
||||
CHECK(2 == axisAlignedCollision(s, m, v, &dtime));
|
||||
CHECK(std::fabs(dtime - 0.9f) < EPSILON);
|
||||
}
|
||||
|
||||
SECTION("Collides on X axis near (-X,-Y,-Z) corner.")
|
||||
{
|
||||
aabb3f m{bx-4.2f, by-4.2f, bz-4.2f, bx-2.3f, by-2.29f, bz-2.29f};
|
||||
v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f};
|
||||
f32 dtime = 17.0f;
|
||||
CHECK(0 == axisAlignedCollision(s, m, v, &dtime));
|
||||
CHECK(std::fabs(dtime - 16.1f) < EPSILON);
|
||||
}
|
||||
|
||||
SECTION("Collides on Y axis near (-X,-Y,-Z) corner.")
|
||||
{
|
||||
aabb3f m{bx-4.2f, by-4.2f, bz-4.2f, bx-2.29f, by-2.3f, bz-2.29f};
|
||||
v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f};
|
||||
f32 dtime = 17.0f;
|
||||
CHECK(1 == axisAlignedCollision(s, m, v, &dtime));
|
||||
CHECK(std::fabs(dtime - 16.1f) < EPSILON);
|
||||
}
|
||||
|
||||
SECTION("Collides on Z axis near (-X,-Y,-Z) corner.")
|
||||
{
|
||||
aabb3f m{bx-4.2f, by-4.2f, bz-4.2f, bx-2.29f, by-2.29f, bz-2.3f};
|
||||
v3f v{1.0f/7.0f, 1.0f/7.0f, 1.0/7.0f};
|
||||
f32 dtime = 17.0f;
|
||||
CHECK(2 == axisAlignedCollision(s, m, v, &dtime));
|
||||
CHECK(std::fabs(dtime - 16.1f) < EPSILON);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Test simple moving collision.", "[collision]")
|
||||
{
|
||||
TestGameDef gamedef;
|
||||
|
||||
auto env = std::make_unique<TestEnvironment>(&gamedef);
|
||||
REQUIRE(!g_collision_problems_encountered);
|
||||
|
||||
for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
|
||||
for (s16 z = 0; z < MAP_BLOCKSIZE; z++)
|
||||
|
@ -217,163 +235,187 @@ void TestCollision::testCollisionMoveSimple(IGameDef *gamedef)
|
|||
const aabb3f box(fpos(-0.1f, 0, -0.1f), fpos(0.1f, 1.4f, 0.1f));
|
||||
collisionMoveResult res;
|
||||
|
||||
/* simple movement with accel */
|
||||
pos = fpos(4, 1, 4);
|
||||
speed = fpos(0, 0, 0);
|
||||
accel = fpos(0, 1, 0);
|
||||
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.collisions.empty());
|
||||
UASSERTEQ_V3F(pos, fpos(4, 1.5f, 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.05f,
|
||||
&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);
|
||||
SECTION("Simple movement with accel.")
|
||||
{
|
||||
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));
|
||||
pos = fpos(4, 1, 4);
|
||||
speed = fpos(0, 0, 0);
|
||||
accel = fpos(0, 1, 0);
|
||||
res = collisionMoveSimple(env.get(), &gamedef, box, 0.0f, 1.0f,
|
||||
&pos, &speed, accel);
|
||||
|
||||
CHECK(!res.touching_ground);
|
||||
CHECK(!res.collides);
|
||||
CHECK(!res.standing_on_object);
|
||||
CHECK(res.collisions.empty());
|
||||
UASSERTEQ_V3F(pos, fpos(4, 1.5f, 4));
|
||||
UASSERTEQ_V3F(speed, fpos(0, 1, 0));
|
||||
}
|
||||
|
||||
/* glitched into ground */
|
||||
pos = fpos(0, 0.499f, 0);
|
||||
speed = fpos(0, 0, 0);
|
||||
accel = fpos(0, -9.81f, 0);
|
||||
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 0.05f,
|
||||
&pos, &speed, accel);
|
||||
|
||||
UASSERTEQ_V3F(pos, fpos(0, 0.5f, 0)); // moved back out
|
||||
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
|
||||
UASSERT(res.collides);
|
||||
UASSERT(res.touching_ground);
|
||||
UASSERT(!res.standing_on_object);
|
||||
UASSERT(res.collisions.size() == 1);
|
||||
SECTION("Standing on ground.")
|
||||
{
|
||||
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));
|
||||
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.05f,
|
||||
&pos, &speed, accel);
|
||||
|
||||
CHECK(res.collides);
|
||||
CHECK(res.touching_ground);
|
||||
CHECK(!res.standing_on_object);
|
||||
UASSERTEQ_V3F(pos, fpos(0, 0.5f, 0));
|
||||
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
|
||||
REQUIRE(res.collisions.size() == 1);
|
||||
{
|
||||
auto &ci = res.collisions.front();
|
||||
CHECK(ci.type == COLLISION_NODE);
|
||||
CHECK(ci.axis == COLLISION_AXIS_Y);
|
||||
CHECK(ci.node_p == v3s16(0, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
/* falling on ground */
|
||||
pos = fpos(0, 1.2345f, 0);
|
||||
speed = fpos(0, -3.f, 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);
|
||||
// Current collision code uses linear collision, which incorrectly yields a collision at 0.741 here
|
||||
// but usually this resolves itself in the next dtime, fortunately.
|
||||
// Parabolic collision should correctly find this in one step.
|
||||
// UASSERTEQ_V3F(pos, fpos(0, 0.5f, 0));
|
||||
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
|
||||
UASSERT(res.collisions.size() == 1);
|
||||
SECTION("Glitched into ground.")
|
||||
{
|
||||
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));
|
||||
pos = fpos(0, 0.499f, 0);
|
||||
speed = fpos(0, 0, 0);
|
||||
accel = fpos(0, -9.81f, 0);
|
||||
res = collisionMoveSimple(env.get(), &gamedef, box, 0.0f, 0.05f,
|
||||
&pos, &speed, accel);
|
||||
|
||||
UASSERTEQ_V3F(pos, fpos(0, 0.5f, 0)); // moved back out
|
||||
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
|
||||
CHECK(res.collides);
|
||||
CHECK(res.touching_ground);
|
||||
CHECK(!res.standing_on_object);
|
||||
REQUIRE(res.collisions.size() == 1);
|
||||
{
|
||||
auto &ci = res.collisions.front();
|
||||
CHECK(ci.type == COLLISION_NODE);
|
||||
CHECK(ci.axis == COLLISION_AXIS_Y);
|
||||
CHECK(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);
|
||||
|
||||
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);
|
||||
// Current collision code uses linear collision, which incorrectly yields a collision at 0.672 here
|
||||
// but usually this resolves itself in the next dtime, fortunately.
|
||||
// Parabolic collision should correctly find this in one step.
|
||||
// UASSERTEQ_V3F(pos, fpos(0, 0.5f, 0));
|
||||
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
|
||||
UASSERT(res.collisions.size() == 1);
|
||||
SECTION("Falling on ground.")
|
||||
{
|
||||
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));
|
||||
pos = fpos(0, 1.2345f, 0);
|
||||
speed = fpos(0, -3.f, 0);
|
||||
accel = fpos(0, -9.81f, 0);
|
||||
res = collisionMoveSimple(env.get(), &gamedef, box, 0.0f, 0.5f,
|
||||
&pos, &speed, accel);
|
||||
|
||||
CHECK(res.collides);
|
||||
CHECK(res.touching_ground);
|
||||
CHECK(!res.standing_on_object);
|
||||
// Current collision code uses linear collision, which incorrectly yields a collision at 0.741 here
|
||||
// but usually this resolves itself in the next dtime, fortunately.
|
||||
// Parabolic collision should correctly find this in one step.
|
||||
// UASSERTEQ_V3F(pos, fpos(0, 0.5f, 0));
|
||||
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
|
||||
REQUIRE(res.collisions.size() == 1);
|
||||
{
|
||||
auto &ci = res.collisions.front();
|
||||
CHECK(ci.type == COLLISION_NODE);
|
||||
CHECK(ci.axis == COLLISION_AXIS_Y);
|
||||
CHECK(ci.node_p == v3s16(0, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
/* moving over ground, no gravity */
|
||||
pos = fpos(0, 0.5f, 0);
|
||||
speed = fpos(-1.6f, 0, -1.7f);
|
||||
accel = fpos(0, 0.0f, 0);
|
||||
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 1.0f,
|
||||
&pos, &speed, accel);
|
||||
SECTION("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);
|
||||
// UASSERT(res.touching_ground); // no gravity, so not guaranteed
|
||||
UASSERT(!res.standing_on_object);
|
||||
UASSERTEQ_V3F(pos, fpos(-1.6f, 0.5f, -1.7f));
|
||||
UASSERTEQ_V3F(speed, fpos(-1.6f, 0, -1.7f));
|
||||
UASSERT(res.collisions.empty());
|
||||
CHECK(!res.touching_ground);
|
||||
CHECK(!res.collides);
|
||||
CHECK(!res.standing_on_object);
|
||||
|
||||
/* moving over ground, with gravity */
|
||||
pos = fpos(5.5f, 0.5f, 5.5f);
|
||||
speed = fpos(-1.0f, 0.0f, -0.1f);
|
||||
accel = fpos(0, -9.81f, 0);
|
||||
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 1.0f,
|
||||
&pos, &speed, accel);
|
||||
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(4.5f, 0.5f, 5.4f));
|
||||
UASSERTEQ_V3F(speed, fpos(-1.0f, 0, -0.1f));
|
||||
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(5, 0, 5));
|
||||
CHECK(res.collides);
|
||||
CHECK(res.touching_ground);
|
||||
CHECK(!res.standing_on_object);
|
||||
// Current collision code uses linear collision, which incorrectly yields a collision at 0.672 here
|
||||
// but usually this resolves itself in the next dtime, fortunately.
|
||||
// Parabolic collision should correctly find this in one step.
|
||||
// UASSERTEQ_V3F(pos, fpos(0, 0.5f, 0));
|
||||
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
|
||||
REQUIRE(res.collisions.size() == 1);
|
||||
{
|
||||
auto &ci = res.collisions.front();
|
||||
CHECK(ci.type == COLLISION_NODE);
|
||||
CHECK(ci.axis == COLLISION_AXIS_Y);
|
||||
CHECK(ci.node_p == v3s16(0, 0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
/* not moving never collides */
|
||||
pos = fpos(0, -100, 0);
|
||||
speed = fpos(0, 0, 0);
|
||||
accel = fpos(0, 0, 0);
|
||||
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 1/60.0f,
|
||||
&pos, &speed, accel);
|
||||
UASSERT(!res.collides);
|
||||
SECTION("Moving over ground, no gravity.")
|
||||
{
|
||||
pos = fpos(0, 0.5f, 0);
|
||||
speed = fpos(-1.6f, 0, -1.7f);
|
||||
accel = fpos(0, 0.0f, 0);
|
||||
res = collisionMoveSimple(env.get(), &gamedef, box, 0.0f, 1.0f,
|
||||
&pos, &speed, accel);
|
||||
|
||||
/* collision in ignore */
|
||||
pos = fpos(0, -100, 0);
|
||||
speed = fpos(5, 0, 0);
|
||||
accel = fpos(0, 0, 0);
|
||||
res = collisionMoveSimple(env.get(), gamedef, box, 0.0f, 1/60.0f,
|
||||
&pos, &speed, accel);
|
||||
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
|
||||
UASSERT(!res.collides); // FIXME this is actually inconsistent
|
||||
UASSERT(res.collisions.empty());
|
||||
CHECK(!res.collides);
|
||||
// CHECK(res.touching_ground); // no gravity, so not guaranteed
|
||||
CHECK(!res.standing_on_object);
|
||||
UASSERTEQ_V3F(pos, fpos(-1.6f, 0.5f, -1.7f));
|
||||
UASSERTEQ_V3F(speed, fpos(-1.6f, 0, -1.7f));
|
||||
CHECK(res.collisions.empty());
|
||||
}
|
||||
|
||||
SECTION("Moving over ground, with gravity.")
|
||||
{
|
||||
/* moving over ground, with gravity */
|
||||
pos = fpos(5.5f, 0.5f, 5.5f);
|
||||
speed = fpos(-1.0f, 0.0f, -0.1f);
|
||||
accel = fpos(0, -9.81f, 0);
|
||||
res = collisionMoveSimple(env.get(), &gamedef, box, 0.0f, 1.0f,
|
||||
&pos, &speed, accel);
|
||||
|
||||
CHECK(res.collides);
|
||||
CHECK(res.touching_ground);
|
||||
CHECK(!res.standing_on_object);
|
||||
UASSERTEQ_V3F(pos, fpos(4.5f, 0.5f, 5.4f));
|
||||
UASSERTEQ_V3F(speed, fpos(-1.0f, 0, -0.1f));
|
||||
CHECK(res.collisions.size() == 1);
|
||||
{ // first collision on y axis zeros speed and acceleration.
|
||||
auto &ci = res.collisions.front();
|
||||
CHECK(ci.type == COLLISION_NODE);
|
||||
CHECK(ci.axis == COLLISION_AXIS_Y);
|
||||
CHECK(ci.node_p == v3s16(5, 0, 5));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Not moving never collides.")
|
||||
{
|
||||
pos = fpos(0, -100, 0);
|
||||
speed = fpos(0, 0, 0);
|
||||
accel = fpos(0, 0, 0);
|
||||
res = collisionMoveSimple(env.get(), &gamedef, box, 0.0f, 1/60.0f,
|
||||
&pos, &speed, accel);
|
||||
CHECK(!res.collides);
|
||||
}
|
||||
|
||||
SECTION("Collision in ignore.")
|
||||
{
|
||||
pos = fpos(0, -100, 0);
|
||||
speed = fpos(5, 0, 0);
|
||||
accel = fpos(0, 0, 0);
|
||||
res = collisionMoveSimple(env.get(), &gamedef, box, 0.0f, 1/60.0f,
|
||||
&pos, &speed, accel);
|
||||
UASSERTEQ_V3F(speed, fpos(0, 0, 0));
|
||||
CHECK(!res.collides); // FIXME this is actually inconsistent
|
||||
CHECK(res.collisions.empty());
|
||||
}
|
||||
|
||||
// TODO things to test:
|
||||
// standing_on_object, multiple collisions, bouncy, stepheight
|
||||
|
||||
// No warnings should have been raised during our test.
|
||||
UASSERT(!g_collision_problems_encountered);
|
||||
CHECK(!g_collision_problems_encountered);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue