Skip to content

Deterministic 2D physics for reinforcement learning

pip install fx2d for Gymnasium environments, scenes stepped in parallel across cores, and results that are the same bits on every platform. Underneath is an open-source C++20 rigid-body engine with SAT contacts, an XPBD solver and motorized joints, 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 ​

From Python, train on an environment or step a scene of your own. From C++, 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.

python
import gymnasium as gym
import fx2d.envs  # registers the environments

envs = gym.make_vec("fx2d/Push-v0", num_envs=64)
obs, info = envs.reset(seed=0)
obs, reward, terminated, truncated, info = envs.step(envs.action_space.sample())
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 Python guide for the package and its environments, the installation guide for the C++ build, 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
Pythonpip install fx2d: NumPy state, step_many, Gymnasium and vector environments
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, from Python or C++: 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 lists planned work and known limitations, and the engineering notes record the measurements behind 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.