### Compile and Install using Makefile Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Navigate to the src directory, compile the library, and install it. Use 'make install' to install. Options like USE_SINGLE=yes can change precision. ```bash cd src/ make make install ``` ```bash make USE_SINGLE=yes ``` -------------------------------- ### Install HTML Documentation Source: https://github.com/danfis/libccd/blob/master/doc/CMakeLists.txt Installs the generated HTML documentation to the system's data root directory under 'doc/ccd'. ```cmake install(DIRECTORY "${CCD_HTML_OUTPUT_DIR}" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/doc/ccd") ``` -------------------------------- ### Install CMake to Custom Prefix Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Configure the build with CMake, specifying a custom installation directory using CMAKE_INSTALL_PREFIX. Then, build and install. ```bash mkdir build && cd build cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/path/to/install .. make && make install ``` -------------------------------- ### Install libccd using CMake Source: https://github.com/danfis/libccd/blob/master/_autodocs/README.md Build and install libccd using CMake by creating a build directory, configuring with CMake, and then compiling and installing. ```sh mkdir build && cd build cmake .. make && make install ``` -------------------------------- ### Install libccd using Makefile Source: https://github.com/danfis/libccd/blob/master/_autodocs/README.md Install libccd by navigating to the src directory and running make commands. This method is suitable for quick local builds. ```sh cd src make make install ``` -------------------------------- ### ccd_t Initialization Example Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/ccd-main-api.md Demonstrates how to initialize the `ccd_t` structure and set required support functions and maximum iterations. ```c ccd_t ccd; CCD_INIT(&ccd); ccd.support1 = my_obj1_support; ccd.support2 = my_obj2_support; ccd.max_iterations = 100; ``` -------------------------------- ### Install libccd Library and Headers Source: https://github.com/danfis/libccd/blob/master/src/CMakeLists.txt Installs the libccd library, headers, and related files to the appropriate system directories based on CMake installation variables. ```cmake install(TARGETS ccd EXPORT ccd-targets ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ccd" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") install(EXPORT ccd-targets DESTINATION "${CMAKE_INSTALL_LIBDIR}/ccd") ``` -------------------------------- ### Build with CMake and Ninja Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Create a build directory, configure the project using CMake with the 'Ninja' generator, and then build and install. ```bash mkdir build && cd build cmake -G Ninja .. ninja && ninja install ``` -------------------------------- ### Configure and Build with Autotools Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Create a build directory, run the configure script, and then compile and install the library. The --enable-double-precision option can be used. ```bash mkdir build && cd build ../configure make && make install ``` -------------------------------- ### Build with CMake and Unix Makefiles Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Create a build directory, configure the project using CMake with the 'Unix Makefiles' generator, and then build and install. ```bash mkdir build && cd build cmake -G "Unix Makefiles" .. make && make install ``` -------------------------------- ### Complete Sphere Collision Detection Example Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/quat-api.md Demonstrates creating two spheres with different rotations and performing collision detection using the GJK algorithm. This example requires the libccd library and its associated headers. ```c #include #include #include typedef struct { ccd_vec3_t center; ccd_quat_t rotation; ccd_real_t radius; } sphere_t; void sphere_support(const void *obj, const ccd_vec3_t *dir, ccd_vec3_t *vec) { sphere_t *s = (sphere_t *)obj; ccd_vec3_t normalized_dir; ccdVec3Copy(&normalized_dir, dir); ccdVec3Normalize(&normalized_dir); ccdVec3Copy(vec, &normalized_dir); ccdVec3Scale(vec, s->radius); ccdVec3Add(vec, &s->center); } void sphere_center(const void *obj, ccd_vec3_t *center) { sphere_t *s = (sphere_t *)obj; ccdVec3Copy(center, &s->center); } int main() { sphere_t sphere1, sphere2; ccd_t ccd; ccd_vec3_t axis; // Create first sphere at origin ccdVec3Set(&sphere1.center, 0.0, 0.0, 0.0); ccdQuatSet(&sphere1.rotation, 0.0, 0.0, 0.0, 1.0); // identity sphere1.radius = 1.0; // Create second sphere with 45-degree rotation ccdVec3Set(&sphere2.center, 1.5, 0.0, 0.0); ccdVec3Set(&axis, 0.0, 1.0, 0.0); ccdQuatSetAngleAxis(&sphere2.rotation, 3.14159 / 4.0, &axis); sphere2.radius = 0.5; // Set up collision detection CCD_INIT(&ccd); ccd.support1 = sphere_support; ccd.support2 = sphere_support; ccd.center1 = sphere_center; ccd.center2 = sphere_center; // Test collision if (ccdGJKIntersect(&sphere1, &sphere2, &ccd)) { printf("Spheres are colliding!\n"); } return 0; } ``` -------------------------------- ### Example Sphere Center Function Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/ccd-main-api.md An example implementation of a center function for a sphere object, returning its position. ```c void sphere_center(const void *obj, ccd_vec3_t *center) { sphere_t *s = (sphere_t *)obj; ccdVec3Copy(center, &s->position); } ``` -------------------------------- ### Build CMake Shared Library Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Configure the build with CMake, enabling shared library creation by setting BUILD_SHARED_LIBS=ON. Then, build and install. ```bash mkdir build && cd build cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=ON .. make && make install ``` -------------------------------- ### Build with CMake and Double Precision Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Configure the build with CMake, enabling double precision by setting ENABLE_DOUBLE_PRECISION=ON. Then, build and install. ```bash mkdir build && cd build cmake -G "Unix Makefiles" -DENABLE_DOUBLE_PRECISION=ON .. make && make install ``` -------------------------------- ### Static Initialization of ccd_quat_t Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/quat-api.md Demonstrates how to statically initialize a `ccd_quat_t` variable using the `CCD_QUAT` macro. This example shows a 90-degree rotation around the z-axis. ```c // Static initialization: CCD_QUAT(name, x, y, z, w) CCD_QUAT(myquat, 0.0, 0.0, 0.707, 0.707); // 90-degree rotation around z-axis ``` -------------------------------- ### Example Box Support Function Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/ccd-main-api.md An example implementation of a support function for a box object. It calculates the furthest point on the box in a given direction, considering its rotation and center. ```c void box_support(const void *obj, const ccd_vec3_t *dir, ccd_vec3_t *vec) { obj_t *box = (obj_t *)obj; ccd_vec3_t rotated_dir; ccdVec3Copy(&rotated_dir, dir); ccdQuatRotVec(&rotated_dir, &box->inverse_quat); ccdVec3Set(vec, ccdSign(ccdVec3X(&rotated_dir)) * box->half_extents.x, ccdSign(ccdVec3Y(&rotated_dir)) * box->half_extents.y, ccdSign(ccdVec3Z(&rotated_dir)) * box->half_extents.z); ccdQuatRotVec(vec, &box->quat); ccdVec3Add(vec, &box->center); } ``` -------------------------------- ### Configure CMake with Xcode Generator Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Example of configuring a CMake project to use the Xcode build system. ```bash cmake -G Xcode .. ``` -------------------------------- ### Set Public Include Directories Source: https://github.com/danfis/libccd/blob/master/src/CMakeLists.txt Configures the public include directories for the ccd target, making headers available during build and install. ```cmake target_include_directories(ccd PUBLIC $ $) ``` -------------------------------- ### Configure CMake with Visual Studio 2015 Generator Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Example of configuring a CMake project to use the Visual Studio 2015 build system. ```batch cmake -G "Visual Studio 14 2015" .. ``` -------------------------------- ### Configuring Collision Detection Tolerances Source: https://github.com/danfis/libccd/blob/master/_autodocs/errors.md These examples show how to adjust the EPA and MPR tolerances, as well as the maximum number of iterations, for different collision detection precision needs, from fast and rough to precise simulations. ```c // For fast, rough collision detection (game physics) ccd.epa_tolerance = 1e-3; // Loose tolerance ccd.mpr_tolerance = 1e-3; ccd.max_iterations = 50; // Limit iterations // For precise collision detection (simulation) ccd.epa_tolerance = 1e-6; // Tight tolerance ccd.mpr_tolerance = 1e-6; ccd.max_iterations = 1000; // Allow more iterations // For balanced performance ccd.epa_tolerance = 1e-4; // Default ccd.mpr_tolerance = 1e-4; ``` -------------------------------- ### Find Sphinx Executable Source: https://github.com/danfis/libccd/blob/master/doc/CMakeLists.txt Searches for the Sphinx executable and fails the build if it's not found. Ensure Sphinx is installed and in your system's PATH. ```cmake find_program(SPHINX_EXECUTABLE NAMES sphinx-build sphinx-build2) if(NOT SPHINX_EXECUTABLE) message(FATAL_ERROR "Could NOT find required executable sphinx-build") endif() ``` -------------------------------- ### Build CMake Test Suite Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Configure the build with CMake, enabling the test suite by setting BUILD_TESTING=ON. Then, build and run tests. ```bash mkdir build && cd build cmake -G "Unix Makefiles" -DBUILD_TESTING=ON .. make && make test ``` -------------------------------- ### Configure Collision Detection with libccd Source: https://github.com/danfis/libccd/blob/master/_autodocs/types.md Demonstrates the initialization and configuration of the `ccd_t` structure for collision detection. It assigns custom support and center functions for two box objects and initiates a GJK penetration test. ```c // Configure collision detection int main() { box_t box1, box2; ccd_t ccd; // Initialize and configure CCD_INIT(&ccd); ccd.support1 = box_support; ccd.support2 = box_support; ccd.center1 = box_center; ccd.center2 = box_center; ccd_real_t depth; ccd_vec3_t dir, pos; if (ccdGJKPenetration(&box1, &box2, &ccd, &depth, &dir, &pos) == 0) { // Process collision: depth (ccd_real_t), dir and pos (ccd_vec3_t) } return 0; } ``` -------------------------------- ### Bootstrap Autotools Source: https://github.com/danfis/libccd/blob/master/doc/compile-and-install.md Generate the configure script for building with Autotools. This is the first step before creating a build directory. ```bash ./bootstrap ``` -------------------------------- ### CCD Vec3 Initialization Macros Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/vec3-api.md Provides macros for initializing `ccd_vec3_t` variables, supporting both static and variable declaration with initial values. ```c // Static initialization ccd_vec3_t v = CCD_VEC3_STATIC(1.0, 2.0, 3.0); // Variable declaration with initialization CCD_VEC3(myvar, 1.0, 2.0, 3.0); // declares ccd_vec3_t myvar = {1.0, 2.0, 3.0} ``` -------------------------------- ### Find and Link Math Library (Unix) Source: https://github.com/danfis/libccd/blob/master/src/CMakeLists.txt Finds the math library (libm) on Unix-like systems and links it to the ccd target. If not found, it raises a fatal error. ```cmake if(NOT WIN32) find_library(LIBM_LIBRARY NAMES m) if(NOT LIBM_LIBRARY) message(FATAL_ERROR "Could NOT find required library LibM") endif() target_link_libraries(ccd "${LIBM_LIBRARY}") if(BUILD_SHARED_LIBS) set(CCD_PKGCONFIG_EXTRA_LIBS -lm PARENT_SCOPE) endif() endif() ``` -------------------------------- ### Manage Documentation Cleanup Files Source: https://github.com/danfis/libccd/blob/master/doc/CMakeLists.txt Configures the build system to clean generated documentation files (doctrees and HTML output) when 'make clean' is invoked. This includes directories for both HTML and man pages if they are generated. ```cmake set(CCD_DOC_ADDITIONAL_MAKE_CLEAN_FILES "${CCD_DOCTREE_DIR}" "${CCD_HTML_OUTPUT_DIR}") set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CCD_DOC_ADDITIONAL_MAKE_CLEAN_FILES}) ``` -------------------------------- ### Get Support Function Pointer Source: https://github.com/danfis/libccd/blob/master/_autodocs/algorithms-and-patterns.md Returns the appropriate support function pointer based on the shape type. Used for type dispatch in collision detection. ```c typedef enum { SHAPE_BOX, SHAPE_SPHERE, SHAPE_CYLINDER } shape_type_t; typedef struct { shape_type_t type; void *data; } shape_t; typedef void (*support_fn)(const void *, const ccd_vec3_t *, ccd_vec3_t *); support_fn get_support_fn(shape_type_t type) { switch (type) { case SHAPE_BOX: return box_support; case SHAPE_SPHERE: return sphere_support; case SHAPE_CYLINDER: return cylinder_support; } return NULL; } ``` -------------------------------- ### Initialize ccd_t Structure with Defaults Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Initializes a ccd_t structure using the CCD_INIT() macro, setting all fields to their default values. Custom values can then be overridden. ```c ccd_t ccd; CCD_INIT(&ccd); // Initialize with defaults // Override defaults as needed ccd.support1 = my_object1_support; ccd.support2 = my_object2_support; ccd.epa_tolerance = 0.001; ``` -------------------------------- ### Build and Test libccd Source: https://github.com/danfis/libccd/blob/master/_autodocs/README.md Instructions for building and running tests for libccd using CMake and Make. Ensure testing is enabled during the CMake configuration step. ```sh cmake -DBUILD_TESTING=ON .. make test ``` -------------------------------- ### Custom Initial Direction for CCD Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Define and set a custom function for the initial direction in collision detection. This example uses the vector between object centers as the initial direction. ```c void initial_direction(const void *obj1, const void *obj2, ccd_vec3_t *dir) { // Get object positions object_t *o1 = (object_t *)obj1; object_t *o2 = (object_t *)obj2; // Use vector from obj1 to obj2 as initial direction ccdVec3Sub2(dir, &o2->center, &o1->center); ccdVec3Normalize(dir); } ccd_t ccd; CCD_INIT(&ccd); cod.first_dir = initial_direction; // Custom initial direction cod.support1 = support1; cod.support2 = support2; ``` -------------------------------- ### Transformed Box Support Function Source: https://github.com/danfis/libccd/blob/master/_autodocs/support-functions.md Demonstrates how to implement a support function for a transformed box, including transforming the direction vector to local space, finding the support point, and transforming the result back to world space. ```c void transformed_box_support(const void *_obj, const ccd_vec3_t *dir, ccd_vec3_t *vec) { box_t *box = (box_t *)_obj; // STEP 1: Transform direction to local (object) space ccd_vec3_t local_dir; ccdVec3Copy(&local_dir, dir); // If object has rotation, apply inverse rotation to direction if (has_rotation) { ccd_quat_t rot_inv; ccdQuatInvert2(&rot_inv, &box->rotation); ccdQuatRotVec(&local_dir, &rot_inv); } // STEP 2: Find furthest point in local space ccdVec3Set(vec, ccdSign(ccdVec3X(&local_dir)) * box->half_extents.x, ccdSign(ccdVec3Y(&local_dir)) * box->half_extents.y, ccdSign(ccdVec3Z(&local_dir)) * box->half_extents.z); // STEP 3: Transform result back to world space if (has_rotation) { ccdQuatRotVec(vec, &box->rotation); } ccdVec3Add(vec, &box->center); // Apply translation } ``` -------------------------------- ### Get Sign of Scalar Value Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/vec3-api.md Use ccdSign to determine the sign of a scalar value, considering an epsilon tolerance for near-zero values. Returns -1 for negative, 0 for near-zero, and 1 for positive. ```c _ccd_inline int ccdSign(ccd_real_t val); ``` ```c int sign = ccdSign(-3.5f); // returns -1 int sign = ccdSign(0.00000001f); // returns 0 int sign = ccdSign(2.5f); // returns 1 ``` -------------------------------- ### Good Support Function: Zero Direction Handling Source: https://github.com/danfis/libccd/blob/master/_autodocs/support-functions.md Demonstrates a correct implementation that ensures a valid point is always returned, even for a zero direction vector, by copying the object's center. ```c void good_support(const void *_obj, const ccd_vec3_t *dir, ccd_vec3_t *vec) { sphere_t *obj = (sphere_t *)_obj; // GOOD: Always fill vec, even for zero direction if (ccdIsZero(ccdVec3Len2(dir))) { ccdVec3Copy(vec, &obj->center); return; } // ... rest of code } ``` -------------------------------- ### CCD_INIT Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/ccd-main-api.md Initializes a ccd_t structure with default values. This macro is used to set up the configuration for CCD operations. ```APIDOC ## CCD_INIT ### Description Macro to initialize a `ccd_t` structure with default values. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Method Macro ### Endpoint N/A ### Parameters - **ccd** (ccd_t *) - Required - Pointer to the `ccd_t` structure to be initialized. ### Return None (initializes the structure in place). ``` -------------------------------- ### Bad Support Function: Zero Direction Handling Source: https://github.com/danfis/libccd/blob/master/_autodocs/support-functions.md Illustrates an incorrect implementation that fails to return a valid point when the input direction is zero, leading to uninitialized memory. ```c void bad_support(const void *_obj, const ccd_vec3_t *dir, ccd_vec3_t *vec) { sphere_t *obj = (sphere_t *)_obj; // BAD: Returns without filling vec if direction is zero if (ccdIsZero(ccdVec3Len2(dir))) { return; // UNINITIALIZED MEMORY! } // ... rest of code } ``` -------------------------------- ### Consistent Support Function Implementation Source: https://github.com/danfis/libccd/blob/master/_autodocs/support-functions.md Shows a correct implementation of a support function that ensures deterministic results by consistently using the object's center and radius. ```c void consistent_support(const void *_obj, const ccd_vec3_t *dir, ccd_vec3_t *vec) { sphere_t *obj = (sphere_t *)_obj; // GOOD: Always uses object's center and radius ccd_vec3_t normalized; ccdVec3Copy(&normalized, dir); ccdVec3Normalize(&normalized); ccdVec3Scale(&normalized, obj->radius); ccdVec3Copy(vec, &obj->center); ccdVec3Add(vec, &normalized); } ``` -------------------------------- ### Initialization Functions Source: https://github.com/danfis/libccd/blob/master/_autodocs/INDEX.md Functions for initializing the collision detection structure and setting default initial directions. ```APIDOC ## CCD_INIT ### Description Initializes a `ccd_t` structure with default values. ### Signature `CCD_INIT(ccd)` ### Parameters - `ccd`: Pointer to the `ccd_t` structure to initialize. ``` ```APIDOC ## ccdFirstDirDefault ### Description Sets a default initial direction for collision detection between two objects. ### Signature `ccdFirstDirDefault(o1, o2, dir)` ### Parameters - `o1`: First object. - `o2`: Second object. - `dir`: Output parameter to store the default initial direction. ``` -------------------------------- ### Initialize and Run GJK Intersection Test Source: https://github.com/danfis/libccd/blob/master/README.md This snippet shows how to initialize the ccd_t structure, set up support functions and maximum iterations, and then call ccdGJKIntersect to determine if two objects intersect. ```cpp #include #include // 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 } ``` -------------------------------- ### libccd Cache Optimization Prefetch Hints Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Provides prefetch hints for GCC compilers to optimize cache usage. `_ccd_prefetch` is for read operations, and `_ccd_prefetchw` is for write operations. These are automatically disabled on non-GCC compilers. ```c _ccd_prefetch(ptr) // Prefetch for read _ccd_prefetchw(ptr) // Prefetch for write ``` -------------------------------- ### Configure libccd with Double Precision (Autotools) Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Enables double-precision floating-point support during the configuration step when using Autotools. ```shell ./configure --enable-double-precision make && make install ``` -------------------------------- ### Initialize CCD Structure Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/ccd-main-api.md Initializes a ccd_t structure with default values. Custom support or center functions can be assigned after initialization. ```c #define CCD_INIT(ccd) \ do { \ (ccd)->first_dir = ccdFirstDirDefault; \ (ccd)->support1 = NULL; \ (ccd)->support2 = NULL; \ (ccd)->center1 = NULL; \ (ccd)->center2 = NULL; \ (ccd)->max_iterations = (unsigned long)-1; \ (ccd)->epa_tolerance = CCD_REAL(0.0001); \ (ccd)->mpr_tolerance = CCD_REAL(0.0001); \ (ccd)->dist_tolerance = CCD_REAL(1E-6); \ } while(0) ``` ```c ccd_t ccd; CCD_INIT(&ccd); cod.support1 = my_support_function; ``` -------------------------------- ### Define Library Includes and Sources Source: https://github.com/danfis/libccd/blob/master/src/CMakeLists.txt Sets the include directories and source files for the libccd library. ```cmake set(CCD_INCLUDES ccd/ccd.h ccd/compiler.h ccd/ccd_export.h ccd/quat.h ccd/vec3.h "${CMAKE_CURRENT_BINARY_DIR}/ccd/config.h") set(CCD_SOURCES alloc.h ccd.c dbg.h list.h mpr.c polytope.c polytope.h simplex.h support.c support.h vec3.c) ``` -------------------------------- ### Configure Header Generation Source: https://github.com/danfis/libccd/blob/master/src/CMakeLists.txt Generates the config.h header file from a template using CMake's configure_file command. ```cmake configure_file(ccd/config.h.cmake.in ccd/config.h) ``` -------------------------------- ### CCD_INIT Source: https://github.com/danfis/libccd/blob/master/doc/reference.md Initializes a ccd_t structure with default values for collision detection parameters. ```APIDOC ## CCD_INIT ### Description Initializes a `ccd_t` structure with default values for collision detection parameters, including support functions, iteration limits, and tolerances. ### Macro ```c #define CCD_INIT(ccd) ``` ### Parameters - **ccd** (ccd_t *) - Pointer to the `ccd_t` structure to be initialized. ``` -------------------------------- ### Inconsistent Support Function Implementation Source: https://github.com/danfis/libccd/blob/master/_autodocs/support-functions.md Illustrates an incorrect implementation that violates determinism by using a random number to calculate the radius, leading to unpredictable results. ```c void inconsistent_support(const void *_obj, const ccd_vec3_t *dir, ccd_vec3_t *vec) { sphere_t *obj = (sphere_t *)_obj; // BAD: Uses random number for radius ccd_real_t radius = obj->radius + (rand() % 10) * 0.001; ccd_vec3_t normalized; ccdVec3Copy(&normalized, dir); ccdVec3Normalize(&normalized); ccdVec3Scale(&normalized, radius); ccdVec3Copy(vec, &obj->center); ccdVec3Add(vec, &normalized); } ``` -------------------------------- ### GJK+EPA for Penetration Depth Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Configure GJK with EPA to find the penetration depth, direction, and position between two objects. Requires support functions for each object. ```c ccd_t ccd; CCD_INIT(&ccd); cod.support1 = cylinder_support; cod.support2 = cylinder_support; ccd_real_t depth; ccd_vec3_t dir, pos; int result = ccdGJKPenetration(cyl1, cyl2, &ccd, &depth, &dir, &pos); if (result == 0) { printf("Penetration depth: %f\n", depth); } ``` -------------------------------- ### Configure Man Page Documentation Target (Unix-like) Source: https://github.com/danfis/libccd/blob/master/doc/CMakeLists.txt Defines a custom target 'man' to build man pages using Sphinx, specifically for non-Windows systems. It specifies the output directory for man pages and adds it as a dependency to the 'doc' target. This target is conditionally compiled. ```cmake if(NOT WIN32) set(CCD_MAN_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/man") add_custom_target(man COMMAND "${SPHINX_EXECUTABLE}" -b man -d "${CCD_DOCTREE_DIR}" -q "${CMAKE_CURRENT_SOURCE_DIR}" "${CCD_MAN_OUTPUT_DIR}") add_dependencies(doc man) install(DIRECTORY "${CCD_MAN_OUTPUT_DIR}/" DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") list(APPEND CCD_DOC_ADDITIONAL_MAKE_CLEAN_FILES "${CCD_MAN_OUTPUT_DIR}") endif() ``` -------------------------------- ### Initialize CCD Structure Source: https://github.com/danfis/libccd/blob/master/doc/reference.md Initializes a ccd_t structure with default values for collision detection parameters. This macro should be used before passing the structure to any ccd functions. ```c #define CCD_INIT(ccd) \ do { \ (ccd)->first_dir = ccdFirstDirDefault; \ (ccd)->support1 = NULL; \ (ccd)->support2 = NULL; \ (ccd)->center1 = NULL; \ (ccd)->center2 = NULL; \ \ (ccd)->max_iterations = (unsigned long)-1; \ (ccd)->epa_tolerance = CCD_REAL(0.0001); \ (ccd)->mpr_tolerance = CCD_REAL(0.0001); \ (ccd)->dist_tolerance = CCD_REAL(1E-6); \ } while(0) ``` -------------------------------- ### Compile libccd with Single Precision (Makefile) Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Compiles libccd using single-precision floating-point numbers, which is the default behavior when using the Makefile. ```shell cd src make ``` -------------------------------- ### Default Values Expanded by CCD_INIT Macro Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Shows the default values assigned to each field of the ccd_t structure when the CCD_INIT() macro is used. ```c CCD_INIT(ccd) expands to: ccd->first_dir = ccdFirstDirDefault; ccd->support1 = NULL; ccd->support2 = NULL; ccd->center1 = NULL; ccd->center2 = NULL; ccd->max_iterations = (unsigned long)-1; // Unlimited ccd->epa_tolerance = CCD_REAL(0.0001); ccd->mpr_tolerance = CCD_REAL(0.0001); ccd->dist_tolerance = CCD_REAL(1E-6); ``` -------------------------------- ### Basic GJK Intersection Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Configure and use the GJK algorithm for basic collision detection between two objects. Requires support functions for each object. ```c ccd_t ccd; CCD_INIT(&ccd); cod.support1 = box_support; cod.support2 = sphere_support; if (ccdGJKIntersect(box, sphere, &ccd)) { printf("Objects collide\n"); } ``` -------------------------------- ### Configure libccd with Double Precision (CMake) Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Enables double-precision floating-point support during the CMake configuration step by setting the ENABLE_DOUBLE_PRECISION flag. ```shell mkdir build && cd build cmake -DENABLE_DOUBLE_PRECISION=ON .. make && make install ``` -------------------------------- ### Test Support Function Source: https://github.com/danfis/libccd/blob/master/_autodocs/support-functions.md Tests a given support function with standard directions and checks for consistency. Ensure the necessary assertions and vector functions are available. ```c void test_support_function(support_fn func, const void *obj) { // Test with standard directions ccd_vec3_t directions[] = { {{1, 0, 0}}, {{-1, 0, 0}}, {{0, 1, 0}}, {{0, -1, 0}}, {{0, 0, 1}}, {{0, 0, -1}}, {{1, 1, 0}}, {{1, 0, 1}}, }; for (int i = 0; i < sizeof(directions)/sizeof(directions[0]); i++) { ccd_vec3_t result; func(obj, &directions[i], &result); // Verify result is filled assert(!isnan(ccdVec3X(&result))); assert(!isnan(ccdVec3Y(&result))); assert(!isnan(ccdVec3Z(&result))); // Verify result is on object surface (approximate check) // This is shape-specific but helps catch bugs } // Test consistency: same input = same output ccd_vec3_t dir, res1, res2; ccdVec3Set(&dir, 1, 2, 3); func(obj, &dir, &res1); func(obj, &dir, &res2); assert(ccdVec3Eq(&res1, &res2)); } ``` -------------------------------- ### Iterating Over Points on Sphere Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/vec3-api.md Shows how to iterate through the `ccd_points_on_sphere` array, which contains points uniformly distributed on a unit sphere. Useful for generating initial directions. ```c for (size_t i = 0; i < ccd_points_on_sphere_len; i++) { ccd_vec3_t dir = ccd_points_on_sphere[i]; // test collision in this direction } ``` -------------------------------- ### Define ccd_t Configuration Structure Source: https://github.com/danfis/libccd/blob/master/_autodocs/types.md The `ccd_t` structure holds all settings and callbacks needed for collision detection algorithms. It includes functions for initial direction, support, and center calculations, along with various tolerance and iteration limits. ```c struct _ccd_t { ccd_first_dir_fn first_dir; ccd_support_fn support1; ccd_support_fn support2; ccd_center_fn center1; ccd_center_fn center2; unsigned long max_iterations; ccd_real_t epa_tolerance; ccd_real_t mpr_tolerance; ccd_real_t dist_tolerance; }; typedef struct _ccd_t ccd_t; ``` -------------------------------- ### Compare Vectors for Equality (C) Source: https://github.com/danfis/libccd/blob/master/_autodocs/api-reference/vec3-api.md Use ccdVec3Eq to check if two ccd_vec3_t vectors are approximately equal, considering a small epsilon tolerance. Returns non-zero if equal, zero otherwise. ```c _ccd_inline int ccdVec3Eq(const ccd_vec3_t *a, const ccd_vec3_t *b); ``` ```c ccd_vec3_t v1, v2; ccdVec3Set(&v1, 1.0, 2.0, 3.0); ccdVec3Set(&v2, 1.0000001, 2.0, 3.0); if (ccdVec3Eq(&v1, &v2)) { printf("Vectors are equal\n"); } ``` -------------------------------- ### Configure HTML Documentation Target Source: https://github.com/danfis/libccd/blob/master/doc/CMakeLists.txt Defines a custom target 'html' to build HTML documentation using Sphinx. It specifies output directories for doctrees and HTML files. This target is a dependency of the 'doc' target. ```cmake set(CCD_DOCTREE_DIR "${CMAKE_CURRENT_BINARY_DIR}/.doctrees") set(CCD_HTML_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/html") add_custom_target(html COMMAND "${SPHINX_EXECUTABLE}" -b html -d "${CCD_DOCTREE_DIR}" -q "${CMAKE_CURRENT_SOURCE_DIR}" "${CCD_HTML_OUTPUT_DIR}") add_dependencies(doc html) ``` -------------------------------- ### Set Maximum Iterations for GJK/MPR Algorithms Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Configure the maximum number of iterations for GJK/MPR algorithms. Use `(unsigned long)-1` for unlimited iterations (default). Limit iterations only if hard real-time guarantees are needed and occasional false negatives are acceptable. ```c ccd.max_iterations = (unsigned long)-1; ``` ```c ccd.max_iterations = 100; ``` ```c ccd.max_iterations = 50; ``` -------------------------------- ### Simple Intersection Test with GJK Source: https://github.com/danfis/libccd/blob/master/_autodocs/algorithms-and-patterns.md Use `ccdGJKIntersect` for a quick yes/no answer on whether two objects are colliding. This pattern requires initialization of the `ccd_t` structure with support functions. ```c int are_colliding(const void *obj1, const void *obj2) { static ccd_t ccd = {0}; static int initialized = 0; if (!initialized) { CCD_INIT(&ccd); ccd.support1 = get_support_function(obj1); ccd.support2 = get_support_function(obj2); initialized = 1; } return ccdGJKIntersect(obj1, obj2, &ccd); } ``` -------------------------------- ### Calculate Penetration Depth with GJK and EPA Source: https://github.com/danfis/libccd/blob/master/README.md Use ccdGJKPenetration to find the penetration depth, direction, and position between two objects. Ensure support functions are correctly implemented and the ccd_t struct is initialized with appropriate parameters like max_iterations and epa_tolerance. ```cpp #include #include // 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 } ``` -------------------------------- ### Implement Support Function for Box Source: https://github.com/danfis/libccd/blob/master/README.md This support function calculates the furthest point on a box in a given direction, considering its position and rotation. It's essential for the GJK algorithm. ```cpp 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); } ``` -------------------------------- ### Build libccd as a Shared Library (CMake) Source: https://github.com/danfis/libccd/blob/master/_autodocs/configuration.md Configures CMake to build libccd as a shared library by setting the BUILD_SHARED_LIBS=ON flag. ```shell cmake -DBUILD_SHARED_LIBS=ON .. ``` -------------------------------- ### Configure Precision (SINGLE/DOUBLE) Source: https://github.com/danfis/libccd/blob/master/src/CMakeLists.txt Sets the build precision for libccd based on predefined CMake variables. Defaults to SINGLE precision if neither CCD_SINGLE nor CCD_DOUBLE is explicitly set. ```cmake if(DEFINED CCD_SINGLE OR DEFINED CCD_DOUBLE) # make sure only DOUBLE or SINGLE is set; default to SINGLE if(CCD_SINGLE) set(CCD_DOUBLE OFF) else() set(CCD_SINGLE ON) endif() if(CCD_DOUBLE) set(CCD_SINGLE OFF) endif() elseif(ENABLE_DOUBLE_PRECISION) set(CCD_DOUBLE ON) set(CCD_SINGLE OFF) else() set(CCD_DOUBLE OFF) set(CCD_SINGLE ON) endif() ``` -------------------------------- ### Batch Collision Checking Source: https://github.com/danfis/libccd/blob/master/_autodocs/algorithms-and-patterns.md Implement a collision matrix for multiple objects using `ccdGJKIntersect` within nested loops for efficient broad-phase culling. Requires initialization of the `ccd_t` structure with a generic support function. ```c #define NUM_OBJECTS 100 typedef struct { void *objects[NUM_OBJECTS]; ccd_t ccd; int collision_matrix[NUM_OBJECTS][NUM_OBJECTS]; } collision_world_t; void init_collision_world(collision_world_t *world) { CCD_INIT(&world->ccd); world->ccd.support1 = generic_support; world->ccd.support2 = generic_support; } void update_collisions(collision_world_t *world) { for (int i = 0; i < NUM_OBJECTS; i++) { for (int j = i + 1; j < NUM_OBJECTS; j++) { int colliding = ccdGJKIntersect(world->objects[i], world->objects[j], &world->ccd); world->collision_matrix[i][j] = colliding; world->collision_matrix[j][i] = colliding; } } } ``` -------------------------------- ### Handle Quaternion Inversion Failure Source: https://github.com/danfis/libccd/blob/master/_autodocs/errors.md Check the return code of `ccdQuatInvert2`. A non-zero return value indicates success. If it fails (returns non-zero), the quaternion is too small to invert. ```c ccd_quat_t q, q_inv; ccdQuatSet(&q, 1.0, 1.0, 1.0, 1.0); if (ccdQuatInvert2(&q_inv, &q) != 0) { // Cannot invert near-zero quaternion printf("Error: quaternion too small to invert\n"); } ``` -------------------------------- ### Apply Compiler Visibility on Unix Source: https://github.com/danfis/libccd/blob/master/src/CMakeLists.txt Applies the -fvisibility=hidden compiler flag to the ccd target on Unix systems if the compiler supports it and symbol hiding is not disabled. ```cmake if(UNIX) check_compiler_visibility() if (COMPILER_SUPPORTS_VISIBILITY) set_target_properties(ccd PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") endif() endif() ```