# first person camera - why wont it work

## Recommended Posts

Hi all Im pretty new to games programming and really need some help with camera control. I'm writing a camera class header file which will let me make a first person camera. the way im trying to do this is: translate the camera to the origin: position -= position do the same for the look point find the angles between the look vector and the z axis roate the look point to the z axis translate / rotate as needed rotate look point back move camera and look point back the code only translates at the minuite in the 3 axis. the problem i seem to be having is that i can move forwards and backwards fine. when i try to mov left or right the look point seems o get locked into place after 1 move so i will them rotate around that point and not move it. there also seems to be the odd rounding errors and im not t sure on the maths but have asked around on what i have so far. code is below and any help would be greatly apreciated. :) Camera.h
class Camera
{
public:
Camera();
void rotateDown(float angle);							//rotates around right vector
void rotateRight(float angle);							//rotates around up vector
void rotateRoll(float angle);							//rotates around look vector
void setViewMatrix(LPDIRECT3DDEVICE9 g_pd3dDevice);		//sets all the coordinates into the view matrix
void moveCamera(char direction, float amount);
void setPosition(float x, float y, float z);
void setLook(float x, float y, float z);
void setUp(float x, float y, float z);
private:
void transformCamera();									//transforms camera
D3DXVECTOR3 position;									// camera position
float yaw;                 								// rotation around the y axis
float pitch;               								// rotation around the x axis
float roll;                								// rotation around the z axis
float forward;											//movement forward or backwards
float sideStep;											//movement left or right
float upMove;											//movement up or down
//camera vectors and vector points
D3DXVECTOR3 up,look,right; 								// camera axis
//used on the transformations and to create the vectors to pass to cam
bool changed;											//stores camera state
};


Camera.cpp

Camera::Camera()
{
yaw = pitch = roll = 0;										//sets angles to rotate by to 0
forward = sideStep = upMove = 0;							//sets movement variables to 0

position = D3DXVECTOR3(0.0f, 0.0f, -150.0f);
look = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);

changed = true;
}

void Camera::rotateDown(float angle)							//rotates around right vector
{
pitch = (angle / 180) * D3DX_PI;

changed = true;
transformCamera();
}
void Camera::rotateRight(float angle)							//rotates around up vector
{
yaw = (angle / 180) * D3DX_PI;

changed = true;
transformCamera();
}
void Camera::rotateRoll(float angle)							//rotates around look vector
{
roll = (angle / 180) * D3DX_PI;
changed = true;
transformCamera();
}

void Camera::moveCamera(char direction, float amount)
{
D3DXVECTOR3 temp;
//determines which way to move the camera
switch(direction)
{
case 'f':			//forward
forward = amount;
break;
case 'b':			//backwards
forward = amount;
break;
case 'l':			//left
sideStep = amount;
break;
case 'r':			//right
sideStep = amount;
break;
case 'u':			//up
upMove = amount;
break;
case 'd':			//down
upMove = amount;
break;
}
changed = true;

transformCamera();
}
//camera transform function
//function translates the up, look and right points to the origin
//by the cameras pos (pos.x += (-cam.pos.x), pos.y += (-cam.pos.y))

//find the vector of upP
void Camera::transformCamera()
{

D3DXVECTOR3 temp(position), lookTemp(0.0f, 0.0f, 0.0f);

//moves camera and look to the origin
position -= temp;
look -= temp;

//caculate the vector between the origin and the look point
lookTemp = look - lookTemp;
D3DXVec3Normalize(&lookTemp, &lookTemp);

D3DXVECTOR3 TEMP(lookTemp.x, lookTemp.y, 0.0f);								//holds z-y plane vector first
D3DXVECTOR3 ZTEMP(0.0f, 0.0f, 1.0f);										//holds the z-axis

//gets angles to rotate by
float yRotation = ((cos(D3DXVec3Dot(&lookTemp, &TEMP) / D3DXVec3Length(&lookTemp))) / 180) * D3DX_PI;	//rotation value around the y-axis
float xRotation = ((cos(D3DXVec3Dot(&TEMP, &ZTEMP) / D3DXVec3Length(&TEMP))) / 180) * D3DX_PI;			//rotation value around the x-axis

D3DXMATRIX rotateToZMat, yRotateMat, xRotateMat, tempMat;					//holds rotation to move look pos to z axis

D3DXMatrixRotationY(&yRotateMat, yRotation);								//sets the y rotation matrix
D3DXMatrixRotationX(&xRotateMat, xRotation);								//sets the x rotation matrix

D3DXMatrixMultiply(&rotateToZMat, &yRotateMat, &xRotateMat);				//multiplies both rotations

D3DXVec3TransformCoord(&look, &look, &rotateToZMat);						//rotates look to z axis

//moves points about the axis x, y and z
if(forward != 0)
{
position.z += forward;
look.z += forward;
}
if(sideStep != 0)
{
position.x += sideStep;
look.x += sideStep;
}
if(upMove != 0)
{
position.y += upMove;
look.y += upMove;
}

rotateToZMat *= -1;

D3DXVec3TransformCoord(&look, &look, &rotateToZMat);						//rotates look to z axis

//moves camera back to original position
position += temp;
look += temp;

//resets movement variables
forward = 0;
sideStep = 0;
upMove = 0;
}

void Camera::setPosition(float x, float y, float z)
{
position.x = x;
position.y = y;
position.z = z;
changed = true;
}

void Camera::setLook(float x, float y, float z)
{
look.x = x;
look.y = y;
look.z = z;
}

void Camera::setUp(float x, float y, float z)
{
up.x = x;
up.y = y;
up.z = z;
}

void Camera::setViewMatrix(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &position, &look, &up);
g_pd3dDevice -> SetTransform(D3DTS_VIEW, &matView);

// Set up the projection matrix.
// This transforms 2D geometry into a 3D space.
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 400.0f);
g_pd3dDevice -> SetTransform(D3DTS_PROJECTION, &matProj);
}



## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628278
• Total Posts
2981785

• 10
• 11
• 17
• 13
• 9