Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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') –  Trevor Powell May 10 at 7:34

3 Answers 3

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

share|improve this answer

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).

share|improve this answer
    
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 –  Michael Buen May 7 at 16:09
4  
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. –  AndrewS May 7 at 17:14
2  
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. –  Andon M. Coleman May 7 at 18:06
3  
@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. –  Steven Stadnicki May 7 at 18:16
    
@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 –  Michael Buen May 8 at 1:13

Although I havn't seen the tutorial yet, but I assume you can always use the following code to reload the level -

Application.LoadLevel(Application.loadedLevel);  

Before loading it, save the iteration to a file. You can have an iteration of probably 20000 meters. So iteration count 1 means the player has run 20k meters and iteration count of 20 means the player has run 20x20k meters.

share|improve this answer
    
Reloading the level will result in a noticeable delay/disruption. Not a good solution really. –  bummzack May 10 at 12:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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