Sign in to follow this  
GreaseMonkey83

Camera help

Recommended Posts

GreaseMonkey83    100
Hey all I'm trying to write a first person camera to use in a game im writing. The problem i seem to be having with it is when i want to move left or right. moving left or right once seems to work, but after he lookAt point seems to lock and not move rotating the camera around that instead of moving the lookAt point also. I'm not the best at the maths part of this but i've also asked a few others for help on the maths and it seems to be right. At the minuite, outside of the class i call camera.moveCamera('l', 5.0f) to move left. my Camera header file code is: 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::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 up vector 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); } Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Buttacup    100
sorry :( had to

guys original.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
};



original.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 up vector
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);
}

Share this post


Link to post
Share on other sites
Shadowflare    132
In your moveCamera() function, the reason it only works once is that you're assigning the amount you want to move to your forward/backward/left/right, rather than incrementing/decrementing it. I think what you meant to do was something like this:


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();
}




You might consider using a D3DXVECTOR3 to store your information about movement and rotation and such, as it will probably save you a lot of trouble later on. For instance, doing so would cause your moveCamera() function to become much simpler:


// fX == forward/backward
// fY == up/down
// fZ == left/right
void Camera::moveCamera(float fX, float fY, float fZ)
{
D3DXVECTOR3 moveVector = D3DXVECTOR3(fX, fY, fZ);

this->position += moveVector;

transformCamera();
}

// Then instead of moveCamera('f', 1.0f) you would do this
moveCamera(1.0f, 0.0f, 0.0f);

Share this post


Link to post
Share on other sites
GreaseMonkey83    100
Hey thanks for that

i figured out an alternative solution, i removed the calculations to move the look point to the z axis and those rotations. i just move them to the origin and rotate now, and translate on the vector: position - look

works fine now :)

Share this post


Link to post
Share on other sites
Shadowflare    132
That's also a good way to it. Just remember that by doing that, you're moving the world around the camera instead of moving the camera through the world. Be sure to keep that in mind if future problems crop up.

Share this post


Link to post
Share on other sites
GreaseMonkey83    100
Quote:
Original post by Shadowflare
That's also a good way to it. Just remember that by doing that, you're moving the world around the camera instead of moving the camera through the world. Be sure to keep that in mind if future problems crop up.


Is that right? im only moving the cameras position so im not realy moving the world at all am i? o.O

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this