# \[SDL2\] Help to smooth player update based on V\_SYNC

**URL:** <https://discourse.libsdl.org/t/sdl2-help-to-smooth-player-update-based-on-v-sync/69334>\
**Category:** Game Development\
**Created:** [September 17, 2026, 5:59pm UTC](https://discourse.libsdl.org/t/sdl2-help-to-smooth-player-update-based-on-v-sync/69334 "2026-09-17T17:59:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabrielsmoreira](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@gabrielsmoreira](https://discourse.libsdl.org/u/gabrielsmoreira)\
**Post date:** [September 17, 2026, 5:59pm UTC](https://discourse.libsdl.org/t/sdl2-help-to-smooth-player-update-based-on-v-sync/69334/1 "2026-09-17T17:59:42Z")

</div>

Hello, i need some help I’m using SDL3 to make a space shooter, just for fun not profit, i make the player move left or right up/down according to the position of the mouse

```c
int player_speed = 5;
SDL_AppResult SDL_AppIterate(void *appstate)
{
.... 

 if (mouse_pos.x > player_pos.x && player_pos.y+player_speed < screen_w) 

        {

            directionRight = 1;

            player_pos.x += player_speed;

        }

if (mouse_pos.x < player_pos.x && player_pos.x-player_speed > 0) 

        {

            directionLeft = 1;

            player_pos.x -= player_speed;

        }

```

the movement of the spaceship is broken it’s like they jumping stead of smooth following the mouse, i thin I need to get some delta time to smooth the movement of the spaceship but i don’t know how get dt, the V\_SYNC is set to SDL\_RENDERER\_VSYNC\_ADAPTIVE

thanks  
…  
}

---

<div class="post-metadata">

**Author:** ![Peter87](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/p/f07891/32.png) [@Peter87](https://discourse.libsdl.org/u/Peter87)\
**Post date:** [September 18, 2026, 8:39am UTC](https://discourse.libsdl.org/t/sdl2-help-to-smooth-player-update-based-on-v-sync/69334/2 "2026-09-18T08:39:05Z")

</div>

> [@gabrielsmoreira](#):
>
> it’s like they jumping

If it jumps back and forth when holding the mouse still I think it’s because you’re moving the player past the mouse position.

Instead of

```c
player_pos.x += player_speed;

```

you might want to do something like

```c
player_pos.x = SDL_min(player_pos.x + player_speed, mouse_pos.x);

```

_And the equvalent for moving left…_

* * *

> [@gabrielsmoreira](#):
>
> I need to get some delta time to smooth the movement of the spaceship but i don’t know how get dt

You could use a variable to keep track of the time of the last update.

```c
Uint64 last_update = SDL_GetTicks();

```

Before each update you then just calculate the dt by subtracting the current time from the time of the last update.

```c
Uint64 now = SDL_GetTicks();
double dt = (now - last_update) / 1000.0;
last_update = now;

```

The reason I initialize `last_update` to `SDL_GetTicks()` is to ensures that `dt` does not become too big the first time, but it might still be a good idea to put a limit on how big you allow `dt` to be (which will result in the game running slower if it’s not able to keep up) to avoid problems such as player moving through walls.

I divided the time difference by 1000.0 and stored `dt` as a floating-point number to get the time in seconds instead of milliseconds. This might not be entirely necessary but I think it simplifies things.

Now you just need to multiply the speed variables by `dt` when moving the player. The speed variables should hold the distance that you want the player to move _per second_.

E.g. moving right then becomes:

```c
player_pos.x += dt * player_speed;

```

Or, if you use my updated code that should prevent the player from moving past the mouse position it instead becomes:

```c
player_pos.x = SDL_min(player_pos.x + dt * player_speed, mouse_pos.x);

```

You probably want to store the player position as floating-point numbers (not as integers) to avoid bad “rounding errors” (truncation) which could result in the player moving noticeably slower (or not at all) if the speed is low and/or the update frequency is high.

This is one of the simplest ways to handle it. The classic article [Fix Your Timestep!](https://gafferongames.com/post/fix_your_timestep/) describes other (possibly better) ways to do it. What I have described above is what the article call “Variable delta time”. I think it’s a good article, and I recommend you read it, but I feel the last part “The final touch” might be a bit overkill, especially for a beginner. The article mentions a function called `integrate` which is just a fancy name for a function that takes the `dt` as argument and updates the state of the game by that amount. If you use the SDL callbacks, the body of the `while ( !quit )` loop would be what you put inside your `SDL_AppIterate` function.

---

<div class="post-metadata">

**Author:** ![gabrielsmoreira](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/g/bbe5ce/32.png) [@gabrielsmoreira](https://discourse.libsdl.org/u/gabrielsmoreira)\
**Post date:** [September 18, 2026, 2:33pm UTC](https://discourse.libsdl.org/t/sdl2-help-to-smooth-player-update-based-on-v-sync/69334/3 "2026-09-18T14:33:54Z")

</div>

Worked perfectly, the article is very helpful too.

Thank you.
