Skip to content

2D physics in C++

Fx2D is an open-source rigid-body physics engine written in C++20. It uses SAT collision detection and XPBD constraints, with YAML scene loading and optional raylib rendering. Licensed under BSD-3-Clause.

Angry Boxes demo — a slingshot ball topples a tower of boxes simulated by Fx2D

Browser playground ​

The playground runs the engine compiled to WebAssembly. You can drag bodies, spawn shapes, and change simulation settings. Its source is the same examples/playground program used by the desktop build.

Running a simulation ​

Define a scene in YAML, load it, then step it. Use Fx2D/Core.h to add the raylib viewer, or work directly with FxScene for a headless simulation.

cpp
#include "Fx2D/Core.h"

int main() {
    auto scene = FxYAML::buildScene("Scene.yml");
    FxRylbRenderer renderer(scene, 60);
    renderer.run();
}
cpp
#include "Fx2D/Scene.h"
#include "Fx2D/YamlUtils.h"

int main() {
    auto scene = FxYAML::buildScene("Scene.yml");
    auto ball  = scene.get_entity("ball");
    for (int i = 0; i < 600; ++i) {
        scene.step(1.0 / 60.0);
        std::cout << ball->pose << '\n';
    }
}

See the installation guide for dependencies and build commands, and your first scene for a YAML example.

Examples ​

The repository includes C++ programs and YAML scenes for the examples below.

Engine overview ​

Each simulation step finds potentially colliding pairs, generates contacts, and resolves contact and joint constraints. The collision detection and XPBD solver pages describe the algorithms and equations used in the implementation.

Fx2D
LanguageC++20; physics core built as a static library
CollisionDynamic AABB tree + skin-aware SAT, opt-in speculative CCD
SolverSubstepped XPBD, warm-started, Coulomb friction, restitution
JointsRevolute and prismatic with position / velocity / effort motors
ScenesYAML with textures, joints, groups, reset
HeadlessYes — no raylib, no window, same API
Renderingraylib + Dear ImGui inspector, optional
LicenseBSD-3-Clause

Development and contributions ​

Fx2D is developed in the open. The roadmap records pending work, known limitations, and measurements from previous changes. The contributing guide covers code style, tests, and benchmarks.

Bug reports, reproducible scenes, documentation corrections, and code contributions are welcome on GitHub.

Released under the BSD-3-Clause License.