FPS camera.

Started by
1 comment, last by Evil Steve 17 years ago
hey. i got this issue that i dont understand, and its the camera lookat. i want to use my mouse to look around and move whit W,A,S,D but i cant get it together. plz help me.
Advertisement
The camera lookat vector is the vector that specifies the point in the world where you want to aim the camera.

Here's the code for the second one:

D3DCamera.h:

#ifndef D3DCAMERA_H
#define D3DCAMERA_H

// CD3DCamera class
class CD3DCamera {
public:
CD3DCamera(); // Constructor

void resetCamera(); // Reset the camera
void rotateCamera(); // Rotate the camera
void walk(float fUnits); // Camera fo walking
void strafe(float fUnits); // Camera for strafing
void fly(float fUnits); // Camera for flying
void yaw(float fAngle); // Camera for yawing
void roll(float fAngle); // Camera for rolling
void pitch(float fAngle); // Camera for pitching
void zoom(float fUnits); // For zooming and out
D3DXMATRIX *getViewMatrix(D3DXMATRIX *pmatView); // Get view transformation matrix

// ECameraType enumeration
enum ECameraType {
Land, // Camera is on the land
Air, // Camera is on the air
};

private:
POINT m_point; // Screen coordinates

D3DXMATRIXA16 m_matView; // View transformation matrix

float m_fX, m_fY, m_fZ; // View positions

D3DXVECTOR3 m_vecPosition, m_vecUp, m_vecRight, m_vecLookAt; // 3D vectors

BOOL m_bCameraPosChanged; // This will notify us if the camera's position has changed since last update

ECameraType m_cameraType; // This will hold camera type
};

extern CD3DCamera *g_pD3DCamera; // Externed class pointer

#endif // D3DCAMERA_H

D3DCamera.cpp:

//------------------------------------------------------------------
// Name: CD3DCamera
// Desc: CD3DCamera constructor
//------------------------------------------------------------------
CD3DCamera::CD3DCamera()
{
m_vecPosition = D3DXVECTOR3(0.0f, 0.0f, -5.0f); // Position vector
m_vecLookAt = D3DXVECTOR3(0.0f, 0.0f, 1.0f); // Look vector
m_vecUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f); // Up vector
m_vecRight = D3DXVECTOR3(1.0f, 0.0f, 0.0f); // Right vector

m_cameraType = Land; // Set camera type to land
}

//------------------------------------------------------------------
// Name: resetCamera
// Desc: Reset the camera
//------------------------------------------------------------------
void CD3DCamera::resetCamera()
{
m_vecPosition = D3DXVECTOR3(0.0f, 0.0f, -250.0f);
m_vecLookAt = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
m_vecUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
::D3DXMatrixLookAtLH(&m_matView, &m_vecPosition, &m_vecLookAt, &m_vecUp);
g_pD3DGraphics->setViewTransform(&m_matView);
}

//------------------------------------------------------------------
// Name: rotateCamera
// Desc: Rotate the camera to an arbitrary axis
//------------------------------------------------------------------
void CD3DCamera::rotateCamera()
{
}

//------------------------------------------------------------------
// Name: walk
// Desc: Camera for walking
//------------------------------------------------------------------
void CD3DCamera::walk(float fUnits)
{
// Walk only on the XZ plane when the camera is on the land
if(m_cameraType == Land)
m_vecPosition += ::D3DXVECTOR3(m_vecLookAt.x, 0.0f, m_vecLookAt.z) * fUnits;
else
m_vecPosition += m_vecLookAt * fUnits;
}

//------------------------------------------------------------------
// Name: strafe
// Desc: Camera for strafing
//------------------------------------------------------------------
void CD3DCamera::strafe(float fUnits)
{
// Move only on the XZ plane when the camera is on the land
if(m_cameraType == Land)
m_vecPosition += ::D3DXVECTOR3(m_vecRight.x, 0.0f, m_vecRight.z) * fUnits;
else
m_vecPosition += m_vecRight * fUnits;
}

//------------------------------------------------------------------
// Name: fly
// Desc: Camera for flying
//------------------------------------------------------------------
void CD3DCamera::fly(float fUnits)
{
// Move only on y-axis when the camera is on the land
if(m_cameraType == Land)
m_vecPosition.y += fUnits;
else
m_vecPosition += m_vecUp * fUnits;
}

//------------------------------------------------------------------
// Name: yaw
// Desc: Camera for yawing
//------------------------------------------------------------------
void CD3DCamera::yaw(float fAngle)
{
// If the camera is on land
if(m_cameraType == Land)
::D3DXMatrixRotationY(&m_matView, fAngle); // Rotate around the y-axis

// The camera is on th air
else
::D3DXMatrixRotationAxis(&m_matView, &m_vecUp, fAngle); // Rotate the up vector

// Rotate right and look vectors around y-axis
::D3DXVec3TransformCoord(&m_vecRight, &m_vecRight, &m_matView);
::D3DXVec3TransformCoord(&m_vecLookAt, &m_vecLookAt, &m_matView);
}

//------------------------------------------------------------------
// Name: roll
// Desc: Camera for rolling
//------------------------------------------------------------------
void CD3DCamera::roll(float fAngle)
{
// Check to see if the camera is on the air
if(m_cameraType == Air) {

// Rotate the look-at vector by the specified amount
::D3DXMatrixRotationAxis(&m_matView, &m_vecLookAt, fAngle);

// Rotate up and right vectors around look vector
::D3DXVec3TransformCoord(&m_vecUp, &m_vecUp, &m_matView);
::D3DXVec3TransformCoord(&m_vecRight, &m_vecRight, &m_matView);
}
}

//------------------------------------------------------------------
// Name: pitch
// Desc: Camera for pitching
//------------------------------------------------------------------
void CD3DCamera::pitch(float fAngle)
{
// Rotate around the x-axis
::D3DXMatrixRotationAxis(&m_matView, &m_vecRight, fAngle);

// Rotate up and look vectors around the right vector
::D3DXVec3TransformCoord(&m_vecUp, &m_vecUp, &m_matView);
::D3DXVec3TransformCoord(&m_vecLookAt, &m_vecLookAt, &m_matView);
}

//------------------------------------------------------------------
// Name: zoom
// Desc: For zooming in and out
//------------------------------------------------------------------
void CD3DCamera::zoom(float fUnits)
{
m_vecPosition.z = fUnits;
}

//------------------------------------------------------------------
// Name: getViewMatrix
// Desc: Get the view transformation matrix
//------------------------------------------------------------------
D3DXMATRIX *CD3DCamera::getViewMatrix(D3DXMATRIX *pmatView)
{
// Normalize the look-at vector
::D3DXVec3Normalize(&m_vecLookAt, &m_vecLookAt);

// Keep up vector orthogonal to the look and right vectors
::D3DXVec3Cross(&m_vecUp, &m_vecLookAt, &m_vecRight);

// Normalize the up vector
::D3DXVec3Normalize(&m_vecUp, &m_vecUp);

// Keep right vector orthogonal to right and up vectors
::D3DXVec3Cross(&m_vecRight, &m_vecUp, &m_vecLookAt);

// Normalize the right vector
::D3DXVec3Normalize(&m_vecRight, &m_vecRight);

// Build the view matrix
m_fX = -::D3DXVec3Dot(&m_vecRight, &m_vecPosition); // X-coordinate
m_fY = -::D3DXVec3Dot(&m_vecUp, &m_vecPosition); // Y-coordinate
m_fZ = -::D3DXVec3Dot(&m_vecLookAt, &m_vecPosition); // Z-coordinate

// Modify the values of view transformation matrix
(*pmatView)(0, 0) = m_vecRight.x; (*pmatView)(0, 1) = m_vecUp.x; (*pmatView)(0, 2) = m_vecLookAt.x; (*pmatView)(0, 3) = 0.0f;
(*pmatView)(1, 0) = m_vecRight.y; (*pmatView)(1, 1) = m_vecUp.y; (*pmatView)(1, 2) = m_vecLookAt.y; (*pmatView)(1, 3) = 0.0f;
(*pmatView)(2, 0) = m_vecRight.z; (*pmatView)(2, 1) = m_vecUp.z; (*pmatView)(2, 2) = m_vecLookAt.z; (*pmatView)(2, 3) = 0.0f;
(*pmatView)(3, 0) = m_fX; (*pmatView)(3, 1) = m_fY; (*pmatView)(3, 2) = m_fZ; (*pmatView)(3, 3) = 1.0f;

return pmatView;
}

I would recommend this book: "Introduction to 3D game programming with DirectX 9.0c a shader approach" by Frank Luna. That's the best book out there!! Here you can download that's book's source code: http://www.wordware.com/files/dx9c/ Good luck
Define "look around and move". What should each key do? You can't get full lookat and movement with just 4 keys (in a easy way). What do you have at the moment? In what way does it not work?

This topic is closed to new replies.

Advertisement