Skip to content

StevenKight/Physics-Engine

Repository files navigation

High-Performance Physics Engine

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.


Why This Stack

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.


Current State

  • CMake build system configured for all three languages compiling together; Makefile wraps 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
  • Vec3 3D vector type and PhysicsObject model 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_run simulation 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.


Build

Prerequisites

  • NVIDIA GPU and the CUDA Toolkit
  • Fortran compiler (GNU Fortran or Intel Fortran)
  • CMake 3.18+
  • CuBLAS (optional, for optimized matrix operations)

Instructions

git clone https://github.com/StevenKight/Physics-Engine.git
cd Physics-Engine
make

Running Tests

make test

Blender Addon

The 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.

Prerequisites

  • Blender 4.2 or later
  • The project cloned and built locally (see Build above)

Installation

1. Build the addon zip

From the project root:

make package

2. Install in Blender

  • Open Edit > Preferences > Get Extensions
  • Click the dropdown arrow (top right) and select Install from Disk
  • Select physics_engine.zip from 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.py and the compiled library at runtime

Usage

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.


Sample Scene

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.


Roadmap

  • Optimized matrix operations replacing naive implementations
  • Core rigid body mechanics
  • Benchmarking against existing engines
  • Soft-body dynamics and constraint systems
  • Additional Rendering pipeline integrations

License

Licensed under the GNU General Public License v3.0. Derivative works must carry the same license.


Contributing

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.

About

This physics engine is a research project aimed at leveraging high-performance computing (HPC) to simulate complex physical systems. It is designed to test and apply mathematical derivations in matrix form while utilizing modern hardware acceleration, including CUDA for GPU offloading and Fortran for optimizing critical tight-loop calculations.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors