English | 中文 | Русский | Français
2,598 Posts served
8,347 Conversations started
Having settled the question of whether I should accumulate forces or accelerations last time, now it’s time to build the accumulation function.
i and j are indices selecting elements of the body array. First task is to compute the distance between them.
Pythagorean Theorem in three dimensions gets me the square of the hypotenuse, but before doing the square root, I’ll avoid the singularity:
That is, if the point masses get too close together, act like they’re not. But wait! Why do I even need the square root, if I’m working with gravitation, an inverse-squared law? Well, because acceleration is a vector so I need the next step.
Array ud represents the unit vector (length 1 direction vector) pointing from body j to body i. I need just one more thing, the magnitude of those accelerations.
All that’s left is to compute the acceleration vector components and apply them to the bodies.
Next time: serial bodies test run
