• Advertisement

Archived

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

doom style camera movement

This topic is 5513 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

im trying to make an .x file model viewer(yeah yeah get your laughs out now... ill convert it to something more useful later)... how do i manipulate the camera matrix so that i can get "doom style" camera movement? some example code would be nice also if at all possible.

Share this post


Link to post
Share on other sites
Advertisement
You will be changing the view matrix based on your cameras location and direction, so just keep track of the cameras position with a Vector3 and the cameras direction with a Vector3 and then rotate, then translate the identity matrix by the opposite of these vectores each frame (to get the view matrix)

as for making it move around like doom, then it''s just a matter of moving the camera class around. you should be able to figure that out with some trig.

Share this post


Link to post
Share on other sites
ugh i kinda get it. :/
ok so uh... i make a class that stores the x,y,z of the camera and also the x,y,z of where the camera is looking at (relative to the position of the camera)... then i add the direction to the the position of the camera and use the original position and the new vector in D3D''s LookAt function??

and the formula for moving forward would be: position = position + amount_scalar*direction? (direction being a unit vector)

someone please tell me whether or not i am right and what optimizations i could/should make.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ya I''m very interested in a tutorial / code for a Quake III style camera.

my current camera isn''t very good.

say I rotate left and then I press forward it still moves forward on the z axis and not in the direction im facing.

a tutorial or code for a Quake III style camera would be cool.

maybe someone has a 3rd/1st person camera class they could post

Share this post


Link to post
Share on other sites



Well, this is my camera class...

u can use it, notice that the difference between an abstract object in 3d space and a camera is u`ll only need to inverse ur orientation and translation vectors...

it may be not optimal, but it suits my needs.

struct sObject3d
{

public:

// Position de l''objet
D3DXVECTOR3 vctPosition;

// Orientation de l''objet
D3DXVECTOR3 vctOrientation;

// Scaling de l''objet
D3DXVECTOR3 vctScaling;

// ID de l''objet
char strObjectID[30];

};

class CObject3d : public CXeonObject
{

protected:

// Informations de l''objet3d
sObject3d m_Object3d;

//Matrice de transformations
D3DXMATRIX m_matTransformations;
D3DXMATRIX m_matTranslations;
D3DXMATRIX m_matRotations;
D3DXMATRIX m_matScaling;

//Flags pour optimiser l''update
bool m_bRotated;
bool m_bMoved;
bool m_bScaled;

public:

// Constructeur
CObject3d();

// Destructeur
virtual ~CObject3d();

// Deplacement absolu de l''objet
void MoveAbs(float fPosX,
float fPosY,
float fPosZ);

// Deplacement relatif de l''objet
void MoveRel(float fDeltaX,
float fDeltaY,
float fDeltaZ);

// Rotation absolue de l''objet.
void RotateAbs(float fAngleX,
float fAngleY,
float fAngleZ);

// Rotation relative de l''objet
void RotateRel(float fDeltaX,
float fDeltaY,
float fDeltaZ);

// Retourne un pointeur vers la matrice de transformations
D3DXMATRIX * GetMatrix();

// Update de l''objet3d, construction de la matrice de transformations finale, et retour du pointeur vers cette nouvelle matrice.
virtual D3DXMATRIX * Update(float fDeltaTime = 0.0f);

// Implementation de la fonction virtuelle de chargement.
virtual bool Load(FILE * pFile);

// Implementation de la fonction virtuelle de destruction
virtual bool Destroy();

// Implementation de la fonction virtuelle d''enregistrement
virtual bool Save(FILE * pFile);

};// END CLASS DEFINITION CObject3d

// Static Model

#include "CObject3d.h"

// Constructeur
CObject3d::CObject3d()
{
ZeroMemory(&m_Object3d, sizeof(sObject3d));
m_Object3d.vctScaling = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
m_bRotated = false;
m_bMoved = false;
m_bScaled = false;
}

// Destructeur
CObject3d::~CObject3d()
{

}

// Deplacement absolu de l''objet
void CObject3d::MoveAbs(float fPosX,
float fPosY,
float fPosZ)
{
m_Object3d.vctPosition = D3DXVECTOR3(fPosX, fPosY, fPosZ);
}

// Deplacement relatif de l''objet
void CObject3d::MoveRel(float fDeltaX,
float fDeltaY,
float fDeltaZ)
{
m_Object3d.vctPosition += D3DXVECTOR3(fDeltaX, fDeltaY, fDeltaZ);
}

// Rotation absolue de l''objet.
void CObject3d::RotateAbs(float fAngleX,
float fAngleY,
float fAngleZ)
{
m_Object3d.vctOrientation = D3DXVECTOR3(fAngleX, fAngleY, fAngleZ);
}

// Rotation relative de l''objet
void CObject3d::RotateRel(float fDeltaX,
float fDeltaY,
float fDeltaZ)
{
m_Object3d.vctOrientation += D3DXVECTOR3(fDeltaX, fDeltaY, fDeltaZ);
}

// Retourne un pointeur vers la matrice de transformations
D3DXMATRIX * CObject3d::GetMatrix()
{
return &m_matTransformations;
}

// Update de l''objet3d, construction de la matrice de transformations finale, et retour du pointeur vers cette nouvelle matrice.
D3DXMATRIX * CObject3d::Update(float fDeltaTime/* = 0.0f*/)
{
//Si il n''y a pas eue de transformations, alors on update rien
if( ! m_bMoved && !m_bRotated && ! m_bScaled )
return &m_matTransformations;

//Reconstruction de la matrice de translation
if ( m_bMoved )
{
D3DXMatrixIdentity(&m_matTranslations);
D3DXMatrixTranslation(&m_matTranslations,
m_Object3d.vctPosition.x,
m_Object3d.vctPosition.y,
m_Object3d.vctPosition.z);
}

//Reconstruction de la matrice de rotations
if ( m_bRotated )
{
D3DXMatrixIdentity(&m_matRotations);
D3DXMatrixRotationX(&m_matRotations, m_Object3d.vctOrientation.x);
D3DXMatrixRotationX(&m_matRotations, m_Object3d.vctOrientation.y);
D3DXMatrixRotationX(&m_matRotations, m_Object3d.vctOrientation.z);
}

//Reconstruction de la matrice de scaling
if ( m_bScaled )
{
D3DXMatrixIdentity(&m_matScaling);
D3DXMatrixScaling(&m_matScaling, m_Object3d.vctScaling.x,
m_Object3d.vctScaling.y,
m_Object3d.vctScaling.z);
}


//Creation de la matrice finale
D3DXMatrixIdentity(&m_matTransformations);
D3DXMatrixMultiply(&m_matTransformations, &m_matTransformations, &m_matScaling);
D3DXMatrixMultiply(&m_matTransformations, &m_matTranslations, &m_matRotations);

//Remise a 0 des variables test
m_bRotated = m_bScaled = m_bMoved = false;

return &m_matTransformations;
}

// Implementation de la fonction virtuelle de chargement.
bool CObject3d::Load(FILE * pFile)
{
if ( ! pFile )
return false;

//Lecture de la strucutre
fread(&m_Object3d, sizeof(sObject3d), 1, pFile);
return true;
}

// Implementation de la fonction virtuelle de destruction
bool CObject3d::Destroy()
{
return true;
}

// Implementation de la fonction virtuelle d''enregistrement
bool CObject3d::Save(FILE * pFile)
{
if ( !pFile )
return false;

fwrite(&m_Object3d, sizeof(sObject3d), 1, pFile);
return true;
}
class CCamera : public CObject3d
{

public:

// Constructeur
CCamera();

// Destructeur
~CCamera();

// Update de la matrice de transformations.

virtual D3DXMATRIX * Update(float fDeltaTime = 0.0f);

// Implementation de la fonction virtuelle pure de chargement
virtual bool Load(FILE * pFile);

// Implementation de la fonction virtuelle pure d''enregistrement
virtual bool Save(FILE * pFile);

// Implementation de la fonction virtuelle pure de destruction
virtual bool Destroy();

};// END CLASS DEFINITION CCamera



// Constructeur
CCamera::CCamera()
{

}

// Destructeur
CCamera::~CCamera()
{

}

// Update de la matrice de transformations.

D3DXMATRIX * CCamera::Update(float fDeltaTime/* = 0.0f*/)
{
//Si il n''y a pas eue de transformations, alors on update rien
if( ! m_bMoved && !m_bRotated && ! m_bScaled )
return &m_matTransformations;

//Reconstruction de la matrice de translation
if ( m_bMoved )
{
D3DXMatrixIdentity(&m_matTranslations);
D3DXMatrixTranslation(&m_matTranslations,
-m_Object3d.vctPosition.x,
-m_Object3d.vctPosition.y,
-m_Object3d.vctPosition.z);
}

//Reconstruction de la matrice de rotations
if ( m_bRotated )
{
D3DXMatrixIdentity(&m_matRotations);
D3DXMatrixRotationX(&m_matRotations, -m_Object3d.vctOrientation.x);
D3DXMatrixRotationX(&m_matRotations, -m_Object3d.vctOrientation.y);
D3DXMatrixRotationX(&m_matRotations, -m_Object3d.vctOrientation.z);
}

//Creation de la matrice finale
D3DXMatrixIdentity(&m_matTransformations);
D3DXMatrixMultiply(&m_matTransformations, &m_matTranslations, &m_matRotations);

//Remise a 0 des variables test
m_bRotated = m_bScaled = m_bMoved = false;
return &m_matTransformations;
}

// Implementation de la fonction virtuelle pure de chargement
bool CCamera::Load(FILE * pFile)
{
if ( !pFile)
return false;

fread(&m_Object3d, sizeof(sObject3d), 1, pFile);
return true;
}

// Implementation de la fonction virtuelle pure d''enregistrement
bool CCamera::Save(FILE * pFile)
{
if ( !pFile )
return false;

fwrite(&m_Object3d, sizeof(sObject3d), 1, pFile);
return true;
}

// Implementation de la fonction virtuelle pure de destruction
bool CCamera::Destroy()
{
return true;
}

Share this post


Link to post
Share on other sites
(this is working in 2d, easy to convert though) Using the left and right to rotate left and right, you need to increase the x and y by the appropriate vector. This can be obtained by using "increase X by (the sine of angle times the total distance you want to move)" for Y you would use cosine. (this is assuming that 0 degrees is up and the angle increases clockwise). What the sine and cosine do is break up the polar vector that you want to move into it''s individual scalar components so they can easily be added to the x and y position.

x+=sin(angle)*distance;
y+=cos(angle)*distance;

Share this post


Link to post
Share on other sites
yeah, you got the basic idea for the camera class, but I think it''s best to keep the orientation as the angle around each axis. that way when you move, you just throw the approiate angle into the appropriate trig function and multiply that by the velocity. (you should also limit the cameras X direction, the up and down, so he can''t look more than strait up, or strait down)

Quake III camera is exactly like every other FPS camera, so this should do it for ya.

Share this post


Link to post
Share on other sites

  • Advertisement