My game engine just went through a small overhaul that changed its fixed time step loops into variable time steps ones. Though everything has already been adapted steadily to the new environment, there is still one algorithm that relied heavily on steady ticks: my camera's behavior.
It was pretty much like this (pseudo-code):
class Camera {
const float factor = /* value between 0 and 1 */;
void update() {
Vector2 distance = player.getPosition() - this.getPosition();
this.move(distance * factor);
}
}
What is generally done for this sort of "pursuing" behavior? This is more than simple speeds and accelerations.
I could go with the following, but it still leaves a feeling of choppiness when in contrast with the rest of the game.
class Camera {
const float factor = /* value between 0 and 1 */;
const float fixedStep = 1.0f / 40.0f; // supposing that it worked under 40FPS before
float timeAcc = 0.0f;
void update (float timeElapsed) {
timeAcc += timeElapsed;
while (timeAcc >= fixedStep) {
timeAcc -= fixedStep;
Vector2 distance = player.getPosition() - this.getPosition();
this.move(distance * factor);
}
}
}
It is C#-like pseudo-code, but don't be fooled: I'm actually using C++ and SFML, so I don't have many (or any) already-implemented methods at my reach.