SDL delta time

I'm trying to get smooth sprite movement in SDL by using delta time. The way I'm calculating it is

1
2
3
4
5
6
7
8
9
long last = 0;
float deltaTime = 0.0;
...		
long now = SDL_GetTicks();
//deltatime is in seconds
if (now > last) {
	deltaTime = ((float)(now - last)) / 1000;
	last = now;
}


and I'm updating the sprite like this:

1
2
3
4
virtual void update(float deltaTime) {
	float dx = 100*deltaTime;
	transform->position.x += dx;
}


But the movement is still noticeably jerky. Also, if I'm correct, my sprite should move 100 pixels to the right every second using this method, so it should take the sprite 8 seconds to reach the end of my window, since it's 800 pixels wide, but it takes it way longer than that. In other words the delta time I'm calculating can't be correct. Am I doing something horribly wrong? I'm guessing it has something to do with rounding, but I'm not sure.


Fafner
I'm guessing it has something to do with rounding, but I'm not sure.

If you store the position as integers that is probably the cause. What you should do instead is to store the position as a float (or double).
Yep, that was it! Thanks :)
Topic archived. No new replies allowed.