5
\$\begingroup\$

Im following Mike Geig's 2D infinite runner tutorial. However, I'm seeing a potential problem of overflowing the float

How can I reset all objects and camera back again to the origin?

The approach I'm seeking is similar to the advice made by David Debnar on this question: http://answers.unity3d.com/questions/491411/best-practices-for-endless-runner-type-games.html

Definitely player. It's a lot easier, but to prevent float overflow, you should pop everything back a few thousand units every few thousand units

\$\endgroup\$
1
  • \$\begingroup\$ Float overflow isn't actually a thing; I assume that David Debnar was referring to the imprecision inherent in storing floating point values of large magnitude. (Which is usually called 'floating point imprecision') \$\endgroup\$ Commented May 10, 2014 at 7:34

4 Answers 4

4
\$\begingroup\$

You can store the iterations, also how often you popped the whole level to the right. After a certain amount (let's say 1000) put everything back, multiplied by the times of iterations and reset your iteration counter. So you prevent a possible overflow and you don't have to do the "expensive calculation" every time

\$\endgroup\$
3
\$\begingroup\$

I have to disagree with David Debnar. In a case of an "endless runner", where player's ability of movement is restricted, I guess it is more efficient to move the world around player to avoid complicated resets on player position.

Because these resets would create a hiccup in the gameplay if not planned carefully, and even for a casual game this is unacceptable for players.

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

First of, calculate after how much time you're getting into trouble. Say you're runner runs at 1 meter per second, and 1 meter is 1 unit in floats. The maximum number a 32bit float can hold is 2,147,483,647. Which would mean that someone would have to play your game for 68 years before an overflow would occur. Not really worth any extra coding ;).

Another trick is to not have the character move but to move the world. Since once a character has traversed a chunk it will be unloaded nothing will ever even get close to these ridiculous high numbers.

(Note that a 32 bit float becomes a lot less precise near the high-end of its values so you might encounter problems before overflowing though it would probably still take weeks or years before these problems become apparent).

\$\endgroup\$
7
  • \$\begingroup\$ Your advice makes sense if I applied Usain Bolt-type of speed (12 meters per second), 2,147,483,647 / 12 = 178,956,970 seconds before overflowing the number, which translates to 295 weeks. For Ironman type of speed (2,722 meters per second), it's 1.3 weeks only. If the player pauses the game or has a save mechanism, it's not that hard to overflow the number; especially if the game has speed booster power-ups \$\endgroup\$ Commented May 7, 2014 at 16:09
  • 7
    \$\begingroup\$ The maximum number a 32-bit float can hold is 3.4 x 10^38. That's pretty huge. The issue is precision, however. There's 23 bits of mantissa in a float so, for a 1024-pixel-wide screen, you can maintain pixel precision for up to 8000 screens. If your character can traverse the screen in 5 seconds, that's 40,000 seconds or about 11 hours. You'll probably start getting glitches after half that. Note that pausing the game doesn't do anything; it's MOVING that eats into available precision. Finally: +1, move the world not the character. \$\endgroup\$
    – AndrewS
    Commented May 7, 2014 at 17:14
  • 4
    \$\begingroup\$ The maximum number you listed in your answer is for a 32-bit signed integer, not a floating-point number. Floating-point sacrifices accuracy for ridiculously large range. \$\endgroup\$ Commented May 7, 2014 at 18:06
  • 3
    \$\begingroup\$ @MichaelBuen Nothing says that you have to map 1 unit : 1 meter. If you're traveling 2K meters per second, how much visibility does the player have? Assuminhg that the player's character is Iron Man sized (2m tall) and that that corresponds to ~20 pixels (a fairly tiny thing), that means that you're scrolling 20,000 pixels per second - a good 600 pixels per (30FPS) frame! It would be virtually impossible to have meaningful gameplay at that scale and speed. More likely, you'd represent things at roughly 1m=1pixel, and so you could say e.g. 1 unit = 100m. \$\endgroup\$ Commented May 7, 2014 at 18:16
  • \$\begingroup\$ @AndrewS Yep I know pausing the game doesn't do anything. I just mentioned the pause/savepoint so as to emphasize that one doesn't need play the game non-stop (who will play a game non-stop for a week let alone 11 hours straight?) to overflow the number, it's in realm of possibility to overflow the number, especially if one is moving the character instead of the world. I'll study the move the world approach. Thanks \$\endgroup\$ Commented May 8, 2014 at 1:13
0
\$\begingroup\$

You can put every object (except the 2D HUD Canvas and the directional light) as a child of a single master empty GameObject (let's say with a name "worldPlaceholder") and Translate its Transform by the position of the main player multiplied by -1. This should be done just once in a while to conserve CPU time (you can use a counter to do that, you place special trigger Collider each X meters to perform such a task). Something like that:

worldPlaceholder.transform.Translate (player.transform.position * -1);

I advise you don't do this every frame as it could be costly in CPU time.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .