Reputation: 63
//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?
Upvotes: 1
Views: 225
Reputation: 824
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.
Upvotes: 4
Reputation: 103733
From your question, I gather you are storing the position of your objects as an integer coordinates representing which pixel it is located at. So the answer to your question is to simply not do that. Store the location as a floating point value, perhaps representing some fraction of a pixel, or whatever other measurement you want to use. If your drawing system requires integer pixel coordinates, you can simply convert to that for the drawing stage.
Upvotes: 4