🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Speed limit

Started by
9 comments, last by David20321 23 years, 11 months ago
I''m trying to make a speed limit so that my program runs at a constant speed. Right now I''m using if (TickCount()%2==1) then it does function DrawGL but that just makes everything go slower and it doesn''t level out the speed. My program has a spaceship and the program goes really fast when it''s not thrusting but when it does thrust and makes a few sprites then it slows down a little. I''m trying to make it constantly run at that slower speed.
Advertisement
Let TS be the time passed since the last frame draw.
Let object movement is represented by adding Velocity to the previous position.

To keep the constant speed move, scale velocity by TS.

For example, if velocity is just float, then you can define another variable mvelocity as

float mvelocity = velocity*TS;

and can use mvelocity instead of velocity. (velocity may have to be scaled to make the speed as desire)

Hope this helps.

GTW


That makes sense, but I don''t know how to measure time other than by ticks. My prog goes faster than 60 fps so I can''t measure the time between frames ''cuz it''s less than 1 tick.
I have some timer code you might be interested in. It can read less than 1ms time.

------------------------------

"My sword is like a menacing cloud, but instead of rain, blood will pour in its path." - Sehabeddin, Turkish Military Commander 1438.
------------------------------"My sword is like a menacing cloud, but instead of rain, blood will pour in its path." - Sehabeddin, Turkish Military Commander 1438.
Does it work on macs? If so, I''m interested.
quote: Original post by David20321
I can''t measure the time between frames ''cuz it''s less than 1 tick.


You can measure timepassed every 10 calls of draw (not every call of draw).

GTW

you don''t tell us what OS you''re running. but getitimer()/setitimer() is what you''re looking for.

I''m sure even windows9x offer similar functionality (if not by the same name).
try synchronizing it to a frame rate..

    do {DWORD start_time = GetTickCount();// render scene etc}//wait 30 MSwhile(GetTickCount() - start_time < 30);    


and remember, GetTickCount() is relative, you cant check for an exact value, just the value since the last call

hope this helps...
Rather than limit the frame rate, here''s a better approach, and a hell of a lot faster.

You basically let the game run as fast as it can (i.e. someone with a 120Hz refresh rate will see the best quality).

Take a look at the timer routines from one of NeHe''s tutorials. It''s not as good as it could be, but it works.

Imagine you''ve currently set up your variables so that it moves at the right speed when running at 85fps.

    ... WinMain loop ...fMultiplier = 1;while(!bDone){    ...    fStart = GetTime();    UpdateFrame(fMultiplier);    Render();    fEnd = GetTime();    fTimeTaken = fEnd - fStart;    fFramesPerSecond = 1000 / fTimeTaken;  // 1000 because it''s in milliseconds.    fMultiplier = 85 / fFramesPerSecond;}...void UpdateFrame(float fMultiplier){    ... Read acceleration/movement values for everything.    for (every value that makes a difference)    {        value *= fMultiplier;    }}    


What the code does is calculate how much faster or slower than normal the app is running, and multiplies the stuff accordingly.

If you find that your fps jerks a lot like this, then maybe only update every 10 frames etc.

That''s a professional way of doing it, and in fact I think that''s how most timers do it now.



========
Smidge
www.smidge-tech.co.uk
========
--Mr Smidge
GetTime seems to want me to give it a DataTimeRec. What''s a datatimerec? What am I supposed to give it?

I have the line "start=GetTime();" and it says:

Error : function call ''GetTime()'' does not match
''GetTime(DateTimeRec *)''
main.c line 139 start=GetTime();

This topic is closed to new replies.

Advertisement