libccd is library for a collision detection between two convex shapes. libccd implements variation on Gilbert–Johnson–Keerthi algorithm plus Expand Polytope Algorithm (EPA) and also implements algorithm Minkowski Portal Refinement (MPR, a.k.a. XenoCollide) as described in Game Programming Gems 7.
libccd is the only available open source library of my knowledge that include MPR algorithm working in 3-D space. However, there is a library called mpr2d, implemented in D programming language, that works in 2-D space.
libccd is currently part of:
For implementation details on GJK algorithm, see http://www.win.tue.nl/~gino/solid/jgt98convex.pdf.
This library is currently based only on standard libraries. The only exception are testsuites that are built on top of CU (https://github.com/danfis/cu) library licensed under LGPL, however only testing depends on it and libccd library itself can be distributed without it.
libccd is licensed under OSI-approved 3-clause BSD License, text of license is distributed along with source code in BSD-LICENSE file. Each file should include license notice, the rest should be considered as licensed under 3-clause BSD License.
libccd contains several mechanisms for compiling and installing. Using a simple Makefile, using autotools, and using CMake.
Directory src/ contains Makefile that should contain everything needed for compilation and installation:
$ cd src/
$ make
$ make install
Library libccd is by default compiled in double precision of floating point numbers - you can change this by options USE_SINGLE/USE_DOUBLE, i.e.:
$ make USE_SINGLE=yes
will compile library in single precision.
Installation directory can be changed by options PREFIX, INCLUDEDIR and LIBDIR. For more info type 'make help'.
libccd also contains support for autotools: Generate configure script etc.:
$ ./bootstrap
Create new build/ directory:
$ mkdir build && cd build
Run configure script:
$ ../configure
Run make and make install:
$ make && make install
configure script can change the way libccd is compiled and installed, most significant option is --enable-double-precision which enables double precision (single is default in this case).
To build using make
:
$ mkdir build && cd build
$ cmake -G "Unix Makefiles" ..
$ make && make install
To build using ninja
:
$ mkdir build && cd build
$ cmake -G Ninja ..
$ ninja && ninja install
Other build tools may be using by specifying a different generator. For example:
$ cmake -G Xcode ..
> cmake -G "Visual Studio 14 2015" ..
To compile using double precision, set the ENABLE_DOUBLE_PRECISION
option:
$ mkdir build && cd build
$ cmake -G "Unix Makefiles" -DENABLE_DOUBLE_PRECISION=ON ..
$ make && make install
To build libccd as a shared library, set the BUILD_SHARED_LIBS
option:
$ mkdir build && cd build
$ cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=ON ..
$ make && make install
To build the test suite, set the BUILD_TESTING
option:
$ mkdir build && cd build
$ cmake -G "Unix Makefiles" -DBUILD_TESTING=ON ..
$ make && make test
The installation directory may be changed using the CMAKE_INSTALL_PREFIX
variable:
$ mkdir build && cd build
$ cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/path/to/install ..
$ make && make install
This section describes how to use libccd for testing if two convex objects intersects (i.e., 'yes/no' test) using Gilbert-Johnson-Keerthi (GJK) algorithm.
Procedure is very simple (and is similar for usages of library):
Here is skeleton of simple program:
#include <ccd/ccd.h>
#include <ccd/quat.h> // for work with quaternions
/** Support function for box */
void support(const void *obj, const ccd_vec3_t *dir, ccd_vec3_t *vec)
{
// assume that obj_t is user-defined structure that holds info about
// object (in this case box: x, y, z, pos, quat - dimensions of box,
// position and rotation)
obj_t *obj = (obj_t *)_obj;
ccd_vec3_t dir;
ccd_quat_t qinv;
// apply rotation on direction vector
ccdVec3Copy(&dir, _dir);
ccdQuatInvert2(&qinv, &obj->quat);
ccdQuatRotVec(&dir, &qinv);
// compute support point in specified direction
ccdVec3Set(v, ccdSign(ccdVec3X(&dir)) * box->x * CCD_REAL(0.5),
ccdSign(ccdVec3Y(&dir)) * box->y * CCD_REAL(0.5),
ccdSign(ccdVec3Z(&dir)) * box->z * CCD_REAL(0.5));
// transform support point according to position and rotation of object
ccdQuatRotVec(v, &obj->quat);
ccdVec3Add(v, &obj->pos);
}
int main(int argc, char *argv[])
{
...
ccd_t ccd;
CCD_INIT(&ccd); // initialize ccd_t struct
// set up ccd_t struct
ccd.support1 = support; // support function for first object
ccd.support2 = support; // support function for second object
ccd.max_iterations = 100; // maximal number of iterations
int intersect = ccdGJKIntersect(obj1, obj2, &ccd);
// now intersect holds true if obj1 and obj2 intersect, false otherwise
}
If you want to obtain also penetration info about two intersection objects ccdGJKPenetration() function can be used.
Procedure is almost same as for previous case:
#include <ccd/ccd.h>
#include <ccd/quat.h> // for work with quaternions
/** Support function is same as in previous case */
int main(int argc, char *argv[])
{
...
ccd_t ccd;
CCD_INIT(&ccd); // initialize ccd_t struct
// set up ccd_t struct
ccd.support1 = support; // support function for first object
ccd.support2 = support; // support function for second object
ccd.max_iterations = 100; // maximal number of iterations
ccd.epa_tolerance = 0.0001; // maximal tolerance fro EPA part
ccd_real_t depth;
ccd_vec3_t dir, pos;
int intersect = ccdGJKPenetration(obj1, obj2, &ccd, &depth, &dir, &pos);
// now intersect holds 0 if obj1 and obj2 intersect, -1 otherwise
// in depth, dir and pos is stored penetration depth, direction of
// separation vector and position in global coordinate system
}
libccd also provides MPR - Minkowski Portal Refinement algorithm that can be used for testing if two objects intersects.
Procedure is similar to the one used for GJK algorithm. Support function is same but also function that returns center (or any point near center) of given object must be implemented:
#include <ccd/ccd.h>
#include <ccd/quat.h> // for work with quaternions
/** Support function is same as in previous case */
/** Center function - returns center of object */
void center(const void *_obj, ccd_vec3_t *center)
{
obj_t *obj = (obj_t *)_obj;
ccdVec3Copy(center, &obj->pos);
}
int main(int argc, char *argv[])
{
...
ccd_t ccd;
CCD_INIT(&ccd); // initialize ccd_t struct
// set up ccd_t struct
ccd.support1 = support; // support function for first object
ccd.support2 = support; // support function for second object
ccd.center1 = center; // center function for first object
ccd.center2 = center; // center function for second object
ccd.mpr_tolerance = 0.0001; // maximal tolerance
int intersect = ccdMPRIntersect(obj1, obj2, &ccd);
// now intersect holds true if obj1 and obj2 intersect, false otherwise
}
Using MPR algorithm for obtaining penetration info about two intersection objects is equally easy as in previous case instead ccdMPRPenetration() function is used:
#include <ccd/ccd.h>
#include <ccd/quat.h> // for work with quaternions
/** Support function is same as in previous case */
/** Center function is same as in prevous case */
int main(int argc, char *argv[])
{
...
ccd_t ccd;
CCD_INIT(&ccd); // initialize ccd_t struct
// set up ccd_t struct
ccd.support1 = support; // support function for first object
ccd.support2 = support; // support function for second object
ccd.center1 = center; // center function for first object
ccd.center2 = center; // center function for second object
ccd.mpr_tolerance = 0.0001; // maximal tolerance
ccd_real_t depth;
ccd_vec3_t dir, pos;
int intersect = ccdMPRPenetration(obj1, obj2, &ccd, &depth, &dir, &pos);
// now intersect holds 0 if obj1 and obj2 intersect, -1 otherwise
// in depth, dir and pos is stored penetration depth, direction of
// separation vector and position in global coordinate system
}