Sign in to follow this  

float coordinates and movement

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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
Admiral

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this