3RD Person camera problems(Solved Well Sort Of)


08-06-2010 00:17:45

Hi there, I have a few questions about auto track and tracking an object.

First off i am working on a camera class and while there does appear to be a few tutorials on this subject i found that certain parts of the code or even just the explanation warranted a better explanation.

So as for the setAutoTrack function i am using a node to be a camera target but i want the camera to rotate around this object. When using auto track i assume that the object is simply locked on to it from where ever it is eg LookAt like functionality. What i want to know is how does one get it so when an object is rotated how can you calculate where the camera node should be keeping a set distance between the two eg the target and the camera.

I have some code for the camera class i will post but it is a bit patchy since it has been tinkered with a lot to get it to work witch i am missing something but are unsure of what it is.

I am pretty sure it is math related witch i unfortunately am not to great with ^_^.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Mogre;

namespace MOgre_Map_Editor.Map_Viewer
class ExtendedCamera
#region Protected Vars

protected Camera mCamera = null;

protected SceneNode TargetNode = null;
protected SceneNode CameraNode = null;

protected int MouseLastX = 0;
protected int MouseLastY = 0;

protected float Yaw = 0.0f;
protected float Pitch = 0.0f;

#region Public Constructor

public ExtendedCamera(SceneManager mSceneManager, string CameraName, System.Windows.Forms.Form FormHandle)

//Check to see if the Form is good
if (FormHandle == null) throw new Exception("Bad Form For Camera");

FormHandle.MouseMove += new System.Windows.Forms.MouseEventHandler(Camera_MouseMove);
FormHandle.MouseUp += new System.Windows.Forms.MouseEventHandler(Camera_MouseUp);

// Create the camera's node structure
this.CameraNode = mSceneManager.RootSceneNode.CreateChildSceneNode(CameraName);
this.TargetNode = mSceneManager.RootSceneNode.CreateChildSceneNode(CameraName += "_TARGET_");
this.TargetNode.Position = new Vector3(0, 0, 10);
this.TargetNode.ShowBoundingBox = true;

//Setup some other details to do with the Camera Node

this.CameraNode.Position = new Vector3(0, 0, 10);

//Setup our Camera itself
this.mCamera = mSceneManager.CreateCamera(CameraName += "_CAMERA_");
this.mCamera.SetPosition(0, 0, 10);
this.mCamera.AutoAspectRatio = true;
this.mCamera.NearClipDistance = 1;

//Attach the camera to the camera Node

public Camera GetCam()
return this.mCamera;

public void SetTargetNode(SceneNode TargetNode)
if (TargetNode != null)
this.TargetNode.Position = TargetNode.Position;


#region private Methods

void Camera_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
if (e.Button == System.Windows.Forms.MouseButtons.Right)
this.MouseLastX = 0;
this.MouseLastY = 0;

void Camera_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
//Check to see if the right button is being held down if so we know we are rotating the camera
if (e.Button == System.Windows.Forms.MouseButtons.Right)
if (this.MouseLastX == 0)
this.MouseLastX = e.X;

if (this.MouseLastY == 0)
this.MouseLastY = e.Y;

if (e.X != this.MouseLastX)
this.Yaw = ((float)e.X - (float)this.MouseLastX) / 10;

if (e.Y != this.MouseLastY)
this.Pitch = ((float)e.Y - (float)this.MouseLastY) / 10;

this.TargetNode.Yaw(new Radian(Yaw));
this.TargetNode.Pitch(new Radian(Pitch));

this.Yaw = 0;
this.Pitch = 0;

this.MouseLastX = e.X;
this.MouseLastY = e.Y;



Any help or even point me in the right direction is appreacated.
Regards XainFaith


08-06-2010 19:46:04

Well Ok i am still having problems but i think i may be heading in some kind of direction that might be correct.

First off what i am trying to do is take the target node and project a vector from it determine a point on that vector at a given distance from its origin.

this what i got so far an i am not even sure it is close.

this.TargetNode.Yaw(Yaw, Node.TransformSpace.TS_WORLD);
this.TargetNode.Pitch(Pitch, Node.TransformSpace.TS_WORLD);

this.TargetNode.Position.x + (Mogre.Math.Cos(this.TargetNode.Orientation.Yaw)),
this.TargetNode.Position.y + (Mogre.Math.Sin(this.TargetNode.Orientation.Pitch)),
this.TargetNode.Position.z + (Mogre.Math.Sin(this.TargetNode.Orientation.Roll))

this.CameraNode.LookAt(this.TargetNode.WorldPosition, Node.TransformSpace.TS_WORLD);

Problem with is this does not work and i am still back a square one but at least i have an approach i like.

So any recommendations on how i can do this. I know one thing i need to get is the direction the TargetNode is facing as that will be the same but inverse for the CameraNode.

Thanks for any and all help.
even if it just a slap in da face :p

Regards XainFaith


14-07-2010 20:26:41

In this example, cam is the camera object, cameraDirection is the projection vector, node is the node you want to follow, and distance is the distance from the node you want to be.

//Variable declaration
Camera cam;
Vector3 cameraDirection = new Vector3(1,0,0);
SceneNode node;
float distance = 2;

//In FrameStarted
cam.Direction = cameraDirection.Reflect(cameraDirection); //Rotates the camera correctly
cam.Position = node.Position + cameraDirection * distance; //Positions the camera correctly

//In your mouse movement event handler (e.state.Y.rel is the relative change in the mouse's position)
cameraDirection = new Quaternion(new Radian((e.state.Y.rel * -.002f)), cam.DerivedRight) * cameraDirection; //Moves the camera vertically
cameraDirection = new Quaternion(new Radian((e.state.X.rel * -.002f)), new Vector3(0,1,0)) * cameraDirection; //Moves the camera horizontally

This may not work correctly (I just kind of ripped it from my project and tweaked it a bit), but I hope you get the idea.

Sorry for the late reply, I didn't see your post until now. :oops:


16-07-2010 00:41:03

namespace MOgre_Map_Editor.Map_Viewer

Hey, this is slightly off topic but I noticed you are writing a map editor. How's that going for you?

I ask because I've been working on a map editor myself and I've recently decided to make it open source. It's still in the fairly early stages, but there's a fair bit of good working code all the same. Perhaps we can join forces or at least learn from each other.

Here's the link to the project, you might not be able to get it to compile without the dependencies, but I'm working on putting together a dependencies package at the moment.