Antonios Gogios
Software Engineer
3D Math & Collision Detection Library Demo
2012
You can get the source code for this demo here. Compile and run to see the demo output, or modify it to test the library yourself.
This is a demo of a 3D math utilities library I have written, maintained and expanded since 2010. It is fully portable, written in standard C++ with no additional dependencies. It allows linear algebra operations such as 3D vector and 4x4 square matrix operations, and contains various utility functions such as orthonormal basis construction, concatenation of 3D transformations, etc. . The library also provides extensive 3D collision detection functionality, currently allowing static collision detection using a variety of parametric volume types such as AABBs, OBBs, spheres, capsules, planes -- as well as arbitrary triangle meshes.
This demo demonstrates some of this functionality through performing some 3D transformations and collision detection and outputing the results in a command-line window.
Also, for those interested in a more detailed explanation of the calculations performed by the demo, an explanatory overview accompanied by images is shown below.
Illustrated Overview
Compile and run the source code, then follow along with the command-line output of the demo. Initially, three 3D vectors are created:
vectorA = (1, 2, 3)
vectorB = (4, 5, 6)
vectorC = vectorA + vectorB = (5, 7, 9)
vectorC vector length = 12.4499


matrixA is a 4x4 rotation matrix constructed using Rx=10, Ry=10, Rz = 10 degrees.
vectorC is treated as a contravariant vector, transformed in the same way as coordinates.
So vectorC' is the result of the transformation by matrixA, i.e:
vectorC' = matrixA * VectorC4D = (5.64712, 6.4088, 9.05745, 0)
vectorC' vector length = 12.4499
(Meaning that its length is retained after the transformation)

Subsequently, some sphere collision volumes are created and some static collision tests are performed.
sphereA: Center = (1, 2, 3), Radius = 2
sphereB: Center = (4, 5, 6), Radius = 0.5
sphereC: Center = (5, 7, 9), Radius = 8
Collision test: sphereA and sphereB. Spheres do not collide, the test returns false.
Collision test: sphereA and sphereC. Spheres collide, the test returns true.


An oriented bounding box is created: OBB_A.
It's orientation is determined by using matrixA (i.e. it is rotated 10 degrees in each local axis).
The local axes are initially aligned to the world axes. Due to covariance, the inverse transpose of matrixA is computed, and is used for transforming the local axes of OBB_A:
for(unsigned int i=0;i<3;++i)
{OBB_A_LocalAxes[i] = matrixAInverseTranspose * OBB_A_LocalAxes[i];}
Note: Since this particular example uses only rotations, covariance does not behave differently, because matrixA is orthogonal (and matrixAInverseTranspose is equal to matrixA). However if other types of transformations are used it does, so this is the proper method with which to treat covariant vectors' transformations.
At this point, the now-transformed local axes of OBB_A are tested for being orthogonal. Mathematically, for two vectors to be orthogonal, their dot product must be equal to 0. Due to rounding errors potentially caused by floating point arithmetic, the axes' dot products are tested using a constant representing the upper bound of the relative error, and which is called machine epsilon. If the dot products' absolute values are <= epsilon, then the vectors compared are essentially orthogonal, as is the case for OBB_A's local axes.


Having created OBB_A, and tested the geometrical integrity of it's members, a collision test is performed with sphereA:
The OBB collides with the sphere, the test returns true.
This concludes the few calculations showcased in the demo.
