XainFaith
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 ^_^.
Any help or even point me in the right direction is appreacated.
Regards XainFaith
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;
#endregion
#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.CameraNode.SetFixedYawAxis(true);
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
this.CameraNode.AttachObject(this.mCamera);
}
public Camera GetCam()
{
return this.mCamera;
}
public void SetTargetNode(SceneNode TargetNode)
{
if (TargetNode != null)
{
this.TargetNode.Position = TargetNode.Position;
}
}
#endregion
#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;
}
}
#endregion
}
}
Any help or even point me in the right direction is appreacated.
Regards XainFaith