time slice

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Guest

time slice

Post by Guest »

quik question:

i need to calculate a variable (float) that i could multiply by any value(movement) to get a movement based on time instead of framerate

i tried doing 1/framerate but that doesnt work since the framerate is only calculated every second
jikbar
Posts: 62
Joined: Wed Aug 25, 2004 4:48 pm
Location: Canada
Contact:

Post by jikbar »

oops, that was me :oops:

Revolution3d, the engine i was using before i found irrlicht, had a ScaleValue() function where you could use movement = 1 * ScaleValue(1) to have your whatever move 1 unit every second, instead of movement = 1 which would give you 1 unit of movement every frame

BTW: i switched to irrlicht because it was faster and much less buggy
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

I am just working on implementing the same thing to my project.
You can get time in miliseconds using ITimer class.

Basicly you get time each loop (frame) of your program and store it. Next loop you get time again and compare it with time you got last loop. Diference is time passed between loops. If you multiply this walue with your speed walue, you get movement independant on framerate.

But there is one small problem: If your framerate is too high, diference can be smaller than 1 milisecond. In this case diference will be = 0.

To make sure this canot happen I made this class:

Code: Select all

class MTimer
{
public:
    //--- CONSTRUCTOR ---
    MTimer(IrrlichtDevice *device)
    {
        time = device->getTimer()->getTime();
        oldtime = time;
        looptime = 1;
         loopnum = 1;
    }
    
    //--- DESTRUCTOR ---
    ~MTimer()
    {
    }
    
    //--- CONTROL, should be called each loop ---
    void control(IrrlichtDevice *device)
    {
        time = device->getTimer()->getTime();
        
        u32 diference = time - oldtime;
        
        if(diference == 0)
        {
            loopnum += 1;
        }
        else
        {
            looptime = diference / loopnum;
            loopnum = 1;
            oldtime = time;
        }
    }
    
    //--- GET TIME OF ONE LOOP IN MILISECONDS ---
    f64 getLoopTimeMil()
    {
        return looptime;
    }
    
    //--- GET TIME OF ONE LOOP IN SECONDS ---
    f64 getLoopTimeSec()
    {
        return (looptime / 1000);
    }
    
    
protected:
    u32 time;         //curent time
    u32 oldtime;     //old time
    f64 looptime;    //time passed between loops (frames)
    u32 loopnum;   //number of loops to consider when calculating diference
};
I was not testing this class enough till now so there might be some bugs.
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

Hmmm. Your working with loopnumber which depends on the speed (FPS) again, doesn't it? Anyway, I got my own Timer class implemented in my project. But how do you use that movementvalue now?
General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

If time between loops is les than 1 milisecond diference between old and new time will be 0 since getTime() return integer.

What I do in such a case is to calculate diference between 2 loops, if its still 0 I add another loop and so on. That's why I need to store number of loops between old and new time.

When you divide diference by number of loops you have 1 loop time. Since looptime is float you can record diference smaller than 1 milisecond in this way.

Once you have your loop time you just multiply it with speed value.

Say:
looplime is 0.0005 second
speed of your object is 10 units per second
you have to move your object 10*0.0005 units far in one loop
spsteam
Posts: 9
Joined: Mon Oct 04, 2004 8:52 am
Location: Spain
Contact:

Post by spsteam »

If you want to see times below the millisecond you need to use the QueryPerformanceFrequency() windows api function. You can see an example here:

http://www.codeproject.com/cpp/precisetimer.asp
---------------------------------------------
http://usuarios.lycos.es/spsteam/
Post Reply