I have a gameloop in which I increment a float variable (time) with the deltatime each iteration. As this time var is never reset during the play, won’t this go out of bounds at one point or another? Im pretty sure its a common issue in gamedevelopment, so im curious how other game developers handle this…
It would, be I’d be interested to know why you’re keeping tracking of the amount of total time that has passed in this way. If its for some game mechanic you could try tracking seconds and then storing them in a long (or better still just record the time the game started). That way you’ve got a fair amount of time before its likely to overflow
But another case where it could let to problems is if you’d want to count the total number of frames drawn… ok it will take a very long time if you count it in a long, but eventually it would bail out…
More accurately, a signed 64bit long counting frames at 60Hz will take almost two and a half years before wrapping. If if that causes a bug, tell the person who found it you don’t believe them and ask them to reproduce it. ;D
it will never go out of bounds, the highest amount or lowest amount is not protected, so after 3.40282346638528860e+38 it will goto 1.40129846432481707e-45 resp, Float.MAX_VALUE Float.MIN_VALUE it’s one nice big cirkel horray \o/
[quote]We are talking a seriously seriously long time here tho
[/quote]
[quote]More accurately, a signed 64bit long counting frames at 60Hz will take almost two and a half years before wrapping. If if that causes a bug, tell the person who found it you don’t believe them and ask them to reproduce it. ;D
[/quote]
Yeah… I’ll just mark this one as not being an issue I guess
[quote]More accurately, a signed 64bit long counting frames at 60Hz will take almost two and a half years before wrapping
[/quote]
Well, actually it will take 4billion years ;D
Actually it is quite likely that it will simply stop incrementing long before it gets near the max value.
Because the delta will be so small relative to the size of the time value that adding it won’t even change the least significant bit so the time value remains the same before and after the addition. It’s the nature of floating point values.
Try adding 1 to a number in a loop but only keeping 2 significant digits in the total… once you get to 100 you are stuck