float coordinates and movement

Started by
1 comment, last by password 17 years, 7 months ago
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:

pX += xSpeed * deltaTime;
pY += ySpeed * deltaTime;
    
SDL_Rect player={pX,pY,50,50};
SDL_FillRect(window, &player, SDL_MapRGB(window->format, 0xFF, 0xFF, 0xFF));

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.
Advertisement
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.

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
Ring3 Circus - Diary of a programmer, journal of a hacker.
So it's okay to typecast them to integers? It compiles, but the movements are really slow and strange enough it can only move from left to right. Another weird thing is that the y-position (pY) is 250 but the block is positioned on 0.

This topic is closed to new replies.

Advertisement