I have to create an application for ogre Camera follows the object, I try to use 3rd person camera method. But I couldn't get proper output using that. My coding is as follows:
ObjectFollow.h
Code: Select all
#ifndef __ObjectFollow_h_
#define __ObjectFollow_h_
#include "BaseApplication.h"
#include "ExampleApplication.h"
#include "ExampleFrameListener.h"
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#include "../res/resource.h"
#endif
class ObjectFollow : public BaseApplication
{
public:
ObjectFollow(void);
virtual ~ObjectFollow(void);
protected:
virtual void createScene(void);
virtual void createFrameListener(void);
virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt );
Ogre::AnimationState *mAnimationState;
Ogre::SceneNode* MainNode;
Ogre::SceneNode* CameraNode;
Ogre::SceneNode* PlayerNode;
Ogre::SceneNode* CameraPitchNode;
Ogre::Entity* PlayerEntity;
Ogre::Real mWalkSpeed;
Ogre::Real Time;
Ogre::Vector3 mDirection;
Ogre::Vector3 mMovement;
};
#endif // #ifndef __ObjectFollow_h_
ObjectFollow.cpp
Code: Select all
#include "ObjectFollow.h"
ObjectFollow::ObjectFollow(void)
{
}
ObjectFollow::~ObjectFollow(void)
{
}
void ObjectFollow::createScene(void)
{
mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
MainNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("MainNode");
MainNode->translate(Vector3(0, 100, 0));
CameraNode = MainNode->createChildSceneNode("CameraNode");
PlayerNode = MainNode->createChildSceneNode("PlayerNode");
CameraPitchNode = CameraNode->createChildSceneNode("CameraPitchNode");
CameraPitchNode->attachObject(mCamera);
CameraNode->setPosition(0, 150, 150);
PlayerEntity = mSceneMgr->createEntity("robot.mesh");
mAnimationState = PlayerEntity->getAnimationState("Idle");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
}
void ObjectFollow::createFrameListener(void)
{
BaseApplication::createFrameListener();
CameraNode = mCamera->getParentSceneNode();
mWalkSpeed = 35.0f;
mDirection = Ogre::Vector3::ZERO;
}
bool ObjectFollow::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
mDirection = MainNode->getOrientation()*Ogre::Vector3::NEGATIVE_UNIT_Z;
mDirection.normalise();
mMovement = Vector3(0,0,0);
mAnimationState->setEnabled(false);
if(mMovement.x == 0 && mMovement.z == 0)
{
mAnimationState = PlayerEntity->getAnimationState("Idle");
}
else
{
mMovement = mMovement * Time * mWalkSpeed;
MainNode->translate(mMovement);
mAnimationState = PlayerEntity->getAnimationState("Walk");
}
mAnimationState->setLoop( true );
mAnimationState->setEnabled( true );
mAnimationState->addTime(Time);
if (mWindow->isClosed())
return false;
if (mShutDown)
return false;
mKeyboard->capture();
mMouse->capture();
mTrayMgr->frameRenderingQueued(evt);
return true;
}
bool ObjectFollow::keyPressed(const OIS::KeyEvent& evt)
{
switch (evt.key)
{
case OIS::KC_UP:
//mDirection.z = -mMove;
mMovement += mDirection;
break;
case OIS::KC_DOWN:
mMovement += -mDirection;
break;
case OIS::KC_LEFT:
mMovement.z += mDirection.x * -1;
mMovement.x += mDirection.z;
break;
case OIS::KC_RIGHT:
mMovement.x += mDirection.z * -1;
mMovement.z += mDirection.x;
break;
case OIS::KC_ESCAPE:
mShutDown = true;
break;
default:
break;
}
return true;
}
bool ObjectFollow::keyReleased(const OIS::KeyEvent& evt)
{
switch (evt.key)
{
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z = 0;
break;
case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z = 0;
break;
case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x = 0;
break;
case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x = 0;
break;
default:
break;
}
return true;
}
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char *argv[])
#endif
{
// Create application object
ObjectFollow app;
try {
app.go();
} catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occured: " <<
e.getFullDescription().c_str() << std::endl;
#endif
}
return 0;
}
#ifdef __cplusplus
}
#endif