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
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
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:
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:
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! 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.