Public Group

# float coordinates and movement

This topic is 4323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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

##### Share on other sites
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.

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002052
×