Exact movements with Delta-Time. In SDL with C ++


I'm creating a 2D game with C ++ and SDL , where in time management I use the Delta-Time technique / em> (delta time) for movements with smooth movements. The execution is perfect, however there are displacements of objects (platforms) that depend on time, that is, to move, for example, 500px must do it in 6 seconds, all right up to this point.

Note: The movement is perpetual, that is, repeats infinitely

The problem occurs when there are more objects, since if there are two or more objects with the same duration and different speeds, a delay lag is observed between the objects, since they should reach their destination in the same time . It is understandable because Delta-Time does not have the same delay between each frame and generates displacement by various pixels.

Is there any way to control the lag, or is there another method for this type of movement that depends on a specific time?

asked by Islam Linarez 09.01.2017 в 09:56

2 answers


What you are looking for is to do something called Linear interpolation .

Your goal every time you call the function where you paint your scene is not to paint each object but calculate where each object should be painted , according to the time elapsed.

To do this, you must define the source value, the end value and the time it will take the target variable to reach the end value.

Here you have a video in English that explains how to perform a smooth movement of a character.

As you mention that you are dealing with it in c ++, look for interpolation libraries such as this port of Tween .

answered by 09.01.2017 / 10:24

If it is vital that the synchronization of the position of these objects be maintained, I recommend that instead of calculating the new position of each object based on the current position, calculate it in an absolute way as a function of time.

That is, right now you will have something like:

pos = pos + vel*deltaTime.

Instead, you can put something like:

totaltime+= deltatime
pos = vel * totaltime'

If instead of a linear movement, you want a round-trip movement, the same thing, use totaltime to calculate what the position would be at a given moment.

answered by 10.01.2017 в 13:55