Welcome! Log In Create A New Profile

Advanced

Getting time between loops

Posted by WiiPhlex 
Getting time between loops
September 13, 2008 10:11AM
I'm doing some physics programming and want to calculate displacement by distance over time rather than over loops as that can get rather messy, can I use the ticks_to_millisecs(gettime()) functions to calculate this?
Re: Getting time between loops
September 13, 2008 11:22PM
I had the same problem getting my wiimote to rumble for half a second. Ended up resorting to looping.
Re: Getting time between loops
September 14, 2008 03:57AM
cstdlib has some time functions (I think), I'm not going to use loops, It's just too messy.
Re: Getting time between loops
September 20, 2008 04:07AM
k well, I figured this out like a week ago, so for others who want to know here's the general idea behind it (thanks Dale)

int old = ticks_to_millisecs(gettime())
while(1) {
showaframeandtakeuserinput();

int new = ticks_to_millisecs(gettime())

int elapsed = new-old;

old=new;
updategamestate(elapsed);
}

just divide elapsed by 1000 to get the number of seconds, that way most of the physics formula which use seconds will be much cleaner.
Re: Getting time between loops
September 20, 2008 07:47AM
User input should be processed in an interrupt or thread, not in the main loop. That way if the game lags and the frame rate drops, the controls remain responsive.
Re: Getting time between loops
September 20, 2008 08:49AM
That is the main idea behind it, None of this was calcualted in the main loop, rather the first instance of old is declared in the enginers constructor and then the engine precedes to show a frame, get user input, then call another function to update the time since old has occured and then start again.
Sorry, only registered users may post in this forum.

Click here to login