I'm working on a small game for Android which deals with some simple physics, including the need to stack objects on top of one another without jitter. This infamous "stacking collisions" issue with custom collision/physics engines really has me on the ropes with this project.
To highlight the issue I'm gonna go through a basic rundown of my gameloop and the order in which this engine computes the various steps. For example, lets say we have 3 objects in this scene. 2 boxes and a ground floor. The update loop is as follows:
- Apply external and internal forces to each object (including gravity, any movement from input, etc.).
- Use a collision tree to separate the various game objects into potential collision pairs (this is working just fine).
- Run a collision detection algorithm on each pair to get a contact manifold for each collision that occurs this frame. My project is using SAT and its been working very well.
- Sort the list of contact manifolds to order collisions by time of occurance.
- Loop through the ordered manifolds and solve each collision as necessary.
- Update each object's position in the world with the results of the collision correction and render.
When dealing with one pair of objects in isolation, this works great. But when you add a 3rd object to the simulation and stack them on top of each other the resulting simultaneous collisions result in visible jitter as the objects are pushed in and out of each other due to the many manifolds they have to deal with. In my example the static ground remains stationary but the bottom box jitters in and out of the ground slightly as the top box pushes down on it due to gravity. Adding an iteration loop around steps 3, 4 and 5 helps stabilize the objects since they rerun the collision checks and get more accurate values, but to make the collisions perfectly stable I need a very high number of iterations (exponential amounts the larger the stack of objects), which kills the framerate of the game.
I'm not looking for any complete code, but some theories on how this stacking issue should be avoided would be fantastic. I'm just unsure where to take this logic-wise anymore, but I feel like anyone taking the custom physics engine route has probably seen similar issues.