Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite jitter due to loss of floating point precision?  (Read 2952 times)

0 Members and 1 Guest are viewing this topic.

Sintax

  • Newbie
  • *
  • Posts: 1
    • View Profile
Sprite jitter due to loss of floating point precision?
« on: August 24, 2013, 03:00:59 am »
I've been working on a game that takes place in space, and I figure that I could generally just map game coordinates straight to sprite coordinates using sf::Vector2f's, then center a view on that position. However, when coordinates get large (over a million), there's a real noticeable sprite jitter and tearing effect that just gets worse with distance.

My best guess is that this is from a loss of floating point precision, so I'm just going to build a local coordinate system and use that to make points relative around 0,0. I'm at least wondering if this is a known issue (or actually a cause of something else) and to make note that I believe this issue can still lend to slight pixel flickering even at reasonably low coordinates (when rotating a sprite every frame).

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Sprite jitter due to loss of floating point precision?
« Reply #1 on: August 24, 2013, 03:12:11 am »
Yep, this is an issue affecting any program using floating-point coordinates.  Definitely not unique to SFML.

Just to make absolutely sure it's a loss of precision problem, try using Vector2<double> and see if the same problems occur when coordinates get over two(?) million instead of one million.

As an extreme example, I think http://www.minecraftwiki.net/wiki/Far_Lands were probably the result of some bizarre floating-point problems.  In those old versions of Minecraft you can still see extreme jittering if you go far enough away from spawn (just watch any recent Far Lands or Bust episode).
« Last Edit: August 24, 2013, 03:15:15 am by Ixrec »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Sprite jitter due to loss of floating point precision?
« Reply #2 on: August 24, 2013, 04:26:35 pm »
As an extreme example, I think http://www.minecraftwiki.net/wiki/Far_Lands were probably the result of some bizarre floating-point problems.  In those old versions of Minecraft you can still see extreme jittering if you go far enough away from spawn (just watch any recent Far Lands or Bust episode).
Actually, it was due to the fact that the internal terrain generator that procedurally generates terrain based on block positions overflowed (causing wraparound). Depending on how the computations are done, the overflow can occur sooner or later but at some point it will happen. The jitter is probably caused by something else not related to floating point representations. Such an abrupt change can never be attributed to precision loss. Precision loss is not something that happens from 1 floating point value to the next, it gradually accumulates to the point where it becomes noticeable, which is what Sintax is experiencing. If you went somewhere in the middle of that far away point and the origin and looked really close, you would notice a bit of jitter already.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).