Could you use integers?

General Box2D issues or C++ specific issues
mike950
Posts: 95
Joined: Tue Sep 11, 2007 7:26 am

Could you use integers?

Postby mike950 » Thu Dec 13, 2007 11:17 am

Hi Erin,
I wonder if you used cm's for units would that allow you to
be able to use only integers in your engine and really speed it up?
I doubt there will be many objects less than a centimeter in length
that would be simulated.
Mike

Erin Catto
Site Admin
Posts: 2948
Joined: Thu Sep 06, 2007 12:34 am

Re: Could you use integers?

Postby Erin Catto » Thu Dec 13, 2007 11:20 am

Sorry, using integers would not work. There is a lot of math going on that needs fractions. I could use fixed-point math, but that would be slower on the PC. Some folks have ported Box2D to platforms that don't have floating point math and they used a fixed-point math library.

mike950
Posts: 95
Joined: Tue Sep 11, 2007 7:26 am

Re: Could you use integers?

Postby mike950 » Sat Dec 15, 2007 6:25 am

Hi Erin,
I guess my lack of deep mathematics is making me not understand :)
I'm just thinking that if the units were really small say even smaller than
a mm. I know it's extreme but say they were micrometers.
Then your numbers would be so large that it seems all the calculations
could be done using integers. Even division using integers.
I guess I'm missing something.
Mike

ewjordan
Posts: 803
Joined: Sun Sep 23, 2007 2:35 pm

Re: Could you use integers?

Postby ewjordan » Sat Dec 15, 2007 10:02 am

10 years ago converting everything to integers would absolutely have lead to a performance increase (without which games like Doom would never have seen the light of day), but apart from mobile phones and maybe consoles, today's floating point units tend to be competitive enough speed-wise that you often get faster performance using floats than ints depending on exactly what you're doing. Quite often I've noticed that you lose more in the casts to and from floating point than you gain by doing math with ints. At the very least, the speed is close enough now that if you're dealing with something that should be a real number, it makes more sense to leave it as a float than try to squeeze speed out of it by doing fixed point tricks.

For anything without an FPU, though, it's definitely worthwhile, but this is becoming less and less relevant as time goes on - in a few years even our phones will have nice FPUs.

Erin Catto
Site Admin
Posts: 2948
Joined: Thu Sep 06, 2007 12:34 am

Re: Could you use integers?

Postby Erin Catto » Sat Dec 15, 2007 12:42 pm

Yeah, at some point you might think that the units would be small enough to represented by whole numbers. But there are many calculations that deal with non-dimensional fractions. For example, Barycentric coordinates.
http://en.wikipedia.org/wiki/Barycentric_coordinates_%28mathematics%29

raigan
Posts: 61
Joined: Fri Sep 28, 2007 7:49 am

Re: Could you use integers?

Postby raigan » Sat Dec 15, 2007 3:29 pm

Someone on gdalgorithms pointed out that int64 for positions would give something like nanometer resolution and span our solar system.. I think it was Tom Forsyth.

One use for fixed-point physics is determinism, otherwise replays may or may not work cross-platform (or even differently on pc depending on the processor).

mike950
Posts: 95
Joined: Tue Sep 11, 2007 7:26 am

Re: Could you use integers?

Postby mike950 » Sat Dec 15, 2007 4:44 pm

Oh yeah! I see that in practice. I have a multiplayer browser based game where it does simple collisions
between discs. Kinda like a pool game. If one person is using a mac and the other is using a
pc each person will see a different result after all collisions even with identical initial configurations.
I always suspected it was because it was using floats for the physics.
Mike

kdmiller3
Posts: 100
Joined: Sun Dec 02, 2007 6:29 pm

Re: Could you use integers?

Postby kdmiller3 » Sun Dec 16, 2007 12:59 am

Battlezone 2 used a lockstep networking model requiring absolutely identical results on every client, down to the least-significant bit of the mantissa, or the simulations would start to diverge. While this was difficult to achieve, it meant we only needed to send user input across the network; all other game state could be computed locally. During development, we discovered that AMD and Intel processors produced slightly different results for trancendental functions (sin, cos, tan, and their inverses), so we had to wrap them in non-optimized function calls to force the compiler to leave them at single-precision. That was enough to make AMD and Intel processors consistent, but it was definitely a learning experience.

The biggest lesson was that any network model requiring clients on heterogeneous machines produce identical simulation results is ultimately impractical. Later projects used a more traditional client-server model, which may be more bandwidth-intensive but easier to implement and maintain. At the very least, a lockstep network model would need some continuous synchronization mechanism to correct for any "drift" between systems.

When it comes down to it, networked systems need to tolerate minor discrepancies between participants. While not as "neat", that tolerance makes systems much more robust.

ewjordan
Posts: 803
Joined: Sun Sep 23, 2007 2:35 pm

Re: Could you use integers?

Postby ewjordan » Sun Dec 16, 2007 1:38 am

kdmiller3: Out of curiosity, what kept you from precomputing lookup tables for the trig functions? Did the other math happen consistently across the processors? I was always under the impression that at least +-*/ should almost always be completely deterministic based on the IEEE specs (barring hardware malfunctions/Pentium bugs), but I'm curious to know if that's not the case - I know that Java offers the strictfp keyword to force determinism in floating point math, but I'm pretty sure that is only supposed to have an effect when overflow is a possibility (it turns off any sort of internal higher precision that may be used on some systems to avoid overflows when the final result is within range).

raigan
Posts: 61
Joined: Fri Sep 28, 2007 7:49 am

Re: Could you use integers?

Postby raigan » Sun Dec 16, 2007 7:40 am

There's also the issue of the internal registers of the pentiums being larger, which means intermediate results can vary. And even worse, directX can screw with the fp settings!


Return to “Bugs, Requests, and Feedback”



Who is online

Users browsing this forum: No registered users and 1 guest