bleeprot(8) System Manager's Manual bleeprot(8)

   __   __                      __
  / /  / /__ ___ ___  _______  / /_
 / _ \/ / -_) -_) _ \/ __/ _ \/ __/
/_.__/_/\__/\__/ .__/_/  \___/\__/
              /_/

 ~ the machine is deeply symptomatic ~

home writing fortunes library links

PhysNgin is a 2-dimensional physics engine which includes an example SDL2 visualizer to consume it. This is a little write-up on some of the mathematics and the design of the project.

A scrot of physNgin.

The engine was designed to run n-body simulations.

All of this is just basic physics, but I'll discuss it here anyway.

Step one is to perform the following calculations for each body in the system against *every other body in the system*. Nothing crazy here. Just getting the distance and calculating the force using Newton's force formula (step 3). Force vectors Fx and Fy are reset to 0 between each engine step, so that the net force can be recalculated properly. β represents a softening parameter which is used to soften the force of gravity between bodies in the system. Generally the softening parameter is used to prevent infinites in the force calculation (due to the numerical limitations of computers), but setting this number high results in some very flowy and satisfying simulations.

physNgin calc 1

Once the engine has the updated force vectors it can update the velocities and the positions of each body in the system. Step 6 is the formula force equals mass times acceleration. This formula is used to calculated the acceleration for each vector (steps 7 and 9). We can then update the velocity by adding the acceleration over time to our current velocity (steps 8 and 10). Δt is the number of seconds that physNgin is stepping forward.

physNgin calc 2

physNgin calc 3

That's pretty much it.

The physNgin threadpool is a very important part of the engine. Prior to implementing the threadpool the engine could not run over 2000 bodies (on my primary test machine) without dropping below the target frequency of 60Hz. After implementing the threadpool it was able to churn out calculations for ~7500 bodies comfortably at 60Hz.

physNgin loop

physNgin body sizes

December 16, 2024