pX += xSpeed * deltaTime;
pY += ySpeed * deltaTime;
SDL_Rect player={pX,pY,50,50};
SDL_FillRect(window, &player, SDL_MapRGB(window->format, 0xFF, 0xFF, 0xFF));
float coordinates and movement
I wanted to have a tolerable timer in my game and show FPS aswell so I added deltatime in the movements of the player and a frame counter. I haven't tried it yet because the program won't compile when using floats.
I've made a temporary block that can be moved:
The problem is that I can't use floats for movement because it can only handle integers. This is something that has been bothering me for a while. I read about deltatime from a tutorial, so it shouldn't be totally out of the question to use float's for movement, because it apparently works for other people.
I get error messages like this:
[Warning] converting to `Sint16' from `float'
So what is the catch? Converting the floats would be the same as using integers.
If you use integers for everything, you'll find that all of your variables will be quantised, maybe unacceptably, and that you'll have minimum speed.
The most common way to deal with this is to use floats for all storage and calculations, but to cast them back to integers when passing to functions that need integral input.
Regards
Admiral
The most common way to deal with this is to use floats for all storage and calculations, but to cast them back to integers when passing to functions that need integral input.
float pX, pY;float xSpeed, ySpeed;// ... Initialise values ...void UpdateAndDraw() { static long LastTickCount = GetTickCount(); float deltaTime = (float) (GetTickCount() - LastTickCount) / 1000.0f; LastTickCount = GetTickCount(); // Perform movement pX += xSpeed * deltaTime; pY += ySpeed * deltaTime; SDL_Rect player={(long) pX, (long) pY, 50, 50}; SDL_FillRect(window, &player, SDL_MapRGB(window->format, 0xFF, 0xFF, 0xFF));}
Regards
Admiral
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement