Imagine a virtual solar system in 3D.
This is how my simulation operates:
[Calculate new positions] -> [Render] -> [Calculate new positions] -> [Render] etc....
The loop maintains a steady pace of 25 frames per second.
One of the planets in my simulation is hurtling towards the sun at high speed, almost on a collision course. Let's dive into the details of the simulation loop:
- Positions are calculated for the planet and the sun (they have not collided yet, but are dangerously close).
- The scene is rendered.
- Due to finite FPS, there is a slight pause before the next calculation.
- New positions are calculated for the planet and sun - and here lies the issue - they did not collide because during the 'pause,' the planet passed through the sun and now their positions are significantly far apart. The collision detection algorithm fails to register any collision.
Here are some possible solutions that I'm considering:
- Increase the FPS
- Decrease the speeds of the planets (reduce gravitational constant maybe?)
- Implement a separate simulation loop that runs concurrently with the rendering loop, but at a faster rate
For those curious, I am utilizing three.js.