OpenTrespasser / JurassicParkTrespasser

A git-based fork of the Jurassic Park: Trespasser source code.
98 stars 24 forks source link

Freeze by endless loops in Physics #84

Open meekee7 opened 4 years ago

meekee7 commented 4 years ago

Sometimes the game freezes because it is stuck in an endless loop in physics code.

Steps to reproduce

  1. Pick up a "storable" object like a pistol or keycard.
  2. Store the object in your inventory.
  3. Move so that you stand directly in front of a wall (doors work too)
  4. Keep storing and retrieving the object in such a way that the hand or the object touch the wall.

Personally I use the pistols in the security entrance at the beginning of the Industrial Town level for testing this.

The bug only rarely occurs in Debug builds, but is easy to reproduce in Release builds.

The cause

The problem is caused by two loops in physics code that are supposed to normalize vectors. The code is almost the same for both. Those loops are Pelvis.cpp lines 1162 to 1187 and Xob_bc.cpp lines 3738 to 3763.

The loop is supposed to end when a float variable L becomes smaller than a certain threshold. However, the comparison does not work anymore when L becomes NaN. This happens because it was calculated from the vector entries, which are all NaN as well. Current research has shown that they were already NaN before the loop. Why this happens needs to be investigated.

Solution approaches

Ending the loop when L becomes NaN prevents the game from freezing. But the vector is still full of NaNs and new problems occur immediately:

An alternative approach is to do the bailout described above, but also set the vector that was supposed to be normalized to a valid default value. That default value needs yet to be determined. After a first test with a quad-0.25 vector, various physics glitches with held items occur, but the game remains playable.

The best solution is to find out why the vector became NaN in the first place.