File size: 2,868 Bytes
9897e20 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include "obb.hpp"
#include <gtest/gtest.h>
#include <madrona/components.hpp>
#include <madrona/math.hpp>
using namespace madrona;
using namespace madrona::base;
using namespace madrona::math;
using madrona_gpudrive::OrientedBoundingBox2D;
TEST(CollisionDetectionAxisAligned, Colliding) {
Position origin{{.x = 0, .y = 0, .z = 0}};
Position offset{{.x = 1, .y = 1, .z = 1}};
Scale unitCubeScale{{.d0 = 1, .d1 = 1, .d2 = 1}};
Rotation noRotation{Quat::angleAxis(0, madrona::math::up)};
auto obbA = OrientedBoundingBox2D::from(origin, noRotation, unitCubeScale);
auto obbB =
OrientedBoundingBox2D::from(origin + offset, noRotation, unitCubeScale);
EXPECT_TRUE(OrientedBoundingBox2D::hasCollided(obbA, obbB));
}
TEST(CollisionDetectionAxisAligned, NotColliding) {
Position origin{{.x = 0, .y = 0, .z = 0}};
Position offset{{.x = 2, .y = 2, .z = 0}};
Scale unitCubeScale{{.d0 = 0.5, .d1 = 0.5, .d2 = 1}};
Rotation noRotation{Quat::angleAxis(0, madrona::math::up)};
auto obbA = OrientedBoundingBox2D::from(origin, noRotation, unitCubeScale);
auto obbB =
OrientedBoundingBox2D::from(origin + offset, noRotation, unitCubeScale);
EXPECT_FALSE(OrientedBoundingBox2D::hasCollided(obbA, obbB));
}
TEST(CollisionDetectionAxisAligned, PointIntersection) {
Position origin{{.x = 0, .y = 0, .z = 0}};
Position offset{{.x = 1, .y = 1, .z = 0}};
Scale unitCubeScale{{.d0 = 0.5, .d1 = 0.5, .d2 = 0.5}};
Rotation noRotation{Quat::angleAxis(0, madrona::math::up)};
auto obbA = OrientedBoundingBox2D::from(origin, noRotation, unitCubeScale);
auto obbB =
OrientedBoundingBox2D::from(origin + offset, noRotation, unitCubeScale);
EXPECT_TRUE(OrientedBoundingBox2D::hasCollided(obbA, obbB));
}
TEST(CollisionDetectionAxisAligned, OneInsideOther) {
Position origin{{.x = 0, .y = 0, .z = 0}};
Scale unitCubeScale{{.d0 = 1, .d1 = 1, .d2 = 1}};
Scale halfUnitCubeScale{{.d0 = 0.5, .d1 = 0.5, .d2 = 0.5}};
Rotation noRotation{Quat::angleAxis(0, madrona::math::up)};
auto obbA = OrientedBoundingBox2D::from(origin, noRotation, unitCubeScale);
auto obbB =
OrientedBoundingBox2D::from(origin, noRotation, halfUnitCubeScale);
EXPECT_TRUE(OrientedBoundingBox2D::hasCollided(obbA, obbB));
}
TEST(CollisionDetectionNonAxisAligned, ExhaustiveRotations) {
Position posA{{.x = 0, .y = 0, .z = 0}};
Position posB{{.x = 0.5, .y = 0.5, .z = 0}};
Scale unitCubeScale{{.d0 = 1, .d1 = 1, .d2 = 1}};
Rotation rotA{Quat::angleAxis(0, madrona::math::up)};
for (float degrees = 0; degrees < 360; degrees += 15) {
Rotation rotB{Quat::angleAxis(toRadians(degrees), madrona::math::up)};
auto obbA = OrientedBoundingBox2D::from(posA, rotA, unitCubeScale);
auto obbB = OrientedBoundingBox2D::from(posB, rotB, unitCubeScale);
EXPECT_TRUE(OrientedBoundingBox2D::hasCollided(obbA, obbB));
}
}
|