0
\$\begingroup\$

For a better context this question is related to this one:

What's the largest "relative" level I can make using float?

When the player is moving too far away from the center, all the scene and objects are moved in the opposite direction, in order to bring the center closer to the player and avoid float precision issues.

Recentering all objects makes me doubt a little, since it might still accumulate error since it's an addition. I also don't think I can tell a physics engine to just teleport all its object without causing weird behavior.

\$\endgroup\$
1
\$\begingroup\$

Addition can lead to a loss of precision when increasing in magnitude.

eg:

$$(1 - 2^{-24}) + (2^{-23}) = 1 + 2^{-24} ≈ 1 \text{(nearest float)}$$

Here we can represent numbers in intervals of \$2^{-24}\$ in the range 0.5–1, but the addition bumps us out of that range and into the range 1–2, where we can represent only intervals of \$2^{-23}\$, so we're forced to round the result.

Bur when we reduce the absolute magnitude of the number, as we do when re-centering, we do not lose available precision in this way. Because of the way floating point keeps more precision closer to zero, reducing a number's absolute magnitude will put it into a range that has at least as much available precision, if not more, than it had previously.

You could get rounding differences if your offset value is not representable in the magnitude range you're starting from — so the result of the subtraction is not exactly representable either and requires rounding even though the precision available does not decrease — but this is easily avoidable by choosing a recentering interval that is itself a representable float. Something simple like a power of two works well here.

So:

  1. Establish a minimum precision you want to maintain

  2. Consult the table in the answer you linked to find the range at which you lose that precision

  3. Step back by a power of two from that limit

    (or more if you want to keep some buffer for chains of lossy operations elsewhere in your game)

  4. Pick the next lower power of two as your recentering interval

Now you will lose no bits of precision below your chosen minimum in any of your relative positions when you recenter.

\$\endgroup\$
  • \$\begingroup\$ I guess a precision of 2^-12 seems fine as my geometry chunks won't be larger than 8km. Another issue is real time physics, teleporting objects with bullet physics might generate problems. \$\endgroup\$ – jokoon May 1 at 17:39
  • \$\begingroup\$ Other detail: wouldn't re-centering be an issue when doing networking? \$\endgroup\$ – jokoon May 4 at 14:51
  • \$\begingroup\$ Short answer: no — there's nothing inherently unsolvable about handling multiple origins in a networked context. \$\endgroup\$ – DMGregory May 4 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.