• 13
• 27
• 9
• 9
• 20

Slow it down.

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

Recommended Posts

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?

Share on other sites
use timeGetTime to calculate the time between each frame and divide by 1000
then multiply that number to the speed of the objects

Share on other sites
To clarify:

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.

Share on other sites
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! :)

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

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

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