Problem with doing simple animation

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
manasij7479
Posts: 1
Joined: Sun Mar 18, 2012 5:16 am

Problem with doing simple animation

Post by manasij7479 »

I started learning to use Irrlicht just now and can't seem to get this to work.
I want the character to reverse directions when it crosses a certain distance in the x direction, but that is happening only for the positive direction.
I'm probably doing a stupid mistke, but please look at the code.

Code: Select all

 
#include<irrlicht/irrlicht.h>
using namespace irr;
using namespace core;
using namespace video;
using namespace scene;
using namespace io;
int main()
{
        IrrlichtDevice* device = createDevice(EDT_OPENGL,dimension2d<u32>(800,600),32,false,false,false,0);
        if(!device)return 1;
        
        IVideoDriver* driver = device->getVideoDriver();
        ISceneManager* smgr = device->getSceneManager();
        
        IAnimatedMesh* m = smgr->getMesh("../../media/sydney.md2");
        IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(m);
        
        if(node)
        {
                node->setMaterialFlag(EMF_LIGHTING,false);
                node->setMaterialTexture(0,driver->getTexture("../../media/sydney.bmp"));
                
                node->setMD2Animation(scene::EMAT_RUN);
        }
        
        smgr->addCameraSceneNode(0,vector3df(0,0,-500),vector3df(0,0,0));
        
        double v = -.0001, x = 0.0;
        double then  = device->getTimer()->getTime();
        
        while(device->run())
        {
                driver->beginScene(true,true,SColor(255,255,255,255));
                        double now  = device->getTimer()->getTime();
                        double dt = then - now;
                        x+=v*dt;
                        node->setPosition(vector3df(x,0,0));
                        if(x>100||x<(-100))
                        {
                                v=-v;
                                node->setRotation(vector3df(0,180,0));
                        }
                        smgr->drawAll();
                        
                driver->endScene();
        }
        
        device->drop();
        
        return 0;
}
 
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Problem with doing simple animation

Post by Nadro »

Irrlicht doesn't automatically add new rotation value to an older, so You must do it manualy, or set rotation to a 180 when x < -100 and set rotation to a 0 when x > 100.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
gerdb
Posts: 194
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: Problem with doing simple animation

Post by gerdb »

Hi, as Nadro said

node->setRotation(vector3df(0,180,0));

sets! the y rotation, and does not add anything

node->setRotation(vector3df(0,node->getRotation().Y + 180.0f,0));

--> this adds 180 to the current value
blAaarg
Posts: 94
Joined: Tue Mar 02, 2010 9:11 pm
Location: SoCal

Re: Problem with doing simple animation

Post by blAaarg »

One thing is:

Code: Select all

double dt = then - now;  /*should at some point, update the 'then' value, like so:*/ then = now;
otherwise, the "then - now value" (i.e. the "dt" value) quickly gets HUGE!! and no puny little "x" value will have a visible effect.

Also, shouldn't it be "now - then" to find the difference in time between the current loop and the previous one? ('now' will be a larger value than 'then' since the engine's time values go 'up' as time moves forward.) Although, that means v should be a positive value.

Also, getTime() returns a u32 (unsigned integer, so...it's always positive) so converting to a double is a bit of a waste. [double now = ....]

Also, you should try to remove those calculations from in between the begin() and end() calls. It should be:

loopStart...// pseudocode
begin();
drawAll();
end();

// do the updating stuff you were doing

loopEnd...// pseudocode
"Computers don't make mistakes! What they do they do on purpose!!"

-Dale Gribble
Post Reply