2d animation help

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
itsjuan2001
Posts: 12
Joined: Tue Jul 05, 2005 7:05 pm

2d animation help

Post by itsjuan2001 »

hello, I am having a small problem with my animation function.
it goes too fast.

Code: Select all

// draw faster flying imp 
driver->draw2DImage(images, core::position2d<s32>(240,240),
Test_Sprite(35), 0, video::SColor(255,255,255,255), true);

calls the function Test_Sprite passing the paramenter 35 (for the speed, its basically how many frames to wait until it changes the frame to another simulating animation)

Code: Select all

core::rect<s32> Game_Main::Test_Sprite(int counter_speed)
{
	framecounter += 1;

	if(framecounter == counter_speed)
	{
	 //do stuff

		framecounter = 0;

		if (frameholder = maxframes)
		{
			frameholder = 0;
		}
		else
		{
			frameholder += 1;
		}
		return frames[1];
	}
	else
	{
	return frames[0];
	}
};
thats the function above.
also i had the change the return value to 1 and 0 it used to be a variable but for some reason it seemed to not work it would return the value + 1, so i changed it temporarily to see if the animation timing system, which i want to work based on FPS would work.
problem is, it goes to fast.

is there a way to control the speed and make go (var) speed on all computers not going faster or slower on better comps?
jrm
Posts: 111
Joined: Tue Dec 13, 2005 8:57 pm

Post by jrm »

use the time:

Code: Select all

u32 Animationtime = device->getTimer()->getTime();

u32 NewTime = device->getTimer()->getTime();

if(NewTime > Animationtime + Frame) //milliseconds
{
 .....animation change code 
}
JRM
itsjuan2001
Posts: 12
Joined: Tue Jul 05, 2005 7:05 pm

Post by itsjuan2001 »

i saw the time function used in the 2d example as well, i did'nt use it cause i am not sure how it works.
does it reset to zero after it hits a certain number, or do i have to reset it?
jrm
Posts: 111
Joined: Tue Dec 13, 2005 8:57 pm

Post by jrm »

this is how you would use it:

Code: Select all




have a private member called u32 Animationtime 

core::rect<s32> Game_Main::Test_Sprite(int counter_speed)
{
   u32 NewTime = device->getTimer()->getTime();

   if(NewTime > Animationtime + counter_speed)
   {
    //do stuff
      Animationtime = device->getTimer()->getTime();

      if (frameholder = maxframes)
      {
         frameholder = 0;
      }
      else
      {
         frameholder += 1;
      }
      return frames[1];
   }
   else
   {
   return frames[0];
   }
}; 



I think this would work, I didn't test this code, but I think the logic is there that you can get the idea... Does that make sense?

JRM
Avalanche
Posts: 18
Joined: Tue Sep 05, 2006 7:21 am

Post by Avalanche »

even the getTime() is not accurate enough, and will slow down during low fps

i suggest you do a time slicing

Code: Select all

float freq = 1.0f/120.0f; // how many times u want to update per second
float accumulative_step = 0.0f;
u32 lasttick = timer->getRealTime();//timer is your device->getTimer()
u32 curtick = 0;

//loop
while(device->run())
{
	curtick = timer->getRealTime();
	accumulative_step += (curtick - lasttick);
	lasttick = curtick;
	while (accumulative_step >= freq)
	{
		//do something
		accumulative_step -= freq;
	}
	driver->beginScene(true, true, video::SColor(0,255,255,255));
	smgr->drawAll();
	driver->endScene();	
}
this way, when frame rate is high, renderer have more priorty
when frame rate is low, //do something have more priorty

this is especially usefull for physics also
Post Reply