Skip to content
/ Fx2D Public

Fx2D is 2D physics simulator written in C++ (20) with XPBD constraint solver, SAT-based collision detection, rendering with raylib + ImGui. Includes comprehensive math utilities.

License

Notifications You must be signed in to change notification settings

Bharath2/Fx2D

Repository files navigation

Fx2D

A 2D physics simulator written in C++ (20).

image

Key Features

  • SAT collision detection: Efficient circle and polygon collision using the Separating Axis Theorem (SAT)
  • XPBD constraint solver: Position-based dynamics with compliance control
  • Modern memory management: Safe resource handling with std::shared_ptr / std::unique_ptr
  • YAML based scene description: Declarative setup of entities, textures, and physics parameters in .yml files
  • Dynamic scene modification: Runtime manipulation of entities and constraints
  • FxArray & Math Utilities: NumPy-style FxArray and comprehensive linear-algebra utilities in Fx2D/Math.h
  • raylib-based rendering: Lightweight, cross-platform renderer with raylib and ImGui integration

Dependencies

  • CMake 3.16+ - Build system generator
  • Eigen3 3.3+ - Linear algebra and math operations
  • raylib 4.5+ - Scene rendering and graphics
  • yaml-cpp - YAML parsing for scene configuration
  • ImGui 1.92 - User interface framework
  • rlImGui - Raylib-ImGui integration

Building

Method 1: CMake

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j

Method 2: Using fxmake

chmod +x fxmake       # Make executable
./fxmake              # Build in Release mode
./fxmake debug        # Build in Debug mode
./fxmake rebuild      # Clean and rebuild
./fxmake clean        # Clean build artifacts

Quick Start

Basic Usage

#include "Fx2D/Core.h"

int main() {
    // Load scene from YAML
    auto scene = FxYAML::buildScene("./Scene.yml");

    // Initialize renderer with 60 FPS target
    FxRylbRenderer renderer(scene, 60);
    
    // Start the simulation loop
    renderer.run();
    return 0;
}

For headless simulation, testing, or data collection.

int main() {
    // Load scene from YAML
    auto scene = FxYAML::buildScene("./Scene.yml");

    const double dt = 0.001f;             // Fixed time step in seconds
    auto ball = scene.get_entity("ball"); // Get the poiner to the entity by name "ball"
    for (size_t i = 0; i < 10; ++i) {
        scene.step(dt); // Advance physics without rendering
        std::cout<< ball->pose <<std::endl;  
    }
    return 0;
}

Running Examples

To run an example, copy the main.cpp from examples to src/, build the project, and ensure Scene.yml and assets are accessible to the executable.

./Fx2D      # Linux/macOS
./Fx2D.exe  # Windows

truck

License

BSD-3-Clause License

Todo

  • Controllable joints
  • Dynamic AABB tree
  • Improve velocity resolution in collisions
  • and More.

Contributing

Contributions are welcome! Please follow the existing code style and conventions.

  • Bugs & feature requests: Open a new issue describing the problem or proposal clearly (steps to reproduce, or the rationale/use case).
  • Working on features/fixes: Create a new branch from main, commit your changes, and open a pull request referencing the issue.

About

Fx2D is 2D physics simulator written in C++ (20) with XPBD constraint solver, SAT-based collision detection, rendering with raylib + ImGui. Includes comprehensive math utilities.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages