Camera move with an Object

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
Uma
Gnoblar
Posts: 23
Joined: Fri Mar 28, 2014 8:01 am

Camera move with an Object

Post by Uma » Fri May 23, 2014 10:57 am

Hi all,

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_
and

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
I'll be thank full if anyone can help me with this.
0 x

User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7152
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 19

Re: Camera move with an Object

Post by Kojack » Fri May 23, 2014 3:20 pm

One thing I spotted, in your frameRenderingQueued method you are clearing mMovement to 0,0,0 just before using it to move the main node. So it will always be 0,0,0, any key input will be wiped before it can be used.
Try moving the mMovement = Vector3(0,0,0); to just after the if/else block where the movement is done.
0 x

Uma
Gnoblar
Posts: 23
Joined: Fri Mar 28, 2014 8:01 am

Re: Camera move with an Object

Post by Uma » Mon May 26, 2014 7:03 am

Thanks for the reply,

I put mMovement = Vector3(0,0,0); after if-else block. But still it is same as before, Nothing appears on rendering window and nor any error.
0 x

iblues1976
Gnome
Posts: 379
Joined: Fri Sep 16, 2011 4:54 pm

Re: Camera move with an Object

Post by iblues1976 » Mon May 26, 2014 3:57 pm

While I have only dealt with First person camera, the principle should be similar. I can think of a few ways to have the camera followed the character. However, the one in the wiki you mention, seems to be the best option. Besides what Kojack mention, I have notice that you are not attaching the entity, like they do in the wiki.
I didn't see you were attaching the entity... (unless I didn't see it)

Code: Select all

  mEntity = mSceneMgr->createEntity (mName, "OgreHead.mesh");
  mMainNode->attachObject (mEntity);
hope this helps.
0 x

Uma
Gnoblar
Posts: 23
Joined: Fri Mar 28, 2014 8:01 am

Re: Camera move with an Object

Post by Uma » Fri May 30, 2014 8:11 am

Thanks for everyone's reviews. I have solved the problem using 1st person camera :D :D :D
0 x

Post Reply