The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Floating points vs integers for motion?
luca00555


Joined: 04 Jan 2015
Posts: 33
Location: The Netherlands
Somewhere on this forum (can't recall the link) I read that for storing a x and y position to a texture, you should use floating points than integers.
Why is that and how does it make a difference? What is the true difference between floating points and integers anyway?
luca00555


Joined: 04 Jan 2015
Posts: 33
Location: The Netherlands
*rather than integers
Naith


Joined: 03 Jul 2014
Posts: 158
Integers are whole number like 1, 2, 3 etc.
Floating point numbers are decimal numbers like 1.0, 2.3, 4.7 etc.

What most people (well, me at least) do is to use a float variable whenever I, in some point in the game, want to move an object from one point to another, over time. With a float variable, I have much more precision than using a int variable.

For example, if I have a SDL_Rect quad that I want to move every frame, I have two float variables named XPosition and YPosition, holding the X- and Y-position of the quad. Each frame I'm updating those two float variables and I then position the quad by type-casting the two float variables into integers, since an SDL_Rect takes integers for positioning the quad.

If I would have used integers instead of floats I would loose the precision that the floats are giving / having.

Example code:

Code:

   // Initialization

   // Quad position
   float XPosition = 0.0f;
   float YPosition = 0.0f;

   // A simple quad
   SDL_Rect Quad = {0, 0, 32, 32};



   // Update

   XPosition += 20.0f * DeltaTime;
   YPosition += 20.0f * DeltaTime;

   // Move the quad to the right and down
   Quad.x = (int)XPosition;
   Quad.y = (int)YPosition;


If you only want to position a texture and never plan to move it, you can use integers though. But, like I said, if you in some point in the game want to move it from one point to another, over time, , you should use floats.
luca00555


Joined: 04 Jan 2015
Posts: 33
Location: The Netherlands
Since you're only using non decimal numbers in your example, what precision would you lose by just using integers instead of floats?
Or does this have to do with, like how high school physics 'n such told us that 20 can actually be any number reaching from 19.5 to 20.4999..(20 in case of an integer)
luca00555


Joined: 04 Jan 2015
Posts: 33
Location: The Netherlands
Oh sorry, I misunderstood. I usually don't have a decimal value for positioning since I'm not writing games but rather menu's and applications.
MikeyPro


Joined: 14 Feb 2015
Posts: 15
Because integers are locked to whole numbers only, the movement granularity is fixed to whole integers only, 1,2,3,4.. etc.. So the smallest you could increment is by 1.
Floats on the other hand can go between whole numbers, 1.1, 1.2, 1.3, 1.4, 1.5, even 1.50001.. Adding extreme levels of granularity. You can't move in-between pixels on the screen, but because floats can iterate from 1.0, 1.1, 1.2.. up to 2.0, instead of 1, 2 as with integers, it gives the visual effect of smoother motion.

In practice, what you normally do is have your floats keeping track of the "virtual" screen position, then you cast the floats to integers when you render whatever it is you're moving. That way you don't lose the movement precisions the floats have. Try it out if you really want to see the difference. But if you're doing non-movement stuff, floats are not necessary.

Also, if you look at Naith's example, he multiples the position by DeltaTime, so the value of (20.f * DeltaTime) is likely not to be a whole number.

Another good example is scaling/percentages. 20 * 0.9f equals 90% of 20, 20 * 0.1f equals 10% of 20, and so on and so forth.. You can do that with integers but it gets complicated and this day in age you don't need to worry about that unless your working on an old console or phone.