Slow it down.

Started by
5 comments, last by mike25025 19 years, 5 months ago
I had trouble with my pong game being to fast on some computers. I was wondering how I would make it the same speed on each computer?
True God of the TribunalKelchargeMy SiteMy Ugly Forums
Advertisement
use timeGetTime to calculate the time between each frame and divide by 1000
then multiply that number to the speed of the objects
To clarify:
instead of something like:

void Update()
{
int SpeedX = 5;
int SpeedY = 3;
Ball.x += SpeedX;
Ball.y += SpeedY;
}

do this:

void Update()
{
static DWORD LastTime = timeGetTime();
float DeltaTime = (timeGetTime() - LastTime) / 1000.0f;

Ball.x += (int)(SpeedX * DeltaTime + 0.5f); //Adding 0.5f and casting to an int effectively rounds
Ball.y += (int)(SpeedY * DeltaTime + 0.5f);
LastTime = TimeGetTime();
}

You'll probably have to drastically increase your speed to get the desired result, so try using something like the above and then adjusting your speeds until you're happy with them.

Also make sure you link to winmm.lib, either through VC++ properites or by adding the line #pragma comment(lib, "winmm.lib") to your main source file.
To expand on what mike25025 was saying, the common approach is to make your movement time-based, instead of frame-based. All your objects should have a direction and speed (miles per hour, units per second, etc.) variable. Depending on your implementation, you may want to google for "3d vector math" - (I would anyway, since it will make things alot easier). Here's some pseudo-code:

.) main loop   .) get elapsed time since last frame   .) move your objects by speed*time   .) draw your objects


Hope this helps - good luck!

EDIT: ms291052 - you're too quick for my fingers! :)
there should be no need to round or cast
the ball positions should be stored as floats
then when rendering the will be casted into ints
The best way is to have a constant update frequency.
This way there is never any difference between two computers updating over the same period.

With a changing time difference the end results may differ.
A good example is that of Quake 3 and rocket jumping. Certain framerates would yield better results than others due to different time differences.
ap there are problems with using a constant update frequency
on fast computers it will look bad
and slow computers might be to slow to reach the frequency makeing the game very slow

using a time delta it will work well on all computers

This topic is closed to new replies.

Advertisement