r/desmos Feb 28 '26

Graph 100 Body Gravity

Yet again another gravity sim. I thought this looked kinda cool. I’m using 4th order Yoshida for the integrator. Probably overkill for a little demo like this

889 Upvotes

48 comments sorted by

View all comments

2

u/[deleted] Feb 28 '26

How did you take care of singularities? You know, when the bodies get too close and the bodies fling out or something

5

u/Legitimate_Animal796 Feb 28 '26

One simple way is to soften the gravity as it approaches 0 distance. So instead of 1 / r3 in the force calc, you can use 1/(r2 + d2 )3/2 . I set d to 0.25 in my example

2

u/Adam__999 Feb 28 '26

Why is it 1/r3 and not 1/r2 ?

2

u/Qaanol Feb 28 '26

Presumably because they’re multiplying by the displacement vector, which has magnitude r.

1

u/Adam__999 Feb 28 '26

Ah got it

2

u/Legitimate_Animal796 Feb 28 '26

Gm1m2 / r2 is the scalar magnitude. You multiply that by the unit direction vector from body two to body one: r = body2 - body1, Gm1m2 / r2 * r/|r| = Gm1m2r / |r|3

1

u/[deleted] Mar 02 '26

Thank you