const Uint8* keysState = SDL_GetKeyboardState(NULL);
float toMove = playerSpeed * dt;
if (keysState[SDL_SCANCODE_S]) {
player.y += toMove;
}
if (keysState[SDL_SCANCODE_D]) {
player.x += toMove;
}
if (keysState[SDL_SCANCODE_W]) {
player.y -= toMove;
}
if (keysState[SDL_SCANCODE_A]) {
player.x -= toMove;
}
when pressing the a key the player moves across the screen in roughly a second but when I press d to move from left to right it takes about 3 seconds to move across the screen. It is the same problem with pressing w and s. Is the += operator just slower than -=? Setting the player speed to <= 300 will make the player at an unbearably slow speed when moving from L to R but R to L is what I would expect 300 pixels per second to look like. rough idea of how the game loop looks like:
Uint32 lastTicks = SDL_GetTicks();
while (!quit) {
Uint32 nowTicks = SDL_GetTicks();
float dt = (nowTicks - lastTicks) / 1000.0f;
lastTicks = nowTicks;
update(dt);
}
I'm running this on linux through wsl2
I tried it with different player speeds but the problem still persisted. here are the constants:
#define SCREEN_WIDTH 640*2
#define SCREEN_HEIGHT 640
#define BLOCK_SIZE (SCREEN_WIDTH / 20)
#define BOARD_HEIGHT (SCREEN_HEIGHT / BLOCK_SIZE)
#define BOARD_WIDTH (SCREEN_WIDTH / BLOCK_SIZE)
float playerSpeed = 800;
Edit 1: changed player speed to a constant value Edit 2: I find that when I move my mouse around the speed of L to R and U to D movement increases. Edit 3: here's an imgur link to a gif showcasing the problem: https://imgur.com/a/wYD9b3P
Answers
The issue you're experiencing is likely related to the way you're handling input and updating the player's position. Let's analyze your code:
const Uint8* keysState = SDL_GetKeyboardState(NULL);
float toMove = playerSpeed * dt;
if (keysState[SDL_SCANCODE_S]) {
player.y += toMove;
}
if (keysState[SDL_SCANCODE_D]) {
player.x += toMove;
}
if (keysState[SDL_SCANCODE_W]) {
player.y -= toMove;
}
if (keysState[SDL_SCANCODE_A]) {
player.x -= toMove;
}
The code looks correct, and the movement should be consistent regardless of the direction (left, right, up, or down). However, the speed difference you're experiencing could be due to multiple factors:
-
Frame Rate: The movement calculation is based on the elapsed time (
dt
) between frames. If the frame rate fluctuates, the movement speed may appear inconsistent. Ensure that your game loop is running at a consistent frame rate. -
Input Handling: It's possible that there's some delay or inconsistency in how SDL handles key presses. Make sure that the keyboard input is being polled and processed correctly in your game loop.
-
Rendering: If there are other operations in your game loop that are affecting rendering performance, it could indirectly impact the movement speed. Ensure that rendering is efficient and not causing unnecessary delays.
-
Hardware Acceleration: Running the game through WSL2 might introduce some overhead or performance differences compared to running it directly on the hardware. Consider testing the game on native Linux or Windows to see if the issue persists.
-
Mouse Input: You mentioned that moving the mouse affects the movement speed. It's possible that mouse input events are interfering with keyboard input events or affecting the performance of your game loop. Ensure that mouse input is handled properly and does not impact movement speed unintentionally.
To troubleshoot the issue further, you can try the following:
- Print debug information to track the elapsed time (
dt
) and the calculated movement distance (toMove
) for each frame. - Check if the frame rate is consistent and matches your expectations.
- Experiment with different input handling methods or adjust the game loop to see if it affects the movement speed.
By isolating and testing different components of your game loop, you should be able to identify the root cause of the speed inconsistency.