about animation

ghostatma

13-03-2010 10:46:04

hi
can any one tell me that ,how can i move or animate two robot parallel in one scene
pls i am trying from two days and i am new in this field
The exact question is in Expert Questions of Intermediate Tutorial 1
i have done the code like that shown below but my robot2 is not animated

#include "ExampleApplication.h"

#include <deque>
using namespace std;
//Ogre::mAnimationState->addTime(evt.timeSinceLastFrame);

class MoveDemoListener : public ExampleFrameListener
{
public:
//mAnimationState->addTime(evt.timeSinceLastFrame);

MoveDemoListener(RenderWindow* win, Camera* cam, SceneNode *sn1,SceneNode *sn2,
Entity *ent1,Entity *ent2, std::deque<Vector3> &walk1,std::deque<Vector3> &walk2)
: ExampleFrameListener(win, cam, false, false), mNode1(sn1),mNode2(sn2), mEntity1(ent1),mEntity2(ent2), mWalkList1(walk1),mWalkList2(walk2)
{
// Set idle animation
/*mAnimationState1 = ent1->getAnimationState("Idle");
mAnimationState1->setLoop(true);
mAnimationState1->setEnabled(true);

mAnimationState2 = ent2->getAnimationState("Idle");
mAnimationState2->setLoop(true);
mAnimationState2->setEnabled(true);*/

// Set default values for variables
mWalkSpeed = 35.0f;
mDirection1 = Vector3::ZERO;
mDirection2 = Vector3::ZERO;



} // MoveDemoListener

/* This function is called to start the object moving to the next position
in mWalkList.
*/
bool nextLocation()
{
/*mAnimationState1->addTime(evt.timeSinceLastFrame);
mAnimationState2->addTime(evt.timeSinceLastFrame);*/


if (mWalkList1.empty())
return false;
mDestination1 = mWalkList1.front(); // this gets the front of the deque
/**mDestination = mWalkList2.front();*/
mWalkList1.pop_front(); // this removes the front of the deque
/**mWalkList.pop_front(); */

mDirection1 = mDestination1 - mNode1->getPosition();
mDirection1 = mDirection1 - mNode2->getPosition();
mDistance1 = mDirection1.normalise();

Vector3 src = mNode1->getOrientation() * Vector3::UNIT_X;
// Vector3 srp = mNode2->getOrientation() * Vector3::UNIT_X;
Ogre::Quaternion quat = src.getRotationTo(mDirection1);
/**Ogre::Quaternion puat = srp.getRotationTo(mDirection);*/
mNode1->rotate(quat);
/**mNode2->rotate(puat);*/

/////////////////////////////////////////////////////////////////////////////////////////////

if (mWalkList2.empty())
return false;
mDestination2 = mWalkList2.front(); // this gets the front of the deque
mWalkList2.pop_front(); // this removes the front of the deque

mDirection2 = mDestination2 - mNode2->getPosition();
mDirection2 = mDirection2 - mNode2->getPosition();
mDistance2 = mDirection2.normalise();

Vector3 srp = mNode2->getOrientation() * Vector3::UNIT_X;
Ogre::Quaternion puat = srp.getRotationTo(mDirection2);
mNode2->rotate(puat);



return true;
} // nextLocation()

//mAnimationState1->addTime(evt.timeSinceLastFrame);

bool frameStarted(const FrameEvent &evt)
{
if (mDirection1 == Vector3::ZERO)
{
if (nextLocation())
{
// Set walking animation
mAnimationState= mEntity1->getAnimationState("Walk");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
}
}
else
{
Real move = mWalkSpeed * evt.timeSinceLastFrame;
mDistance1 -= move;
if (mDistance1 <= 0.0f)//overshoot the entity1 as it approach rhe target
{
mNode1->setPosition(mDestination1);
mDirection1 = Vector3::ZERO;
// Set animation based on if the robot has another point to walk to.
if (! nextLocation())
{
// Set Idle animation
mAnimationState = mEntity1->getAnimationState("Idle");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
}
else
{
// Rotation Code will go here later
}
}

else
{
mNode1->translate(mDirection1 * move);//rotate the face of robot in the dir it has to move
} // else

//mAnimationState->addTime(evt1.timeSinceLastFrame);
//return false;
} //mAnimationState->addTime(evt1.timeSinceLastFrame);
//return ExampleFrameListener::frameStarted(evt1);
//}

/*mAnimationState1->addTime(evt.timeSinceLastFrame);*/
//return ExampleFrameListener::frameStarted(evt);

// if

//
//mAnimationState->addTime(evt.timeSinceLastFrame)
//bool frameStarted(const FrameEvent &evt2)
//{
if (mDirection2 == Vector3::ZERO)
{
if (nextLocation())
{
//Set walking animation
mAnimationState = mEntity2->getAnimationState("Walk2");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
}
}
else
{
Real move = mWalkSpeed * evt.timeSinceLastFrame;
mDistance2 -= move;
if (mDistance2 <= 0.0f) //overshoot the entity2 as it approach rhe target
{
mNode2->setPosition(mDestination2);
mDirection2 = Vector3::ZERO;
// Set animation based on if the robot has another point to walk to.
if (! nextLocation())
{
// Set Idle animation
mAnimationState= mEntity2->getAnimationState("Idle");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
}
else
{
// Rotation Code will go here later
}
}
else
{
mNode2->translate(mDirection2 * move);//rotate the face of robot in the dir it has to move
} // else

// mAnimationState->addTime(evt.timeSinceLastFrame);
//return true;
} // if

mAnimationState->addTime(evt.timeSinceLastFrame);

return ExampleFrameListener::frameStarted(evt);
}
protected:
Real mDistance1; // The distance the object has left to travel
Real mDistance2;
Vector3 mDirection1; // The direction the object is moving
Vector3 mDirection2;
Vector3 mDestination1; // The destination the object is moving towards
Vector3 mDestination2;
AnimationState *mAnimationState; // The current animation state of the object
//AnimationState *mAnimationState2;

Entity *mEntity1; // The Entity1 we are animating
Entity *mEntity2; //The Entity2 we are animating
SceneNode *mNode1; // The SceneNode that the Entity is attached to
SceneNode *mNode2;
std::deque<Vector3> mWalkList1; // The list of points we are walking to
std::deque<Vector3> mWalkList2;
Real mWalkSpeed; // The speed at which the object is moving
};


class MoveDemoApplication : public ExampleApplication
{
protected:
public:
/*MoveDemoApplication()
{
}

~MoveDemoApplication()
{
}*/
protected:
Entity *mEntity1; // The entity1 of the object we are animating
Entity *mEntity2;
SceneNode *mNode1; // The SceneNode of the object we are moving
SceneNode *mNode2;
std::deque<Vector3> mWalkList1; // A deque containing the way points
std::deque<Vector3> mWalkList2;

void createScene(void)
{
// Set the default lighting.
mSceneMgr->setAmbientLight(ColourValue(1.0f, 1.0f, 1.0f));

//creat two robot from same position

//Create the entity
mEntity1 = mSceneMgr->createEntity("Robot1", "robot.mesh");

// Create the scene node
mNode1 = mSceneMgr->getRootSceneNode()->
createChildSceneNode("RobotNode1", Vector3(0.0f, 0.0f, 25.0f));
mNode1->attachObject(mEntity1);

// Create the entity
mEntity2 = mSceneMgr->createEntity("Robot2", "robot.mesh");

// Create the scene node
mNode2 = mSceneMgr->getRootSceneNode()->
createChildSceneNode("RobotNode2", Vector3(0.0f, 0.0f, -25.0f));
mNode2->attachObject(mEntity2);

////tell the robot where he needs to be moved to.
//// Create the walking list
mWalkList1.push_back(Vector3(550.0f, 0.0f, 50.0f ));
mWalkList1.push_back(Vector3(-100.0f, 0.0f, -200.0f));

mWalkList2.push_back(Vector3(550.0f, 0.0f, -50.0f));
mWalkList2.push_back(Vector3(-100.0f, 0.0f, 200.0f));


//// Create objects so we can see movement
Entity *ent1;
Entity *ent2;
SceneNode *node1;
SceneNode *node2;

ent1 = mSceneMgr->createEntity("Knot1", "knot.mesh");
node1 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot1Node",
Vector3(0.0f, -10.0f, 25.0f));
node1->attachObject(ent1);
node1->setScale(0.1f, 0.1f, 0.1f);

ent2 = mSceneMgr->createEntity("Knot2", "knot.mesh");
node2 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot2Node",
Vector3(0.0f, -10.0f, -25.0f));
node2->attachObject(ent2);
node2->setScale(0.1f, 0.1f, 0.1f);


ent1 = mSceneMgr->createEntity("Knot3", "knot.mesh");
node1 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot3Node",
Vector3(550.0f, -10.0f, 50.0f));
node1->attachObject(ent1);
node1->setScale(0.1f, 0.1f, 0.1f);

ent2= mSceneMgr->createEntity("Knot4", "knot.mesh");
node2= mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot4Node",
Vector3(550.0f, -10.0f, -50.0f));
node2->attachObject(ent2);
node2->setScale(0.1f, 0.1f, 0.1f);



ent1 = mSceneMgr->createEntity("Knot5", "knot.mesh");
node1 = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot5Node",
Vector3(-100.0f, -10.0f,-200.0f));
node1->attachObject(ent1);
node1->setScale(0.1f, 0.1f, 0.1f);

ent2 = mSceneMgr->createEntity("Knot6", "knot.mesh");
node2= mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot6Node",
Vector3(-100.0f, -10.0f,200.0f));
node2->attachObject(ent2);
node2->setScale(0.1f, 0.1f, 0.1f);


/**ent = mSceneMgr->createEntity("Knot4", "knot.mesh");
node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Knot4Node",
Vector3());
node->attachObject(ent);
node->setScale(0.1f, 0.1f, 0.1f);*/

// Set the camera to look at our handiwork
mCamera->setPosition(90.0f, 280.0f, 535.0f);
mCamera->pitch(Degree(-30.0f));
mCamera->yaw(Degree(-15.0f));






}

void createFrameListener(void)
{
mFrameListener= new MoveDemoListener(mWindow, mCamera, mNode1,mNode2,mEntity1,mEntity2,mWalkList1,mWalkList2);
mFrameListener->showDebugOverlay(true);
mRoot->addFrameListener(mFrameListener);
}

};

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"


INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
{
// Create application object
MoveDemoApplication app;

try {
app.go();
} catch(Exception& e) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBox(NULL, e.getFullDescription().c_str(), "An exception has occurred!",
MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
fprintf(stderr, "An exception has occurred: %s\n",
e.getFullDescription().c_str());
#endif
}


return 0;
}
thank you in Advance

PJani

14-03-2010 11:53:45

put your code in (code) yourcode (/code) - replace ( ) with [ ], because its unreadable.