Движение, независимое от частоты кадров (C++ и SDL)C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Движение, независимое от частоты кадров (C++ и SDL)

Сообщение Anonymous »


I am learning about gameloops in c++ and I have created code that can make movement (of a rectangle) independent of the fps.

Like so:

delta_time = (SDL_GetTicks64() - last_frame_time); last_frame_time = SDL_GetTicks64(); int player_speed = 1; // player control of box const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); if (currentKeyStates[SDL_SCANCODE_RIGHT]) rec_player.x +=(int)(delta_time) * player_speed * 0.5; if (currentKeyStates[SDL_SCANCODE_LEFT]) rec_player.x -=(int)(delta_time) * player_speed; if (currentKeyStates[SDL_SCANCODE_UP]) rec_player.y -= (int)(delta_time) * player_speed; if (currentKeyStates[SDL_SCANCODE_DOWN]) rec_player.y +=(int)(delta_time) * player_speed; This appears to work as intended (I have tested with various SDL_delay settings and the rectangle appears to move with the same speed even with low fps.

However as soon as I want to scale the movement speed (as seen in the line with the 0.5 miltiplicator at the end to a lower than 1 value, it breaks, the rectangle will move at a lower speed, however at very uneven speeds.

I also had to cast the thing to int because moving left would be slower because of a rounding error or something similar.

How to get the scaling of the speed to work? I think the issue lies with it being int and multiplying it by 0.5 appears to make the end result 0. Because the delta_time in program is usually 1-2 (miliseconds).

How to scale the speed of the object while retaining the synchronicity to the time?


Источник: https://stackoverflow.com/questions/780 ... -c-and-sdl
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»