It's good question to pops up interesting solution.
Your question tagged as libgdx
, so my solution won't stay away from using it. Let's tackle into each issue here.
No Matter, It's Still Float
Firstly, I need to tell you that no matter how precision of position (float, double) you have, libgdx's underlying system will go with float
at the end. See its SpriteBatch class which use float
to represent position whenever it needs to draw something on screen, and also Sprite
class which bases things off float
for its position as well.
This means no matter your precision, things will be drew on screen in float
.
Precision
Also no matter you use float
or double
, you will be getting floating-point calculation error. If you cannot be off by one bit (i don't know your game requirement deeply), whether it's about position or something else according to your game, then you might need to consider using BigDecimal. Bank application would use something like this as it cannot lose any one bit due to rounding-error, customer would likely complain!
You might get away by maintaining BigDecimal
inside your game object. You update its position through them instead of normal way to set position in libgdx. But whenever you need to draw, you get its value via doubleValue(). Why double and not float? It's better to get highest precision that it can offers to use for your game, so when floating-number cannot 100% accurately represents the number, you will get smallest error as less as possible.
BigDecimal Is Slow
Be careful as BigDecimal
is slower than normal floating-point operation. See its benchmarks here and here.
Workarounds
Because I see that at one point in time, users only see a limited area on your space. Or even if you might have a space map that allows user to zoom-out to see the whole area, that's a time when you don't really need precision or per-se double
-precision, default way libgdx does is suffice and no need to switch game library (others will mostly base on float
anyway) as well.
That's the reason that you can still use float
but use BigDecimal
or just String
to represent any large scientific value on screen which doesn't involve in calculation that affects the game performance in run-time. Thus I believe this allows us to scale down the massive distance so you can represent game objects with same intention to be scientific correct.