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
time slice
oops, that was me
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
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
You'll need timebased movement. Some articles:
http://www.gamedev.net/reference/articl ... le1604.asp
http://www.gamedev.net/reference/articl ... le1382.asp
http://www.gamedev.net/reference/articl ... le1604.asp
http://www.gamedev.net/reference/articl ... le1382.asp
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:I was not testing this class enough till now so there might be some bugs.
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
};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
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
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://www.codeproject.com/cpp/precisetimer.asp
---------------------------------------------
http://usuarios.lycos.es/spsteam/
http://usuarios.lycos.es/spsteam/