$ show erincatto/box2d
Box2D is a mature, production-grade 2D rigid-body physics engine written in C17, designed primarily for game development. It provides a comprehensive C API for simulating rigid bodies, shapes (circles, capsules, polygons, chain segments), joints (revolute, prismatic, distance, weld, wheel, motor, etc.), sensors, and continuous collision detection (CCD). The engine is data-oriented, with a focus on determinism, multithreading, and SIMD acceleration (SSE2, AVX2, NEON). It includes a built-in scheduler for parallel constraint solving using graph coloring, and supports large worlds via double-precision coordinates. The architecture is modular: the public API is defined in `include/box2d/` with separate headers for types, math, collision, and the main `box2d.h`. The implementation in `src/` is organized into subsystems: world management, body/shape/joint creation, broad-phase collision (dynamic AABB tree), narrow-phase collision (GJK, SAT, manifold generation), constraint solving (contact solver with SIMD variants), island management, and recording/replay for deterministic debugging. The engine also provides snapshot serialization for save/load. While the core is a library, the repository includes a rich set of sample applications (using GLFW, OpenGL, and ImGui) that demonstrate every feature, a comprehensive unit test suite, and performance benchmarks. The codebase is exceptionally well-crafted, with clean abstractions, extensive validation, and a strong emphasis on correctness and performance. It is actively maintained by the original author (Erin Catto) and the community.
The LLM identified 3 files worth highlighting for reuse:
A dynamic bounding volume hierarchy (BVH) for spatial indexing, supporting insertion, removal, query (AABB, ray, shape cast), and rebuild. It uses a median-split heuristic with optional SAH, lazy marking, and DFS ordering. This is a standalone, reusable spatial index that can be extracted for any project requiring efficient 2D spatial queries, such as collision detection, nearest-neighbor search, or culling. The implementation is well-documented and demonstrates non-obvious optimization techniques.
A binary recording and replay system that logs every mutating API call (world, body, shape, joint, chain, spatial queries) into an op-stream with versioning and endianness checks. It supports keyframe-based seeking, state hashing for divergence detection, and validation against a reference simulation. This component is reusable for any deterministic simulation or debugging tool that needs to capture and replay a sequence of operations, and it demonstrates a high level of engineering for correctness and performance.
A header-only library of inline math functions for 2D vectors, rotations, transforms, and AABB operations. It includes dot/cross products, rotation, transformation, clamping, and a spring-damper parameterization function (b2MakeSoft). These functions are pure, stateless, and have no dependencies beyond basic types. They can be extracted and used in any C or C++ project requiring 2D math, and they demonstrate clean, efficient implementation with proper use of constexpr and inline.