ShotgunNinja / Kerbalism

Hundreds of Kerbals were killed in the making of this mod.
The Unlicense
43 stars 19 forks source link

Solar panel sun visibility sampling error at max timewarp for loaded vessels #95

Closed ShotgunNinja closed 7 years ago

ShotgunNinja commented 7 years ago

The issue is as following: assume a vessel in low orbit around a body. At each simulation step, the sun visibility is determined by raycasting from vessel position to sun position, and if the ray hit the body then the vessel is considered in shadow. This work, provided that the number of simulation steps per-orbit is high enough. However, as timewarp speed increase, the number of simulation steps per-orbit decrease. When the visibility samples taken approach a low number per-orbit (eg: only one), then the vessel will be considered either in shadow or in sunlight depending on the point where the single sampling was done along the orbit.

This infographic illustrate the problem better:

This issue was recognized long time ago, and fixed for unloaded solar panel simulation by switching to an analytical sunlight estimation over the whole orbit when timewarp speed exceed a threshold.

However, the stock code for the solar panel module have this issue too. This lead to wrong sunlight estimates and EC production in loaded vessels, and indirectly to CO2 poisoning or climatization insta-death.

The issue is relatively hard to reproduce consistently without knowing the details provided here. In fact, the frequency at which it can be observed depend on the portion of orbit that is in shadow and the timewarp speed, both indirectly determining the number of sampling points per-orbit. As the number of sampling points is very probably not an integer, the sampling points will appear to move orbit after orbit. All this complicate reproduction steps, and make the related reports random as hell.

The only practical way to fix this is to implement a custom solar panel module, and use it to replace all occurrences of the stock solar panels

ShotgunNinja commented 7 years ago

This was fixed, without replacing the stock solar panel module. Instead, a new module WarpFixer was created. This module set stock module output to zero, and then calculate the correct output itself,considering analytical sunlight visibility. Then the EC is produced using the resource cache, so that warpspeed and EC capacity can't influence the amount actually produced.