Page 1 of 1

### How are you able to simulate objects passing through eachother without having them accelerate to infinity?

Posted: Mon Apr 02, 2018 5:46 pm
Because the force of gravity is based on an inverse square, how come it doesn't accelerate into infinity when objects pass over eachother and by the next frame vanish before it has a chance to update? How are the values being integrated? I figured no better person to ask than the expert @testtubegames, or maybe there are some others around this board that know about simulating gravity on a computer. .

Here is a simple one dimensional example of what I mean: https://stackoverflow.com/questions/496 ... a-pendulum

Also a gif of the phenomenon: https://imgur.com/PhhRhP7

### Re: How are you able to simulate objects passing through eachother without having them accelerate to infinity?

Posted: Tue Apr 03, 2018 2:29 pm
Getting a good simulation of gravity is a surprisingly complex topic — not something I realized when I started way back in the day. Some tips for your simulation:

-First off, shrinking the timestep is always helpful. My simulation runs, as a baseline, about 40 ‘steps’ per frame, and 30 frames per second.

-To deal with the exact issue you talk about, I think modeling the bodies not as pure point masses - but rather spherical masses with a certain radius will be vital. That prevents the force of gravity from diverging to infinity. So, for instance, if you drop an asteroid into a star in my simulation (with collisions turned off), the force of gravity will increase as the asteroid gets closer, up until it reaches the surface of the star, at which point the force will begin to decrease. And the moment it’s at the center of the star (or nearby), the force will be zero (or nearly zero) - instead of near-infinite.

(That means these objects very oddly... after all you can't actually just pass through a star. But, hey, it's more fun and interesting that way!)

-Instead of Euler integration, I use Runge Kutta 4 - which you can look up to learn more about. It’s more accurate, though more of a headache to implement, so I’d bet you could solve your problem with the two tips above first.