Cornell University ECE4760
Gravity particle simulation
Pi Pico RP2350
Gravity
Gravitating particles interact over long distances via a inverse-square law force. If we write the force equation to factor out the particle mass to make an acceleration,
then in vector format we get the vector acceleration of the ith particle as:
where G is the gravitational constant, mj is the mass of the jth particle, epsilon is a small number to prevent infinite forces, and rij is the vector pointing
from particle i to particle j. This is effectively the inverse square law, muliiplied by the cos and sin components of the connecting vector.
The equation and some of the code is based on an ECE5760 project by Mark Eiding and Brian Curless in 2015.
The goal is to compute this as fast as possible in floating point on the rp2350. You can get almost a factor of 2 speed up by
noting that the equation is symmetric between particle i and j, except for the sign of the connecting vector and the mass.
The following snippet computes the acceleration for the ith particle, while also adding up the effects of the ith particle
on all the other particles of higher index j.
A short video shows 150 gravitating particles computed in floating pont at a frame rate of 140 frames/second.
Display, of course, is 60 frames/second.
The red mass is 800, the green mass 15, the white masses are small, 0.001.
Mass units are arbitrary, since big G was set to unity.
Copyright Cornell University March 6, 2025