6
\$\begingroup\$

We can use integer for game physics (or without physics, simply object representation): mass, position and rotation, where the integers represent, for example, the number of milligrams, millimeters or (1/56000) of a degree. However, almost all game code I've seen recently use floating points.

Is it slower to use integers for game physics calculations? Are there any other advantages and disadvantages from the developer's point of view for using integers? Or it is because all our hardwares

\$\endgroup\$
  • 1
    \$\begingroup\$ floats are used because we want to represent continuous spaces like R. \$\endgroup\$ – tp1 Jan 3 '14 at 21:00
  • \$\begingroup\$ Possibly the only benefit I could see, given the range and versatility of floating point, is a reduction in storage space down from 32 bits for a standard float. This might have some benefit on the GPU, for instance, where sending data across the bus comes at a premium. \$\endgroup\$ – Engineer Jan 3 '14 at 21:13
  • \$\begingroup\$ See Also About Floating Point and why we still use it \$\endgroup\$ – Jimmy Jan 3 '14 at 21:43
7
\$\begingroup\$

The days where floating point computations were (significantly) slower than integer calculations are long gone. If I'm not mistaken it was the Pentium CPU that first leveled the playing field of floats vs integers.

So there's actually no reason whatsoever to not use float (or even double) for any calculation that benefits from it. The use of integers to represent floating points was just a crook in order to avoid the performance impact of floating point calculations on older hardware.

In fact modern (desktop) CPUs don't even make much of a difference between float and double, especially on 64bit CPUs running a 64bit OS.

\$\endgroup\$
  • \$\begingroup\$ +1 Quite so. Not only that, but IEEE754 standardised decimal point numeric operations across a vast range of platforms (by now, given the standard was introduced in '85). At this stage, it would be folly to attempt to work around the highly efficient FPU in favour of non-standard fixed point. \$\endgroup\$ – Engineer Jan 3 '14 at 21:09
  • 2
    \$\begingroup\$ So you want to say that DDA and Bresenham drawing algorithms which use integers only, are no longer useful optimizations? I'd bet these will still be faster than naive float + round solution. \$\endgroup\$ – Tomas Jan 4 '14 at 13:58
  • \$\begingroup\$ Anyway, I think it makes sense to use integers since everything - both time and space - in the game are discrete (pixels, time steps...) \$\endgroup\$ – Tomas Jan 4 '14 at 14:01
  • 1
    \$\begingroup\$ @Tomas Betting is not knowing. ;) Besides to draw lines I can usually rely on glDrawLine and similar. And not everything is discrete, delta time for example. Also: subpixel rendering, anti-aliasing, and similar effects. Point being: even if there are optimized integer-based algorithms that may even to this day be faster, they typically aren't meaningful to most real world development being done today. \$\endgroup\$ – LearnCocos2D Jan 4 '14 at 14:16
  • \$\begingroup\$ 1) anti-aliasing is for sure integer thing - it's the remainder in the DDA/Bresenham algorithms 2) and how is glDrawLine implemented? I'd bet (again :) its based on integer algorithms :-) \$\endgroup\$ – Tomas Jan 4 '14 at 14:44
1
\$\begingroup\$

You can use integers for the inputs to your physics system. Although you're likely going to have to use non-standard units of measure. Typically we see meters per second, kilograms, etc. Using smaller units just so we can more easily round the values to whole numbers isn't hard, it's just kind of silly.

However, as soon as you want to perform any calculations, you're going to want to use floats, not integers. Any formula that has a division involved is going to want to use floating point values.

The reason we use floating point values is because it's closer to what we use in mathematics and real world physics. There's no reason to constrain ourselves to integers. Floating point calculations are very fast on modern CPUs.

\$\endgroup\$
  • \$\begingroup\$ Rationale for downvotes helps answers improve and gives people seeking information in the future more information on why they shouldn't listen to an answer. \$\endgroup\$ – MichaelHouse Jan 3 '14 at 21:15
  • 2
    \$\begingroup\$ the only issue I have is the sentence "Any formula that has a division involved is going to want to use floating point values." You don't; you just need to get the double sized result and slice the correct part (if you use a power of 2 factor), same with multiplication, where you get a real problem with overflow if you don't rescale carefully \$\endgroup\$ – ratchet freak Jan 4 '14 at 1:21
  • \$\begingroup\$ I see, I was referring to the loss of accuracy when rounding to integers, not overflowing. \$\endgroup\$ – MichaelHouse Jan 4 '14 at 18:27
  • \$\begingroup\$ Division is the real deal. So, i think huge worlds server side (even if it is an offline engine) global positioning, should have one data storage, that could even be long integer in millimeters, and no cells! Now, for a cell physics calculations, the global positioning would be translated to a cell/local positioning, that would use floats as is nowadays. We can easily abstract the global positioning on our database, and just convert it to the current cell physics calculations when needed. \$\endgroup\$ – VeganEye Jan 13 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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