# Gravity simulation that works on close distances

I'm programming a gravity system, based on this answer, and I've also tried this approach.

Everything is working fine, but I want the objects to pass through each other without any collision and to keep an harmonic motion,. To see what I mean, try placing two planets on this simulation with 'Merge off'.

However, on my implementation, there's a point in which the bodies are very close and they make a huge force on each other, getting them into unexpected places.

I guess this can be solved by using adaptative time steps and/or trying different integration approaches. However, I'm sure there has to be a simple, hacky way to get the harmonic behaviour descrived above.

Any help?

• Your second link should be your approach. Do you know the physical meaning of the parameter "$\epsilon$" in that equation? It is (roughly) the distance at which your force (artificially) stops depending on distance and becomes constant. My guess is your $\epsilon$ is too small. Try using a larger value of $\epsilon$ and see if it works. – pela Dec 6 '16 at 12:03
• Well, thank you a lot! I thought it was supposed to be a value like 0.01. I tried using the radius of the body and it works pretty well. Just a matter of tuning it now, I guess. – Knexator Dec 6 '16 at 15:21
• You're welcome. In case you're using Eulerian integration rather than Leapfrog, you might wanna change that as well, since it's just as easy and much better. This answer has a Python version that should be translatable to other languages. Note though that the rest of the code in that particular answer is rather specific, in that it calculates the trajectory of a planet orbiting a star that suddenly starts to move. – pela Dec 6 '16 at 21:08
• This seems to be a programming issue, which ought to be asked in Stack Overflow. – sammy gerbil Dec 6 '16 at 21:22