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\$
3
  • 1
    \$\begingroup\$ floats are used because we want to represent continuous spaces like R. \$\endgroup\$
    – tp1
    Jan 3, 2014 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, 2014 at 21:13
  • \$\begingroup\$ See Also About Floating Point and why we still use it \$\endgroup\$
    – Jimmy
    Jan 3, 2014 at 21:43

3 Answers 3

8
\$\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\$
7
  • 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, 2014 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, 2014 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\$
    – CodeSmile
    Jan 4, 2014 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, 2014 at 14:44
  • \$\begingroup\$ @Engineer Integer operations are very consistent across platforms. Fixed-point operations are implemented using integer operations (with scaling applied by the developer, who presumably would not scale differently on each platform). \$\endgroup\$ Dec 10, 2021 at 19:54
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\$
4
  • \$\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\$
    – House
    Jan 3, 2014 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\$ Jan 4, 2014 at 1:21
  • \$\begingroup\$ I see, I was referring to the loss of accuracy when rounding to integers, not overflowing. \$\endgroup\$
    – House
    Jan 4, 2014 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, 2021 at 17:48
1
\$\begingroup\$

I would add here that there is a reason to consider alternative physics models than floating points in applications like when having a large world in games. Typically, the farther from the origin you go, the less space you have for digits and thus the lower precision your calculations have. This causes rounding errors to accumulate and physics to break. Some games where this limitation is observed include Minecraft, BeamNG, and VR Chat. I'm unsure if it's actually worth it (or is practical) to use a pure integer model to fix this since it can be fixed with other methods. For example, having many origins with a number ID and simply cycling between them such that floating numbers never get high enough to lose precision.

\$\endgroup\$
1
  • \$\begingroup\$ Also, floating point sometimes has issues where it very occasionally won't return exactly the same cross platform - in many places that won't matter, but sometimes that can mess thing up in multi-player games. \$\endgroup\$
    – MadMan
    May 31 at 5:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .