//Calculate time step
float timeStep = stepTimer.getTicks() / 1000.f;
//Move for time step
dot.move( timeStep );
//Restart step timer
stepTimer.start();
This code taken from Lazy Foo's SDL Tutorials should produce a variable timestep based movement system. My main question about this is how does it work? float timeStep = stepTimer.getTicks() / 1000.f;
calculates the elapsed time in seconds. So if let's say 2 milliseconds passed since the last move call, timeStep var will be 0.02 seconds. How is that possibly usable in code? If i have a constant speed of 10 pixels per second and I'd like to make that speed work with any framerate, I'd have to multiply my px/s by that value (0.02).
However this would result in the following posx += 10*0.02
. And as we just learned, that means we increase the position by a value of 0.2 pixels (which of course wont work and it will just floor the value).
What am I missing here?
You maintain a position variable in code; a floating point value, which is exact and accepts tiny changes, down to number of significants (ca 7 for 32-bit floats and 14 for 64-bit floats).
Then you draw onto screen from that.
For a while, it will be translated (or say rounded) to one pixel, after a while to the neighboring pixel, etc.
You mistake is that you use (or interpret it in this way) pixel addresses as position data. A screen has only a highly limited "numeric space". Consider doing any math using integers between 0 and 2000 only. Won't take you far.