History: Simple 3rd person camera
Source of version: 5
- «
- »
Copy to clipboard
{maketoc}
!Introduction
!!Pre-requisites
This is an average tutorial, I encourage you to follow at least the two firsts basic ((Ogre Tutorials|tutorials)) before reading this one. This tutorial assumes you know how to manipulate the entities, the camera and the scene nodes.
!!Main goal
The idea of this tutorial is to have a camera orbiting freely around a character so that it can rotate with or without the camera.
It can be seen in many games, where the camera follows a character, but the user can modify the angle from where the character is seen. An example of an orbit camera is the one in Soul Reaver, or Splinter Cell (__IIRC. Someone confirm this!__), where the player can choose the angle, and move in different directions, always watching from the same angle.
!Getting started
!!How it works
There will be three scene nodes:
{CODE(wrap="1", colors="c++")} '''Movement'''
'''Node'''
/ / / / '''Player''' '''Camera'''
'''Node''' '''Node'''
{CODE}
Both __PlayerNode__ and __CameraNode__ will be children of the __MovementNode__ one. All the three nodes are at the same position. The translations will be executed on the movement node while the rotations will be executed either on the camera node, or on the player node. We'll have to be careful on the movements because they depend directly to the player's direction.
!!Prepare the sources
!!!Files list
For the foundations, we will use the ExampleApplication.h and ExampleFrameListener.h files and we will directly modify these files. We can't derived the classes because there is already a camera management that we need to rewrite.
* ExampleApplication.h
* ExampleFrameListener.h
!!!Source code
__To be written__
!!Let's get it on!
!!!Creation of the nodes
__To be written__
!!!Creation of the listener
__To be written__
!!!Just a Pseudocode snippet
__Create the Node Structure__
{CODE(wrap="1", colors="c++")} MainNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("MainNode");
MainNode->translate(Vector3(0, 100, 0));
PlayerNode = MainNode->createChildSceneNode("PlayerNode");
CameraNode = MainNode->createChildSceneNode("CameraNode");
CameraPitchNode = CameraNode->createChildSceneNode("CameraPitchNode");
CameraPitchNode->attachObject (mCam);
CameraNode->setPosition(0, 150, 150);
PlayerEntity = mSceneMgr->createEntity("Robot.mesh");
mAnimationState = PlayerEntity->getAnimationState( "Idle" );
mAnimationState->setLoop( true );
mAnimationState->setEnabled( true );
{CODE}
__Update the System with Input__
{CODE(wrap="1", colors="c++")} Ogre::Vector3 Direction = MainNode->getOrientation()*Ogre::Vector3::NEGATIVE_UNIT_Z;
Direction.normalise();
Ogre::Vector3 Movement;
Movement = Vector3(0,0,0);
mAnimationState->setEnabled(false);
if(Input->Up)
{
Movement += Direction;
}
if(Input->Down)
{
Movement += -Direction;
}
if(Input->Right)
{
Movement.x += Direction.z * -1;
Movement.z += Direction.x;
}
if(Input->Left)
{
Movement.z += Direction.x * -1;
Movement.x += Direction.z;
}
if(Movement.x == 0 && Movement.z == 0)
{
mAnimationState = PlayerEntity->getAnimationState( "Idle" );
}
else
{
Movement = Movement * Time * mWalkSpeed;
MainNode->translate(Movement);
mAnimationState = PlayerEntity->getAnimationState( "Walk" );
}
MainNode->yaw(Input->mRotX);
CameraPitchNode->pitch(Input->mRotY);
mAnimationState->setLoop( true );
mAnimationState->setEnabled( true );
mAnimationState->addTime(Time);
{CODE}
!Afterwords
!!Related tutorials
This tutorial is based on the ((3rd person camera system tutorial)) tutorial by ((User:Kencho|Kencho)).
You can see also ((Quaternion and Rotation Primer)) and especially the part about the ((Quaternion and Rotation Primer|#Misc|dual SceneNode)) trick to manipulate easily the camera.