Friday, August 12, 2016

How do I fix objects "popping" or jittering in physics engine?


I've got a simple physics engine that solves collisions by simply correcting the position of overlapping bodies (just circles for now) directly, as opposed to just changing the velocity or applying an impulse. Velocity is only changed after impacts are already resolved, or during the integration part.


I've had the problem that in heaps of objects the top objects apply too much pressure (this exists implicitly, there's no pressure modelling in the algorithm) on the objects at the bottom of the heap, which results in them getting pushed through floors, etc.


I wanted to fix this by sorting the objects by their y-coordinate, so the collisions are resolved bottom-up. But now, the engine shows weird popping behavior for objects which should actually be at rest (see gif)


enter image description here


Without just giving you the source code - what could this be?




Answer



One solution I have found when using position correcting is to have a few iterations and vary the strength with each iteration.


doPhysics();

int num_iterations = 5;
for(int iteration=0; iteration{
float strength = float(iteration+1)/num_iterations;
correctPositions(strength);
}


So the first iteration has a strength of 1/num_iterations and the last has a strength of 1. This makes my simulations smoother and more stable than simply using the same number of iterations with a fixed strength.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...