Simple 3rd person camera         Orbit camera like in Soul Reaver or Splinter Cell
Print

Introduction

Pre-requisites

This is an average tutorial, I encourage you to follow at least the two firsts basic 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:

.           Movement
              Node
             /    \  
            /      \ 
       Player     Camera
        Node       Node

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

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 );

Update the System with Input

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);

Afterwords

 

This tutorial is based on the 3rd person camera system tutorial tutorial by Kencho.

You can see also Quaternion and Rotation Primer and especially the part about the dual SceneNode trick to manipulate easily the camera.

 


Contributors to this page: sarcacid719 points  , spacegaier4386 points  , jacmoe133512 points  and AnomalousUnderdog .
Page last modified on Monday 15 of August, 2011 19:57:56 UTC by sarcacid719 points .


The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.