   SDL Velocity Problem
  My screen size in SDL is 800 x 600, which is not really small, but whenever I try to implement velocity, the images go much too fast. I have set an image to increase velocity by 1 each frame, at 30 fps. So that becomes an arithmetic series with d = 1 and n1 = 0. Term 30 would be 29, so sum30 = 30((0 + 29) / 2) = 435 pixels per second. The image crosses the screen in under two seconds, while I need it to cross in around 10 seconds!! How can I fix this?   
  Assuming you want a constant velocity, and move an object a fixed distance in a fixed time interval30fps * 10sec = 300frames800 pixels /  300frames = 2.666pixels per framesSo you should add approximately 2.666 pixels to the object position each frame.If on the other case you actually increase the velocity each frame by a value, you are talking about (constant) acceleration.At an acceleration of 1 pixel per frame, of course it don't take long before your object fly through the screen, it should be much less. For the exact value you have to calculate the derivative. See some mathematical sites for that.*Quick calculus,.. would it be 0.017666pixels/frames^2 ? I'm not quite sure myself*[Edited by - Dunge on June 2, 2010 9:51:11 PM];  It seems to me like you're simply increasing the rate of the velocity too fast; scaling it back down would be a good idea. From what I can understand from your post and without seeing any code, the velocity increases by 1 each frame at a rate of 30 frames each second. Instead of doing it for every frame, try increasing it by 1 once every 5 frames. To accurately mimic real world velocity, you'd have to take in other factors for acceleration, which are largely dependent upon what you're trying to achieve so I can't really help you any more on that end.If you're doing something like constant velocity, e.g. you want whatever image you have to cross 800 pixels in 10 seconds at 30fps, a bit of maths helps with this, mostly division -> ((distance/time in seconds)/frames per second) -> ((800px/10s)/30fps) being 2 and 2/3 pixels per second, although SDL, to my knowledge, will only blit in whole integer pixels.Some of this might have been redundant, as you might have already known it, but I hope it helps you a bit.EDIT: Aw man, beaten by Dunge...   ;  The trouble I have been having is as you say, SDL will only blit whole pixels. I tried setting the acceleration to something like .002, but because I have to convert to an integer before blitting, it only serves to cause a delay between the keypress and the image movement. The image movement itself though is still too fast.   ;  You can keep your velocity/position variables in float or double for precision, and when you get to blitting you cast them to integer. This shouldn't cause any delay of any kind and keeps precision going in your movement.   ;  Even at a velocity of 1, though, the image is moving too fast, and the cast truncates the number, so if I add +0.0001 velocity / frame, up until 0.9999 velocity, the image will sit there which is what causes the delay, then at 1.0000 velocity it goes flying off the screen. >:(This is the setup I'm using:if left/right is pressed, +- xVelocityif up/down is pressed, +- yVelocityeach frame, the image moves byx-position += xVelocityy-position += yVelocityPerhaps there is a better setup?   ;  It seems like you're still adding to the velocity on every frame. If you just had the velocity as constant without changing the velocities, you ought to get something that doesn't surprise you.Like I said, if you want to emulate acceleration a bit more accurately, try increasing the velocity by 1 once every 90 frames or something like that. Keeping tabs on the number of frames and then running a modulus operator would help in this regard too.   ;  I'm not sure how to do that, because the velocity is responding to the input of the arrow keys.Also, what is different in increasing velocity by 1/90th every frame from increasing velocity by 1 every 90th frame? I am not seeing the difference, I thought a velocity change in the decimals would be the same thing.   ;  There isn't a difference, but that's the way I would do it since SDL blits in integers. Increasing a double or float by 1/90 or adding 1 to the velocity after 90 frames give you the same effect. It's the same route, but it just doesn't deal with decimals. For some people, counting frames seems cleaner, but each method works out to have the exact effects on either side.Sorry, I didn't see this before, but I think I realize the problem you're having. Even when you're adding decimals, the integer casting rounds your values down to zero until it reaches 1.000, at which point, the image starts to move. If you're moving the image every frame, after 30 frames, one second, you'll have jumped 30 pixels in one second. Is this what your working code is doing, or something similar to it? If that's the case, then you'll probably need to reduce the amount of times you blit the image to the screen. Also, you might wanna display the important bits of your code for close examination.   ;  Yes, that is exactly what it is doing. I have already switched over to a static pixels/second speed, and given up on velocity. What I had was basically like this:#include "Include.h" //All libraries needed#define SPEED .01#define FPS 30int main(int argc, char* argv[]) {  bool quit = false;  double xVel = 0;  double yVel = 0;  int x = 0, y = 0;  Uint32 fps = SDL_GetTicks();  SDL_Surface* screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE)  //load() returns an optimized, colorkeyed image  SDL_Surface* test = load("Images\\dot.png");   while(quit == false) {    while(SDL_PollEvent(&events)) { if(events.type == SDL_QUIT) {   quit = true;   break; }    }Uint8* keystates = SDL_GetKeyState(NULL);    if(keystates[SDLK_UP] || keystates[SDLK_w]) { yVel -= SPEED;    }    if(keystates[SDLK_DOWN] || keystates[SDLK_s]) { yVel += SPEED;    }    if(keystates[SDLK_LEFT] || keystates[SDLK_a]) { xVel += SPEED;    }    if(keystates[SDLK_RIGHT] || keystates[SDLK_d]) { xVel -= SPEED;    }x += xVel; y += yVel;    apply(x, y, test, screen); //apply() is my blitting function    if((SDL_GetTicks() - fps) < (1000 / FPS)) { SDL_Delay((1000 / FPS) - (SDL_GetTicks() - fps));    }fps = SDL_GetTicks();    SDL_Flip(screen);}If I set SPEED too low, then it becomes unresponsive and very hard to control, if I set SPEED at 1.0 or greater, then it moves too fast.   