Jump to content
  • Advertisement

Archived

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

DevLiquidKnight

Strange camera problem....

This topic is 5334 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I made it so the camera moves based on the location of the mouse per frame if the mouse has moved.. however I got it to do up down left right ect.. type of rotations with the camera so that the user can look around but, it seems like if i take my mouse and move it in a circluar motion for awhile the world tends to rotate? I dont think this should happen any info on how i can fix this or what is causing it?

Share this post


Link to post
Share on other sites
Advertisement
Before you proceed much farther, you may want to look into quaternions. Maybe your rotation problem is coming from gimble lock or something. The Cull SDK sample has a good quaternion-based camera, and the topic has been brought up a lot on this board, so I quick search would suffice

Share this post


Link to post
Share on other sites

// Camera.h

#include <windows.h>
#include <d3d9.h>
#include <d3dx9math.h>

#ifndef __CAMERA_H__
#define __CAMERA_H__

class CCamera
{
public:
CCamera(); // constructor

~CCamera(); // destructor


// create a camera, and associate it with the given device. The camera will

// update the view matrix of the given device until it is Destroy()'d

HRESULT Create(LPDIRECT3DDEVICE9 lpDevice);

// destroy the camera. Further actions won't affect the d3d device it was

// associated with.

void Destroy();

// should be called once a frame to update the view matrix of the device so that any

// movements which have occured this frame actually take effect.

// Will not update the device unless movement (translation or rotation)

// has occured since the last device update.

HRESULT UpdateDevice();
// set the camera's position by hand (won't affect its orientation)

void SetPosition(D3DXVECTOR3 *pvPos);
// move the camera forwards (the way its facing) fDist units.

void MoveForward(float fDist);

// move the camera forwards (the way its facing) fDist units.

void MoveBackwards(float fDist);

// move the camera fDist units to the right

void MoveRight(float fDist);
// move the camera fDist units to the right

void MoveLeft(float fDist);
// move the camera fDist units up

void MoveUp(float fDist);
// move the camera in some arbitrary direction - assumes *pvDir is of unit length

void MoveInDirection(float fDist, D3DXVECTOR3 *pvDir);

// rotate the camera fAngle radians about its right axis

void RotateDown(float fAngle);
// rotate the camera fAngle radians about its right axis

void RotateUp(float fAngle);

// rotate the camera about its up axis

void RotateRight(float fAngle);
// rotate the camera about its up axis

void RotateLeft(float fAngle);

// rotate the camera about its facing axis

void Roll(float fAngle);

private:
LPDIRECT3DDEVICE9 m_lpDevice;// the device the camera is associated with

bool m_bMovedSinceLastUpdate;// tracks whether the camera has moved since it was

// last updated

// our 4 vectors

D3DXVECTOR3 m_vPosition;
D3DXVECTOR3 m_vFacing;
D3DXVECTOR3 m_vUp;
D3DXVECTOR3 m_vRight;

// floats to keep track of how far we've rotated since the last update which

// can then be used by UpdateDevice() to recalculate the new orientation vectors

float m_fRotAboutUp;
float m_fRotAboutRight;
float m_fRotAboutFacing;

D3DXMATRIX m_matView;
};
#endif

// Camera.cpp

CCamera::CCamera()
{
// initialise members

m_lpDevice = NULL;
m_bMovedSinceLastUpdate = false;
m_vPosition = D3DXVECTOR3(0.0f,0.0f,0.0f); // start at the origin

m_vFacing = D3DXVECTOR3(0.0f,0.0f,1.0f); // facing down the positive Z axis

m_vRight = D3DXVECTOR3(1.0f,0.0f,0.0f); // with the positive X axis to the right

m_vUp = D3DXVECTOR3(0.0f,1.0f,0.0); // and the positive Y axis up

m_fRotAboutUp = m_fRotAboutRight = m_fRotAboutFacing = 0.0f;

D3DXMatrixIdentity(&m_matView); // initialise the view matrix to the identity

}

CCamera::~CCamera()
{
Destroy(); // make sure that Destroy() can be called more than once

}

HRESULT CCamera::Create(LPDIRECT3DDEVICE9 lpDevice)
{
// associate the camera with the device

m_lpDevice = lpDevice;

D3DXMatrixIdentity(&m_matView); // ensure that the device's view matrix is the identity

return m_lpDevice->SetTransform(D3DTS_VIEW,&m_matView);
}

void CCamera::Destroy()
{
if (m_lpDevice)
{
m_lpDevice = NULL;
}
}

void CCamera::SetPosition(D3DXVECTOR3 *pvPos)
{
m_vPosition = *pvPos;
m_bMovedSinceLastUpdate = true;
}

void CCamera::MoveForward(float fDist)
{
m_vPosition.x += fDist*m_vFacing.x;
m_vPosition.z += fDist*m_vFacing.z;
m_bMovedSinceLastUpdate = true;
}
void CCamera::MoveBackwards(float fDist)
{
m_vPosition -= fDist*m_vFacing;
m_bMovedSinceLastUpdate = true;
}

void CCamera::MoveRight(float fDist)
{
m_vPosition.x += fDist*m_vRight.x;
m_vPosition.z += fDist*m_vRight.z;
m_bMovedSinceLastUpdate = true;
}
void CCamera::MoveLeft(float fDist)
{
m_vPosition.x -= fDist*m_vRight.x;
m_vPosition.z -= fDist*m_vRight.z;
m_bMovedSinceLastUpdate = true;
}

void CCamera::MoveUp(float fDist)
{
m_vPosition += fDist*m_vUp;
m_bMovedSinceLastUpdate = true;
}

void CCamera::MoveInDirection(float fDist, D3DXVECTOR3 *pvDir)
{
m_vPosition += fDist*(*pvDir);
m_bMovedSinceLastUpdate = true;
}
void CCamera::RotateDown(float fAngle)
{
m_fRotAboutRight += fAngle;
m_bMovedSinceLastUpdate = true;
}
void CCamera::RotateUp(float fAngle)
{
m_fRotAboutRight -= fAngle;
m_bMovedSinceLastUpdate = true;
}

void CCamera::RotateRight(float fAngle)
{
m_fRotAboutUp += fAngle;
m_bMovedSinceLastUpdate = true;
}
void CCamera::RotateLeft(float fAngle)
{
m_fRotAboutUp -= fAngle;
m_bMovedSinceLastUpdate = true;
}

void CCamera::Roll(float fAngle)
{
m_fRotAboutFacing += fAngle;
m_bMovedSinceLastUpdate = true;
}

HRESULT CCamera::UpdateDevice()
{
HRESULT hr;
D3DXMATRIX matTotal; // used to speed up the process by performing less matrix

// multiplications.


// the matrices which represent the rotation about each of our 3 axes

D3DXMATRIX matRotAboutUp, matRotAboutRight, matRotAboutFacing;
POINT cursor_pos;
int screen_width=GetSystemMetrics(SM_CXSCREEN);
int screen_height=GetSystemMetrics(SM_CYSCREEN);
GetCursorPos(&cursor_pos);
SetCursorPos(screen_width/2,screen_height/2);
float verticleRot=(float)cursor_pos.y/10000;
float horizontalRot=(float)cursor_pos.x/10000;

if(cursor_pos.y != screen_height/2)
{
if(cursor_pos.y>screen_height/2)
{
RotateDown(0.03f);
}
if(cursor_pos.y<screen_height/2)
{
RotateUp(0.03f);
}
}
if(cursor_pos.x != screen_width/2)
{
if(cursor_pos.x<screen_width/2)
{
RotateLeft(0.03f);
}
if(cursor_pos.x>screen_width/2)
{
RotateRight(0.03f);
}
}
// bail if we haven't moved since the last update

if (!m_bMovedSinceLastUpdate)
{
return S_OK;
}

// get the rotation matrices for each rotation

D3DXMatrixRotationAxis(&matRotAboutRight,&m_vRight,m_fRotAboutRight);
D3DXMatrixRotationAxis(&matRotAboutUp,&m_vUp,m_fRotAboutUp);
D3DXMatrixRotationAxis(&matRotAboutFacing,&m_vFacing,m_fRotAboutFacing);

// concatenate them to form a total rotation matrix

D3DXMatrixMultiply(&matTotal,&matRotAboutUp,&matRotAboutRight);
D3DXMatrixMultiply(&matTotal,&matRotAboutFacing,&matTotal);

// transform 2 of the vectors by this matrix and get the third using the cross product

D3DXVec3TransformCoord(&m_vRight,&m_vRight,&matTotal);
D3DXVec3TransformCoord(&m_vUp,&m_vUp,&matTotal);
D3DXVec3Cross(&m_vFacing,&m_vRight,&m_vUp);

// are these two still perpandicular?

if (fabs(D3DXVec3Dot(&m_vUp,&m_vRight)) > 0.01)
{
// these suckers aren't perpandicular anymore - get another using the cross product

D3DXVec3Cross(&m_vUp,&m_vFacing,&m_vRight);
}

// remember to normalise vectors before putting them into the view matrix

D3DXVec3Normalize(&m_vRight,&m_vRight);
D3DXVec3Normalize(&m_vUp,&m_vUp);
D3DXVec3Normalize(&m_vFacing,&m_vFacing);

// used to compute the bottom row of the view matrix

float fView41,fView42,fView43;
fView41 = -D3DXVec3Dot(&m_vRight,&m_vPosition);
fView42 = -D3DXVec3Dot(&m_vUp,&m_vPosition);
fView43 = -D3DXVec3Dot(&m_vFacing,&m_vPosition);

m_matView = D3DXMATRIX( m_vRight.x, m_vUp.x, m_vFacing.x, 0.0f,
m_vRight.y, m_vUp.y, m_vFacing.y, 0.0f,
m_vRight.z, m_vUp.z, m_vFacing.z, 0.0f,
fView41, fView42, fView43, 1.0f
);

// update the device - if we fail then return the error code

hr = m_lpDevice->SetTransform(D3DTS_VIEW, &m_matView);
if (FAILED(hr))
{
return hr;
}

// reset the various variables to reflect the new update

m_fRotAboutUp = m_fRotAboutRight = m_fRotAboutFacing = 0.0f;
m_bMovedSinceLastUpdate = false;

return S_OK;
}


This is my camera object, if anyone sees what may be causing this.

[edited by - DevLiquidKnight on January 13, 2004 10:41:42 PM]

Share this post


Link to post
Share on other sites
Without looking at your code too closely, I'd guess that it is probably just cumulative error building up in your orientation vectors.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________


[edited by - Thunder_Hawk on January 13, 2004 11:16:19 PM]

Share this post


Link to post
Share on other sites
anyone have a quadrion camera that works very well? I tried looking in sdk but im lost I checked the resource/article section and found one once but i never got it to work so if anyone has any that do work please help

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!