Advance, highly interactive 3D computational physics engine built in C++ and Raylib. This simulation visualizes the behavior of charged particles in uniform electric fields and non-uniform magnetic topologies. It serves as a foundational tool for understanding electromagnetics, particle drift (
- Symplectic Physics Engine: Utilizes the industry-standard Boris Algorithm for perfect energy conservation over infinite time frames, outperforming standard RK4 methods for magnetic fields.
-
Dynamic Magnetic Topologies: Simulates a "Magnetic Bottle" with configurable base strength and spatial gradients (
$\nabla \cdot \vec{B} = 0$ compliant). -
Data Acquisition Pipeline: Real-time telemetry streaming dumps 3D positional and velocity data to a
.csvfile for external data analysis in Python or MATLAB. - Custom Interactive UI: A decoupled 2D control panel with smooth click-and-hold adjustments for live simulation tweaking without pausing.
- Spherical Camera Controller: Custom-built 3D camera that ensures stable viewing and precise click-and-drag rotation, independent of the UI.
-
Velocity-Mapped Trails: Particle trails dynamically shift color (Blue
$\rightarrow$ Red) based on real-time kinetic energy. -
Energy Tracking: A live UI readout calculating the exact Kinetic Energy (
$\frac{1}{2}mv^2$ ) at every frame to prove that energy is being conserved properly. -
Configuration Files: Instead of hard-coding the starting positions, the program read a
.jsonfile on startup. This allows to set up 100 different starting scenarios without recompiling the C++ code
The core of the simulation is governed by the Lorentz force law:
When both an Electric Field (
While first simulation was with the Runge-Kutta 4th Order (RK4) method , which is highly accurate for general differential equations, it is not symplectic (A manifold is not symplectic if it fails to admit a closed, non-degenerate 2-form (\omega ) due to Dimension). Over millions of integration steps, floating-point errors accumulate, causing simulated particles to artificially gain or lose energy.
This engine uses the Boris Algorithm—the absolute standard in plasma physicsand PIC codes because it offers excellent long-term energy conservation and accuracy. this method that cleanly separates the electric acceleration from the magnetic rotation. It perfectly conserves phase-space volume and the particle's kinetic energy over infinite time.
To trap a particle in 3D space (as done in fusion reactors), we implement a Magnetic Mirror. We artificially increase the magnetic field strength at the edges of the simulation along the Z-axis. When the spiraling particle hits the high-density magnetic wall, the physics force it to compress, stop, and violently bounce back, effectively trapping the superheated plasma.
- Pause / Resume: Freeze the physics engine while keeping the camera active.
- Reset Scene: Return the particle to its initial state.
- CSV Recording: Toggle real-time data dumping to
telemetry_data.csv. - Click-and-Hold Parameters: Adjust Charge, Mass, E-Field (X/Y/Z), B-Field Base Strength, and Bottle Gradient dynamically.
- Rotate Camera:
Left Click+Draganywhere in the 3D space. - Zoom:
Mouse Wheelscroll.
- A C++17 compatible compiler (e.g., GCC/MinGW on Windows).
- The Raylib graphics library.
- Install Raylib from github and the GCC C++ compiler.
- Compile and Simulate
- Install Raylib dependencies:
sudo apt install libraylib-dev - Compile:
g++ -O2 main.cpp -o simulation -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
Clicking "START CSV RECORDING" in the UI generates a telemetry_data.csv file in the root directory formatted as follows:
Time, PosX, PosY, PosZ, VelX, VelY, VelZ, KineticEnergy
This file is ready for import into MATLAB, or Excel for phase-space plotting and statistical analysis.
This project is open-source and available under the MIT License. Feel free to fork, modify, and use it for your own educational or research purposes!
Emiail - soumasishchakraborty02@gmail.com


