Archived

This topic is now archived and is closed to further replies.

ZitherMan

Camera references

Recommended Posts

I''m wondering if there''s any referances how to move the camera. I find the ones in the SDK documentation difficult to follow so i was wondering if there''s a site that maybe has tutorials making a cameraclass? Something like www.gametutorials.com does in OGL.

Share this post


Link to post
Share on other sites
I have a nice camera class that I''m using for my current project.
I have a base camera interface like so:


  
enum CameraType
{
FPS_CAMERA,
FOLLOW_CAMERA,
ARCBALL_CAMERA,
UNKNOWN_CAMERA
};

class CCamera
{
public:
CCamera() { m_CameraType = UNKNOWN_CAMERA; }
virtual ~CCamera() {}

D3DXMATRIX SetViewMatrix(D3DXMATRIX &mat) { m_matView = mat; }
D3DXMATRIX* GetViewMatrix() { return &m_matView; }

D3DXMATRIX SetBillboardMatrix(D3DXMATRIX &mat) { m_matBillboard = mat; }
D3DXMATRIX* GetBillboardMatrix() { return &m_matBillboard; }

void SetPosition(float x, float y, float z) { m_vPosition = D3DXVECTOR3(x,y,z); }
D3DXVECTOR3 GetPosition() { return m_vPosition; }

CameraType GetCameraType() { return m_CameraType; }

virtual void Update(float fElapsedTime) = 0;
virtual void Reset() = 0;

protected:
D3DXMATRIX m_matView;
D3DXMATRIX m_matBillboard;
D3DXMATRIX m_matOrientation;
D3DXVECTOR3 m_vPosition;

CameraType m_CameraType;
};


And for now all I have for a concrete camera class is a user controlled camera:


  
class CUserControlledCamera : public CCamera
{
protected:
D3DXVECTOR3 m_vVelocity;

float m_fYaw;
float m_fYawVelocity;
float m_fPitch;
float m_fPitchVelocity;
float m_fRoll;
float m_fRollVelocity;

float m_bInvertYAxis;

float m_fSpeed;
float m_fAngularSpeed;

float m_fDeceleration;
float m_fAngularDeceleration;

public:
CUserControlledCamera();
virtual ~CUserControlledCamera();

void SetInvertYAxis(bool inv) { m_bInvertYAxis = inv; }
void ToggleInvertYAxis() { m_bInvertYAxis = !m_bInvertYAxis; }
void AddToYawPitchRoll(float fYaw, float fPitch, float fRoll);
void AddToVelocity(float x, float y, float z);
void Update(float fElapsedTime);
void Reset();
};


Implementation:

  
#include "CUserControlledCamera.h"
CUserControlledCamera::CUserControlledCamera()
{
m_CameraType = FPS_CAMERA;
Reset();
}

CUserControlledCamera::~CUserControlledCamera()
{
}

void CUserControlledCamera::Reset()
{
m_vVelocity.x = 0.0f;
m_vVelocity.y = 0.0f;
m_vVelocity.z = 0.0f;

m_fYaw = 0.0f;
m_fPitch = 0.0f;
m_fRoll = 0.0f;

m_fYawVelocity = 0.0f;
m_fPitchVelocity = 0.0f;
m_fRollVelocity = 0.0f;

// m_fSpeed = 3.0f;

// m_fAngularSpeed = 1.0f;

m_fSpeed = 1.0f;

// m_fDeceleration = 0.97f;

// m_fAngularDeceleration = 0.75f;

m_fDeceleration = 0.9f;
m_fAngularDeceleration = 0.8f;

m_bInvertYAxis = false;

m_vPosition = D3DXVECTOR3(0.0f,0.0f,0.0f);
D3DXMatrixIdentity(&m_matOrientation);
D3DXMatrixIdentity(&m_matBillboard);
}

void CUserControlledCamera::Update(float fElapsedTime)
{
float fTime;

if( fElapsedTime > 0.0f ) fTime = fElapsedTime;
else fTime = 0.05f;

float fSpeed = m_fSpeed * fTime;
float fAngularSpeed = m_fSpeed * 0.3f * fTime;

// Update the position vector

D3DXVECTOR3 vT = m_vVelocity * fSpeed;
D3DXVec3TransformNormal( &vT, &vT, &m_matOrientation );
m_vPosition += vT;

// keep the position locked in the vertical

// D3DXVECTOR3 pos = GetPosition();

// pos.y = 5.00f;

// SetPosition(pos.x,pos.y,pos.z);


// Update the yaw-pitch-rotation vector

m_fYaw += fAngularSpeed * m_fYawVelocity;
if(m_bInvertYAxis)
m_fPitch -= fAngularSpeed * m_fPitchVelocity;
else
m_fPitch += fAngularSpeed * m_fPitchVelocity;

// Set the view matrix

D3DXQUATERNION qR;
D3DXQuaternionRotationYawPitchRoll(&qR, m_fYaw, m_fPitch, m_fRoll);
D3DXMatrixAffineTransformation(&m_matOrientation, 1.25f, NULL, &qR, &m_vPosition );
D3DXMatrixInverse( &m_matView, NULL, &m_matOrientation );

// Decelerate the camera''s position velocity (for smooth motion)

m_vVelocity *= m_fDeceleration;

// decelerate the abgular velocity (mouse look)

// if you raise this value too much you''ll barf

m_fYawVelocity *= m_fAngularDeceleration;
m_fPitchVelocity *= m_fAngularDeceleration;

// update the billboard matrix

D3DXMatrixInverse( &m_matBillboard, NULL, &m_matView );
m_matBillboard._41 = 0.0f;
m_matBillboard._42 = 0.0f;
m_matBillboard._43 = 0.0f;
}

void CUserControlledCamera::AddToYawPitchRoll(float fYaw, float fPitch, float fRoll)
{
m_fPitchVelocity += fPitch;
m_fYawVelocity += fYaw;
m_fRollVelocity += fRoll;
}

void CUserControlledCamera::AddToVelocity(float x, float y, float z)
{
m_vVelocity += D3DXVECTOR3(x,y,z);
}


An now in order for me to control the camera with mouse and keyboard, I use directInput to poll the mouse/keyboard in immediate mode and use the following code to have a mouse-look camera with arrow keys for movement - like a Quake-type camera movement:


  
//---------------------------------

// MOUSE

// mouse look with camera

mx = mouse->getMouseX();
my = mouse->getMouseY();
camera->AddToYawPitchRoll(mx/0.8f, my/0.8f, 0.0f);

//---------------------------------

// KEYBOARD

// keyboard arrow keys to translate camera

if(keyboard->KeyIsDown(DIK_LSHIFT)|| keyboard->KeyIsDown(DIK_RSHIFT)) cameraSpeed *= 2;
if(keyboard->KeyIsDown(DIK_LCONTROL) || keyboard->KeyIsDown(DIK_RCONTROL)) cameraSpeed *= 10;

if(keyboard->KeyIsDown(DIK_LEFTARROW)) camera->AddToVelocity(-cameraSpeed,0,0);
if(keyboard->KeyIsDown(DIK_RIGHTARROW)) camera->AddToVelocity(cameraSpeed,0,0);
if(keyboard->KeyIsDown(DIK_UPARROW)) camera->AddToVelocity(0,0,cameraSpeed);
if(keyboard->KeyIsDown(DIK_DOWNARROW)) camera->AddToVelocity(0,0,-cameraSpeed);
if(keyboard->KeyIsDown(DIK_HOME)) camera->AddToVelocity(0,cameraSpeed,0);
if(keyboard->KeyIsDown(DIK_END)) camera->AddToVelocity(0,-cameraSpeed,0);


Note really a tutorial, but maybe you can use this same camera in your code or you will find it useful to get started with your own camera.

Share this post


Link to post
Share on other sites