Box2D Forums

It is currently Fri Jul 25, 2014 1:39 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 15 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Could you use integers?
PostPosted: Thu Dec 13, 2007 11:17 am 
Offline

Joined: Tue Sep 11, 2007 7:26 am
Posts: 95
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


Top
 Profile  
 
PostPosted: Thu Dec 13, 2007 11:20 am 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2946
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.


Top
 Profile  
 
PostPosted: Sat Dec 15, 2007 6:25 am 
Offline

Joined: Tue Sep 11, 2007 7:26 am
Posts: 95
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


Top
 Profile  
 
PostPosted: Sat Dec 15, 2007 10:02 am 
Offline

Joined: Sun Sep 23, 2007 2:35 pm
Posts: 803
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.


Top
 Profile  
 
PostPosted: Sat Dec 15, 2007 12:42 pm 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2946
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


Top
 Profile  
 
PostPosted: Sat Dec 15, 2007 3:29 pm 
Offline

Joined: Fri Sep 28, 2007 7:49 am
Posts: 61
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).


Top
 Profile  
 
PostPosted: Sat Dec 15, 2007 4:44 pm 
Offline

Joined: Tue Sep 11, 2007 7:26 am
Posts: 95
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


Top
 Profile  
 
PostPosted: Sun Dec 16, 2007 12:59 am 
Offline

Joined: Sun Dec 02, 2007 6:29 pm
Posts: 100
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.


Top
 Profile  
 
PostPosted: Sun Dec 16, 2007 1:38 am 
Offline

Joined: Sun Sep 23, 2007 2:35 pm
Posts: 803
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).


Top
 Profile  
 
PostPosted: Sun Dec 16, 2007 7:40 am 
Offline

Joined: Fri Sep 28, 2007 7:49 am
Posts: 61
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group