giovedì 17 settembre 2015

Day 73: The limits of physics simulations

Today I experienced the hard way a problem about physics in a discrete time context.

Tweaking a few values of the drag formula I noticed that with certain values the ball was behaving unexpectedly. With other values its position was rapidly growing to infinite.
After quite a lot of time trying to understand what was wrong, I realized it was not fault of the formula, but of the fact that physics is processed only 60 times for second.

Let's say that a ball is travelling very fast inside water. The water applies a resistance force which is very high at the beginning, and gets softer with the ball losing speed.
If this happens in less than a frame, the force is calculated at the beginning, and it will be so high, that it will actually invert the speed of the ball, which is not the desired effect.
The force will be calculated again the next frame, but the damage will already be done.
The values of the speed were something like this (in m/s):

20 → -30 → 90 → -900 → 500000 → - INF → INF

These values shows that the drag force is so intense that it will actually invert the speed, and make it even more intense. This generates a sort of pong effect, and v will tend to infinite.
Thanks to these numbers I realized the problem, and actually there is not a real solution, except to avoid using "risky" values, or bring to 120 the fps of the physics simulation.
These methods don't solve the problem, but makes it less easy to happen.

Since I was stuck on this problem for quite some time, I just added a few things to the engine: lateral bouncing and a debug tail that changes color depending on how much time before the object passed from that point (as can be seen in the picture).


Nessun commento:

Posta un commento