Usually we constantly render frames (redrawing screen) in a while loop (so called "main loop"). To "move" an object we just draw it in another position than it was in previous frame.
To move objects consistently, you need to know a time between frames. We call it "delta time" (dt
). So, between frames, time increases by dt
. Given velocity (speed) of object (v
), we can calculate displacement as dx = dt * v
. Then, to get current position, we just add dx
to previous position: x += dx
.
Note, that is a bad idea to calculate delta just inside your update or rendering code. Avoiding spreading out this functionality, we usually localize this calculations in timer/clock class.
Here is a simplified example:
// somewhere in timer class
// `Time` and `Duration` are some time units
class Timer {
Time m_previousTime;
Duration m_delta;
public:
Duration getDelta() const { return m_delta; }
void tick() {
m_delta = currentTime() - m_previousTime; // just subtract
m_previousTime = currentTime; // `current` becomes `previous` for next frame
}
};
// main loop
while(rendering) {
Timer.tick();
Frame(m_Timer.getDelta());
}
void Frame(Duration dt) {
if(keyPressed) {
object.position += dt * object.velocity;
}
}
You can now even make your object to move with acceleration (throttle, gravity, etc.):
object.velocity += dt * object.acceleration;
object.position += dt * object.velocity;
Hope you got the idea! Happy coding!