initial commit

This commit is contained in:
2010-04-05 23:38:59 +02:00
commit e7e0b39e82
150 changed files with 28636 additions and 0 deletions
@@ -0,0 +1,51 @@
PROJECT (ENGINETESTS)
CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
# Needed for UnitTest++
LIST( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../CMake )
SET ( TESTS_SRCS
main.cc
general.cc
polygon_sphere.cc
sphere_sphere.cc
)
FIND_PACKAGE (UnitTest++)
INCLUDE_DIRECTORIES ( ../mathlib/ )
SET_TARGET_PROPERTIES ( ${PROJECT_EXECUTABLES} PROPERTIES
LINKER_LANGUAGE CXX
)
IF ( UNITTEST++_FOUND )
ADD_EXECUTABLE ( coll2dtests ${TESTS_SRCS} )
INCLUDE_DIRECTORIES ( ${UNITTEST++_INCLUDE_DIR} )
SET_TARGET_PROPERTIES ( coll2dtests PROPERTIES
LINKER_LANGUAGE CXX
OUTPUT_NAME runtests
)
TARGET_LINK_LIBRARIES ( coll2dtests
${UNITTEST++_LIBRARY}
mathlib
coll2d
)
OPTION (RUN_AUTOMATIC_TESTS "Perform automatic tests after compilation?" OFF)
IF (RUN_AUTOMATIC_TESTS)
ADD_CUSTOM_COMMAND (TARGET coll2dtests
POST_BUILD
COMMAND coll2dtests
COMMENT "Running automated tests..."
)
ENDIF (RUN_AUTOMATIC_TESTS)
ENDIF ( UNITTEST++_FOUND )
+284
View File
@@ -0,0 +1,284 @@
#include <UnitTest++.h>
#include <coll2d.h>
using namespace coll2d;
using namespace std;
TEST ( SphereCopyConstructer ) {
Sphere sphere_a (123.);
sphere_a.setVelocity (vector3d(1., 2., 3.));
Sphere sphere_b (sphere_a);
CHECK_EQUAL (sphere_a.getRadius (), sphere_b.getRadius ());
vector3d velocity_a = sphere_a.getVelocity ();
vector3d velocity_b = sphere_b.getVelocity ();
CHECK (velocity_a == velocity_b );
sphere_b.setVelocity (vector3d (0., 0., 0.));
velocity_a = sphere_a.getVelocity ();
velocity_b = sphere_b.getVelocity ();
CHECK (velocity_a != velocity_b );
}
TEST ( PolygonCopyConstructer ) {
Polygon polygon_a (3);
vector3d vertice0 (-1., 0., -1.);
vector3d vertice1 (1., 0., -1.);
vector3d vertice2 (0., 0., 1.);
polygon_a.setVertice (0, vertice0);
polygon_a.setVertice (1, vertice1);
polygon_a.setVertice (2, vertice2);
polygon_a.setVelocity (vector3d(1., 2., 3.));
Polygon polygon_b (polygon_a);
vector3d velocity_a = polygon_a.getVelocity ();
vector3d velocity_b = polygon_b.getVelocity ();
CHECK (vertice0 == polygon_b.getVertice (0));
CHECK (vertice1 == polygon_b.getVertice (1));
CHECK (vertice2 == polygon_b.getVertice (2));
polygon_b.setVelocity (vector3d (0., 0., 0.));
velocity_a = polygon_a.getVelocity ();
velocity_b = polygon_b.getVelocity ();
CHECK (velocity_a != velocity_b );
}
/** Checks whether we identify the different shapes
* correctly.
*/
TEST ( CheckShapeType ) {
vector3d sphere_position (0., 0., 0.);
Shape* polygon = new Polygon (0, NULL);
Shape* sphere = new Sphere (0, sphere_position);
Shape* cast_test = NULL;
cast_test = dynamic_cast <Sphere*> ( sphere );
CHECK_EQUAL (cast_test, sphere);
cast_test = dynamic_cast <Sphere*> ( polygon );
CHECK (cast_test == NULL );
cast_test = dynamic_cast <Polygon*> (polygon);
CHECK_EQUAL (cast_test, polygon);
cast_test = dynamic_cast <Polygon*> (sphere);
CHECK (cast_test == NULL );
delete polygon;
delete sphere;
}
/** Checks whether the right check function is chosen
*/
TEST ( CheckGetCheck ) {
vector3d sphere_position (0., 0., 0.);
Shape* polygon = new Polygon (0, NULL);
Shape* sphere = new Sphere (0, sphere_position);
check_cb check = NULL;
// polygon - sphere -> polygon_sphere
check = get_check (polygon, sphere);
CHECK (static_cast<check_cb> (check) == static_cast<check_cb> (check_collision_polygon_sphere));
// sphere - polygon -> polygon_sphere
check = get_check (sphere, polygon);
CHECK (static_cast<check_cb> (check) == static_cast<check_cb> (check_collision_polygon_sphere));
// and we do not want that the pointers have changed!
// (as it used to be in previous versions)
CHECK (dynamic_cast <Polygon*> (sphere) == NULL);
CHECK (dynamic_cast <Sphere*> (polygon) == NULL);
check = get_check (sphere, sphere);
CHECK (check == check_collision_sphere_sphere);
delete polygon;
delete sphere;
}
TEST ( CheckErrorNotImplemented ) {
Shape* polygon = new Polygon (0, NULL);
vector3d velocity (0., 0., 0.);
CollisionInfo info;
int result = check_collision_rel (1., polygon, polygon, &velocity, &info);
CHECK (result == CHECK_ERROR_NOT_IMPLEMENTED);
delete polygon;
}
TEST ( CheckCalculateContactPlanePoint ) {
vector3d normal (1., 0., 0.);
vector3d plane_point (0., 0., 0.);
vector3d point (1., 0., 0.);
vector3d velocity (-1., 0., 0);
float result;
// Directly moving towards the plane
result = calculate_contact_plane_point (normal, plane_point, point, velocity);
CHECK_EQUAL (1, result);
// Moving with a slight angle onto the plane
velocity.setValues (-1., 0., 0.2);
result = calculate_contact_plane_point (normal, plane_point, point, velocity);
CHECK_EQUAL (1, result);
velocity.setValues (-1., 0., 0.);
// Point is "below" the plane and moves even deeper
plane_point.setValues (2., 0., 0);
result = calculate_contact_plane_point (normal, plane_point, point, velocity);
CHECK_EQUAL (-999, result);
// Point is "below" but moves towards the plane
velocity.setValues (1., 0., 0);
result = calculate_contact_plane_point (normal, plane_point, point, velocity);
CHECK_EQUAL (-111, result);
}
TEST ( CheckCollisionStepsize ) {
vector3d normal (1., 0., 0.);
vector3d plane_point (0., 0., 0.);
vector3d point (1., 0., 0.);
vector3d velocity (-1., 0., 0);
float result;
// Directly moving towards the plane
result = calculate_contact_plane_point (normal, plane_point, point, velocity);
CHECK_EQUAL (1, result);
// Moving with a slight angle onto the plane
velocity.setValues (-1., 0., 0.2);
result = calculate_contact_plane_point (normal, plane_point, point, velocity);
CHECK_EQUAL (1, result);
velocity.setValues (-1., 0., 0.);
// Point is "below" the plane and moves even deeper
plane_point.setValues (2., 0., 0);
result = calculate_contact_plane_point (normal, plane_point, point, velocity);
CHECK_EQUAL (-999, result);
// Point is "below" but moves towards the plane
velocity.setValues (1., 0., 0);
result = calculate_contact_plane_point (normal, plane_point, point, velocity);
CHECK_EQUAL (-111, result);
}
/* Here we test, whether the value of info.reference_shape is correct.
* If we swap the shapes passed to check_collision, then the value must swap,
* too.
*/
TEST ( CheckReferenceShapeValue ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
// first part of the test, the collision occurs
// on an edge
vector3d sphere_position (2.3, 0., 0.);
Shape* sphere = new Sphere (1, sphere_position);
sphere->setVelocity (vector3d (-1., 0., 0.));
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0, info.reference_shape);
result = check_collision (1., sphere, polygon, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (1, info.reference_shape);
// here the collision occurs on a vertice
sphere->setPosition (vector3d (1.5, 0., -2.5));
sphere->setVelocity (vector3d (0., 0., 1.));
result = check_collision (1., sphere, polygon, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (1, info.reference_shape);
result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0, info.reference_shape);
delete sphere;
delete polygon;
}
/* Test whether Polygon::getCopy() does what it should */
TEST ( CheckPolygonGetCopy ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Polygon* polygon = new Polygon (4, vertices);
Polygon* polygon_copy = polygon->getCopy();
CHECK (polygon != polygon_copy);
CHECK_EQUAL (polygon->getVerticeCount(), polygon_copy->getVerticeCount());
unsigned int i, count;
count = polygon->getVerticeCount ();
for (i = 0; i < count; i ++) {
vector3d vertice = polygon->getVertice (i);
vector3d vertice_copy = polygon_copy->getVertice (i);
int j;
for (j = 0; j < 3; j++) {
CHECK_EQUAL (vertice[j], vertice_copy[j]);
}
}
delete polygon;
delete polygon_copy;
}
/* Test whether Sphere::getCopy() does what it should */
TEST ( CheckSphereGetCopy ) {
Sphere* sphere = new Sphere (1.23);
Sphere* sphere_copy = sphere->getCopy();
CHECK_EQUAL (sphere->getRadius(), sphere_copy->getRadius());
CHECK (sphere != sphere_copy);
delete sphere;
delete sphere_copy;
}
TEST ( SphereSetGetRadius ) {
Sphere sphere (123.);
CHECK_EQUAL (123, sphere.getRadius());
sphere.setRadius (456.);
CHECK_EQUAL (456, sphere.getRadius());
}
+6
View File
@@ -0,0 +1,6 @@
#include <UnitTest++.h>
int main (int argc, char *argv[])
{
return UnitTest::RunAllTests ();
}
@@ -0,0 +1,544 @@
#include <UnitTest++.h>
#include <coll2d.h>
using namespace coll2d;
using namespace std;
TEST ( CheckErrorInvalidTypes ) {
Shape* polygon = new Polygon (0, NULL);
vector3d velocity (0., 0., 0.);
vector3d sphere_position (2.1, 0., 0.);
Shape* sphere = new Sphere (1, sphere_position);
sphere->setVelocity (vector3d(0., 0., 0.));
CollisionInfo info;
int result = check_collision_polygon_sphere (1., sphere, sphere, &info);
CHECK (result == CHECK_ERROR_INVALID_TYPES);
result = check_collision_polygon_sphere (1., polygon, polygon, &info);
CHECK (result == CHECK_ERROR_INVALID_TYPES);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereNoVelocity ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., -1.);
vertices[1].setValues (1., 0., -1.);
vertices[2].setValues (1., 0., 1.);
vertices[3].setValues (-1., 0., 1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (2.1, 0., 0.);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (0., 0., 0.);
CollisionInfo info;
int result = check_collision_rel (1., polygon, sphere, &velocity, &info);
CHECK (result >= 0);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereDiverging ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (2.1, 0., 0.);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (1., 0., 0.);
CollisionInfo info;
int result = check_collision_rel (1., polygon, sphere, &velocity, &info);
CHECK (result >= 0);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereContact ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (2., 0., 0.);
Shape* sphere = new Sphere (1, sphere_position);
sphere->setVelocity (vector3d (-1., 0., 0.));
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK (result > 0);
CHECK_EQUAL (0.0, info.time);
CHECK_EQUAL (1., info.normal[0]);
CHECK_EQUAL (0., info.normal[1]);
CHECK_EQUAL (0., info.normal[2]);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereColliding ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (2.5, 0., 0.1);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (-1., 0., 0.);
CollisionInfo info;
int result = check_collision_rel (1., polygon, sphere, &velocity, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereCollidingTop ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (0., 0., 2.5);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (0., 0., -1.);
CollisionInfo info;
int result = check_collision_rel (1., polygon, sphere, &velocity, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereCollidingNonPerpendicular ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (0., 0., 2.5);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (0.01, 0., -1.);
CollisionInfo info;
int result = check_collision_rel (1., polygon, sphere, &velocity, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereCollidingCorner) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position ( 1.5, 0., 2.5);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (0.0, 0., -1.);
CollisionInfo info;
int result = check_collision_rel (1., polygon, sphere, &velocity, &info);
CHECK_EQUAL (1, result);
delete polygon;
delete sphere;
}
// In this test we make sure, that the z value of both the polygon and the
// sphere get ignored:
TEST ( CheckCollisionPolygonSphereCollidingCornerIgnoreHeight) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position ( 1.5, 10., 2.5);
Shape* sphere = new Sphere (1, sphere_position);
sphere->setVelocity (vector3d (0., 0., -1.));
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
vertices[0].setValues (-1., -10., 1.);
vertices[1].setValues (1., -10., 1.);
vertices[2].setValues (1., -10., -1.);
vertices[3].setValues (-1., -10., -1.);
sphere->setPosition (vector3d (1.5, 0., 2.5));
result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereCollidingCornerTop) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (1.5, 0., - 2.5);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (0., 0., 1.);
CollisionInfo info;
int result = check_collision_rel (1., polygon, sphere, &velocity, &info);
CHECK_EQUAL (1, result);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereCollidingCornerNeighbour) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., -0.99);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (1.5 , 0., - 2.5);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (0., 0., 1.);
CollisionInfo info;
check_collision_rel (1., polygon, sphere, &velocity, &info);
CHECK (info.point == vertices[2]);
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereNonCollidingSetPosition ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (2.5, 0., 0.1);
Shape* sphere = new Sphere (1, sphere_position);
sphere->setPosition (vector3d (35, 0, 0));
vector3d velocity (-1., 0., 0.);
CollisionInfo info;
int result = check_collision_rel (1., polygon, sphere, &velocity, &info);
CHECK_EQUAL (0, result);
delete polygon;
delete sphere;
}
// In this test we set the velocity of the polygon instead of the
// sphere.
TEST ( CheckCollisionPolygonSphereCollidingPolygonSetVelocity ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (2.5, 0., 0.);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (1., 0., 0.);
polygon->setVelocity (velocity);
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
delete polygon;
delete sphere;
}
TEST ( CheckSetGetAngle ) {
Shape* polygon = new Polygon (0, NULL);
polygon->setAngle (0.1234567);
CHECK (fabs (0.1234567 - polygon->getAngle ()) < 1.0e-7);
delete polygon;
}
TEST ( CheckCollisionPolygonSphereDiamond ) {
vector3d vertices[4];
float sqrt2 = sqrt (2);
vertices[0].setValues (-sqrt2, 0., 0.);
vertices[1].setValues (0., 0., sqrt2);
vertices[2].setValues (sqrt2, 0., 0.);
vertices[3].setValues (0., 0., -sqrt2);
Shape* polygon = new Polygon (4, vertices);
Shape* sphere = new Sphere (1., vector3d (sqrt2 + 1.5, 0., 0.));
sphere->setVelocity (vector3d (-1., 0., 0.));
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK (fabs (0.5 - info.time) <= 1.0e-6);
CHECK_EQUAL (0, info.reference_shape);
delete polygon;
delete sphere;
}
TEST ( CheckPolygonSpherePassingBy ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (1., 0., 2.5);
Shape* sphere = new Sphere (1, sphere_position);
sphere->setVelocity (vector3d (-2., 0., 0.));
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (0, result);
delete sphere;
delete polygon;
}
TEST ( CheckCollisionPolygonSphereReturnValuePoint ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
polygon->setPosition (vector3d (sqrt (2), 0., - sqrt (2)));
polygon->setAngle (M_PI * 0.25);
Shape* sphere = new Sphere (1, vector3d (-sqrt(2) * 0.5, 0., sqrt (2) * 0.5));
sphere->setVelocity (vector3d (sqrt (2), 0., -sqrt(2)));
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK ( fabs (0.5 - info.time) < 1.0e-6);
CHECK ( (info.point - vector3d (sqrt (2) * 0.5, 0., -sqrt(2) * 0.5)).length() < 1.0e-6 );
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereReturnValuePointTranslated ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
polygon->setPosition (vector3d (sqrt (2) + 1., 0., - sqrt (2) - 1.));
polygon->setAngle (M_PI * 0.25);
Shape* sphere = new Sphere (1, vector3d (-sqrt(2) * 0.5 + 1., 0., sqrt (2) * 0.5 -1.));
sphere->setVelocity (vector3d (sqrt (2), 0., -sqrt(2)));
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK ( fabs (0.5 - info.time) < 1.0e-6);
CHECK ( (info.point - vector3d (sqrt (2) * 0.5 + 1, 0., -sqrt(2) * 0.5 - 1.)).length() < 1.0e-6 );
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereReturnValuePointVerticeTranslated ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
polygon->setPosition (vector3d (0. + 2., 0., - sqrt (2) - 1.5));
polygon->setAngle (M_PI * 0.25);
Shape* sphere = new Sphere (1, vector3d (0. + 2., 0., 0.));
sphere->setVelocity (vector3d (0. , 0., -1.));
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK ( fabs (0.5 - info.time) < 1.0e-6);
CHECK ( (info.point - vector3d (0. + 2., 0., -1.5)).length() < 1.0e-6 );
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereReferenceShapePolygon ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
polygon->setPosition (vector3d (0., 0., 0.));
polygon->setVelocity (vector3d (1., 0., 0.));
Shape* sphere = new Sphere (1, vector3d (2.5, 0., 0.));
sphere->setVelocity (vector3d (0., 0., 0.));
CollisionInfo info;
int result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK ( fabs (0.5 - info.time) < 1.0e-6);
CHECK_EQUAL ( 0, info.reference_shape );
polygon->setPosition (vector3d (0., 0., 0.));
polygon->setVelocity (vector3d (1., 0., 0.));
sphere->setPosition (vector3d (2.5, 0., 0.));
sphere->setVelocity (vector3d (0., 0., 0.));
result = check_collision (1., sphere, polygon, &info);
CHECK_EQUAL (1, result);
CHECK ( fabs (0.5 - info.time) < 1.0e-6);
CHECK_EQUAL ( 1, info.reference_shape );
delete polygon;
delete sphere;
}
TEST ( CheckCollisionPolygonSphereTimestep ) {
vector3d vertices[4];
vertices[0].setValues (-1., 0., 1.);
vertices[1].setValues (1., 0., 1.);
vertices[2].setValues (1., 0., -1.);
vertices[3].setValues (-1., 0., -1.);
Shape* polygon = new Polygon (4, vertices);
vector3d sphere_position (2.5, 0., 0.1);
Shape* sphere = new Sphere (1, sphere_position);
vector3d velocity (-1., 0., 0.);
sphere->setVelocity (velocity);
CollisionInfo info;
int result;
result = check_collision (1., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
result = check_collision (2., polygon, sphere, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
result = check_collision (0.499, polygon, sphere, &info);
CHECK_EQUAL (0, result);
delete polygon;
delete sphere;
}
@@ -0,0 +1,245 @@
#include <UnitTest++.h>
#include <coll2d.h>
using namespace coll2d;
using namespace std;
TEST ( CheckSphereSphereInvalidTypes ) {
Shape* polygon = new Polygon (0, NULL);
vector3d velocity (0., 0., 0.);
vector3d sphere_position (2.1, 0., 0.);
Shape* sphere = new Sphere (1, sphere_position);
sphere->setVelocity (vector3d(0., 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., polygon, sphere, &info);
CHECK (result == CHECK_ERROR_INVALID_TYPES);
delete polygon;
delete sphere;
}
TEST ( CheckSphereSphereNoVelocity ) {
Shape* sphere_a = new Sphere (1., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1, vector3d (4., 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (0, result);
delete sphere_a;
delete sphere_b;
}
/* This tests whether we report a collision time of 0.0 when two spheres are
* in contact with each other
*/
TEST ( CheckCollisionSphereSphereContact ) {
Shape* sphere_a = new Sphere (1., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1, vector3d (2., 0., 0.));
sphere_b->setVelocity (vector3d(-1., 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK (result >= 0);
CHECK_EQUAL (0.0, info.time);
delete sphere_a;
delete sphere_b;
}
TEST ( CheckSphereSphereDiverging ) {
Shape* sphere_a = new Sphere (1., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1, vector3d (4., 0., 0.));
sphere_b->setVelocity (vector3d (1., 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (0, result);
delete sphere_a;
delete sphere_b;
}
TEST ( CheckSphereSphereCollision ) {
Shape* sphere_a = new Sphere (1., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1, vector3d (3., 0., 0.));
sphere_b->setVelocity (vector3d (-1., 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (1, result);
delete sphere_a;
delete sphere_b;
}
TEST ( CheckSphereSphereCollisionCheckOrder ) {
Shape* sphere_a = new Sphere (1., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1, vector3d (3., 0., 0.));
sphere_a->setVelocity (vector3d (1., 0., 0.));
sphere_b->setVelocity (vector3d (-1.5, 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (1, result);
vector3d normal = info.normal;
result = check_collision_sphere_sphere (1., sphere_b, sphere_a, &info);
CHECK_EQUAL (1, result);
CHECK ( normal[0] == -info.normal[0]);
CHECK ( normal[1] == -info.normal[1]);
CHECK ( normal[2] == -info.normal[2]);
delete sphere_a;
delete sphere_b;
}
TEST ( CheckSphereSphereNoCollision ) {
Shape* sphere_a = new Sphere (1., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1, vector3d (3., 0., 0.));
sphere_b->setVelocity (vector3d (-0.9, 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (0, result);
delete sphere_a;
delete sphere_b;
}
TEST ( CheckSphereSphereCollisionResult ) {
Shape* sphere_a = new Sphere (1., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1, vector3d (2.5, 0., 0.));
sphere_b->setVelocity (vector3d (-1, 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
delete sphere_a;
delete sphere_b;
}
TEST ( CheckSphereSphereMovingNonHorizontal ) {
Shape* sphere_a = new Sphere (3., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1.5, vector3d (3., 0., 4.));
sphere_a->setVelocity (vector3d (3./5., 0., 4./5.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
vector3d result_normal = vector3d (-3., 0., -4.).normalize();
vector3d collision_point = result_normal * -0.5;
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
CHECK_EQUAL (1, info.reference_shape);
CHECK (result_normal == info.normal);
CHECK (collision_point == info.point);
sphere_a->setVelocity (vector3d (0., 0., 0.));
sphere_b->setVelocity (vector3d (-3./5., 0., -4./5.));
result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
delete sphere_a;
delete sphere_b;
}
/* If there is one of the sphere non moving we must ensure that we
* set the moving one as the incidence shape.
*/
TEST ( CheckSphereSphereReferenceShape ) {
Shape* sphere_a = new Sphere (3., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1.5, vector3d (3., 0., 4.));
sphere_a->setVelocity (vector3d (3./5., 0., 4./5.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
vector3d result_normal = vector3d (-3., 0., -4.).normalize();
vector3d collision_point = result_normal * 0.5;
CHECK_EQUAL (1, result);
CHECK_EQUAL (0.5, info.time);
CHECK (result_normal == info.normal);
CHECK_EQUAL (1, info.reference_shape);
sphere_a->setVelocity (vector3d (0., 0., 0.));
sphere_b->setVelocity (vector3d (-3./5., 0., -4./5.));
result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (1, result);
CHECK (result_normal * -1. == info.normal);
CHECK_EQUAL (0.5, info.time);
CHECK_EQUAL (0, info.reference_shape);
delete sphere_a;
delete sphere_b;
}
TEST ( CheckSphereSphereIgnoreHeight ) {
Shape* sphere_a = new Sphere (1., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1, vector3d (3., 0., 0.));
sphere_b->setVelocity (vector3d (-2., 0., 0.));
sphere_a->setPosition (vector3d (0., 10., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (1, result);
delete sphere_a;
delete sphere_b;
}
TEST ( CheckSphereSphereCollisionTimestep ) {
Shape* sphere_a = new Sphere (1., vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (1, vector3d (3., 0., 0.));
sphere_b->setVelocity (vector3d (-1., 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (1, result);
CHECK_EQUAL (1, info.time);
result = check_collision_sphere_sphere (2., sphere_a, sphere_b, &info);
CHECK_EQUAL (1., result);
CHECK_EQUAL (1., info.time);
result = check_collision_sphere_sphere (0.5, sphere_a, sphere_b, &info);
CHECK_EQUAL (0, result);
delete sphere_a;
delete sphere_b;
}
TEST ( CheckPerpendicularMovement ) {
Shape* sphere_a = new Sphere (0.4, vector3d (0., 0., 0.));
Shape* sphere_b = new Sphere (0.4, vector3d (0., 0., -1.));
sphere_a->setVelocity (vector3d (0., 0., 0.));
sphere_b->setVelocity (vector3d (5., 0., 0.));
CollisionInfo info;
int result = check_collision_sphere_sphere (1., sphere_a, sphere_b, &info);
CHECK_EQUAL (0, result);
delete sphere_a;
delete sphere_b;
}