A research project building a physics simulation engine using C/C++, Fortran, and CUDA. The goal is rigid body mechanics simulation with computation routed to whatever hardware makes the most sense for the job.
The short version: CUDA handles the big parallel workloads, Fortran handles small tight-loop numerical operations where it genuinely outperforms the alternatives, and C++ manages everything in between. Getting all three to compile together through CMake was its own adventure.
CUDA for GPU-accelerated parallel work is a straightforward choice. The Fortran decision gets more questions — the reasoning is that for small, tight numerical loops, Fortran's array handling and compiler optimizations are hard to beat and don't carry the overhead of GPU dispatch. Rather than forcing everything onto the GPU, the idea is to use the right tool for the scale of the problem.
C/C++ ties it all together and handles the core simulation logic.
- CMake build system configured for all three languages compiling together;
Makefilewraps common workflows (build,test,package,clean) - Complete matrix operation suite (addition, subtraction, multiplication, scalar operations, element-wise division, Hadamard product, power, row/column summing) implemented in both CUDA and Fortran backends
Vec33D vector type andPhysicsObjectmodel with Velocity Verlet integration (object_step)- Newtonian N-body gravity with adaptive CPU/GPU routing (Fortran for ≤64 bodies, CUDA above that threshold)
- Two-phase collision detection: octree broad phase (AABB overlap) + SAT narrow phase (convex meshes; parallelised with OpenMP)
- Inelastic collision response with configurable coefficient of restitution applied along the collision normal
- Top-level
sim_runsimulation loop sequencing gravity, collision detection, collision response, and Velocity Verlet integration each tick - Python ctypes interface (
interface/nbody.py) for driving simulations from Python;PhysicsObject.set_mesh()attaches convex geometry for collision detection - Blender addon (
blender/) integrating the simulation into Blender's physics system — convex hulls are extracted automatically from object meshes and baked N-body motion is written as keyframes - Unit test suite covering matrix operations, gravity, AABB helpers, collision detection, inelastic response, and Velocity Verlet integration
The Wiki has derivations and math notes as they get worked out.
- NVIDIA GPU and the CUDA Toolkit
- Fortran compiler (GNU Fortran or Intel Fortran)
- CMake 3.18+
- CuBLAS (optional, for optimized matrix operations)
git clone https://github.com/StevenKight/Physics-Engine.git
cd Physics-Engine
makemake testThe blender/ directory contains a Blender addon that exposes the simulation through the Physics properties panel, allowing objects to be tagged as N-Body participants with their mass and initial velocity set directly in the UI.
- Blender 4.2 or later
- The project cloned and built locally (see Build above)
1. Build the addon zip
From the project root:
make package2. Install in Blender
- Open Edit > Preferences > Get Extensions
- Click the dropdown arrow (top right) and select Install from Disk
- Select
physics_engine.zipfrom the project root
3. Configure the project root
- Go to Edit > Preferences > Add-ons and find Physics Engine
- Set Project Root to the absolute path of this repository (e.g.
/home/user/Physics-Engine) - This allows the addon to locate
interface/nbody.pyand the compiled library at runtime
With the addon enabled, select any object and open the Physics tab in the Properties panel. An N-Body button will appear alongside the built-in physics types. Enabling it marks the object as an N-Body participant and exposes its Mass and Initial Velocity for use in simulation.
Two sample scenes are included. Open either in Blender (with the addon installed and configured) and hit Run Simulation.
Sample - Solar System.blend — Newtonian N-body gravity with planetary orbits. Planet models by FyorDev on SketchFab — Solar System (Real Scale, 2k Textures), used for demonstration purposes.
Sample - Collisions.blend — Demonstrates inelastic collision detection and response between objects with convex mesh geometry.
- Optimized matrix operations replacing naive implementations
- Core rigid body mechanics
- Benchmarking against existing engines
- Soft-body dynamics and constraint systems
- Additional Rendering pipeline integrations
Licensed under the GNU General Public License v3.0. Derivative works must carry the same license.
Not open for contributions yet. If you want to discuss the project or have ideas, open an issue or reach out directly. See CONTRIBUTING.md for more details.