Sign in to follow this  
Chett2001

FPS Mouse Implementation

Recommended Posts

Hey all, ive been designing a simple FPS to help brush up on my DirectX skills, im still very much a novice trying to learn the ropes so this might seem pretty simple but bare with me. Currently my mouse works like this, i have a macro shown below that picks up the mouse co-ordinates:
#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
#endif
#ifndef GET_Y_LPARAM
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
#endif
This is picked up in the Windows Process function and stored into two variables under the WM_MOUSEMOVE case:
PosAbsolute = GET_X_LPARAM(lParam); 
yPosAbsolute = GET_Y_LPARAM(lParam);
Like in an FPS i want to make it so i can continually turn the view without having to drag the mouse back to the center of the screen to be able to do so, for this i found the SetCursorPos. I read up on this sort of thing and identified that one of the best ways to do this might be by rotating the view matrix around the Y axis and the X axis, to give the feel of looking around. The problem is i dont know how to implement this: firstly, how do i convert the input from the mouse to an angle of rotation and implement it to turn around the X and Y axis using the code i have (or a better way of doing it if there is one). Secondly, how do i effectively implement the SetCursorPos method using this example (or another), because im using co-ordinates not a value and a direction sort of thing. Thanks for the help as allways, Chett

Share this post


Link to post
Share on other sites
It's not the easiest code to read, but DXUT provides a first-person camera for you. Search for CFirstPersonCamera in DXUTcamera.h - even if you're not using DXUT you should be able to get some answers from their code as it's all open-source.

hth
Jack

Share this post


Link to post
Share on other sites
After looking at the code i was able to implement something allmost there using D3DXMatrixRotationYawPitchRoll to look around an angle taken from the screen co-ordinates.

But this doesnt really fit in with the rest of my FPS (WSAD) controls which uses D3DXMatrixLookAtLH(&V, &eye, &target, &up). The two are acting like completely different entities, so instead im going to try and rotate the camera 'target' point around the 'eye', so its going to involve Pi and a radius and i dont know where to begin, anyone got any ideas?


Share this post


Link to post
Share on other sites
Hi there, here is a camera class you can look at

class Camera2
{
public:
enum CameraType { LANDOBJECT, AIRCRAFT };

Camera2();
Camera2(CameraType cameraType);
~Camera2();

void strafe(float units); // left/right
void fly(float units); // up/down
void walk(float units); // forward/backward

void pitch(float angle); // rotate on right vector
void yaw(float angle); // rotate on up vector
void roll(float angle); // rotate on look vector

void getViewMatrix(D3DXMATRIX* V);
void setCameraType(CameraType cameraType);
void getPosition(D3DXVECTOR3* pos);
void setPosition(D3DXVECTOR3* pos);

void getRight(D3DXVECTOR3* right);
void getUp(D3DXVECTOR3* up);
void setLook(D3DXVECTOR3* look);
void getLook(D3DXVECTOR3* look);
private:
CameraType _cameraType;
D3DXVECTOR3 _right;
D3DXVECTOR3 _up;
D3DXVECTOR3 _look;
D3DXVECTOR3 _pos;
};//end class Camera2
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////






CPP files

Camera2::Camera2()
{
_cameraType = AIRCRAFT;

_pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
_up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
_look = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
}

Camera2::Camera2(CameraType cameraType)
{
_cameraType = cameraType;

_pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
_up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
_look = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
}

Camera2::~Camera2()
{

}

void Camera2::getPosition(D3DXVECTOR3* pos)
{
*pos = _pos;
}

void Camera2::setPosition(D3DXVECTOR3* pos)
{
_pos = *pos;
}

void Camera2::getRight(D3DXVECTOR3* right)
{
*right = _right;
}

void Camera2::getUp(D3DXVECTOR3* up)
{
*up = _up;
}



void Camera2::setLook(D3DXVECTOR3* look)
{
_look = *look;
}

void Camera2::getLook(D3DXVECTOR3* look)
{
*look = _look;
}

void Camera2::walk(float units)
{
// move only on xz plane for land object
if( _cameraType == LANDOBJECT )
_pos += D3DXVECTOR3(_look.x, 0.0f, _look.z) * units;

if( _cameraType == AIRCRAFT )
_pos += _look * units;
}

void Camera2::strafe(float units)
{
// move only on xz plane for land object
if( _cameraType == LANDOBJECT )
_pos += D3DXVECTOR3(_right.x, 0.0f, _right.z) * units;

if( _cameraType == AIRCRAFT )
_pos += _right * units;
}

void Camera2::fly(float units)
{
// move only on y-axis for land object
if( _cameraType == LANDOBJECT )
_pos.y += units;

if( _cameraType == AIRCRAFT )
_pos += _up * units;
}

void Camera2::pitch(float angle)
{
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T, &_right, angle);

// rotate _up and _look around _right vector
D3DXVec3TransformCoord(&_up,&_up, &T);
D3DXVec3TransformCoord(&_look,&_look, &T);
}

void Camera2::yaw(float angle)
{
D3DXMATRIX T;

// rotate around world y (0, 1, 0) always for land object
if( _cameraType == LANDOBJECT )
D3DXMatrixRotationY(&T, angle);

// rotate around own up vector for aircraft
if( _cameraType == AIRCRAFT )
D3DXMatrixRotationAxis(&T, &_up, angle);

// rotate _right and _look around _up or y-axis
D3DXVec3TransformCoord(&_right,&_right, &T);
D3DXVec3TransformCoord(&_look,&_look, &T);
}

void Camera2::roll(float angle)
{
// only roll for aircraft type
if( _cameraType == AIRCRAFT )
{
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T, &_look, angle);

// rotate _up and _right around _look vector
D3DXVec3TransformCoord(&_right,&_right, &T);
D3DXVec3TransformCoord(&_up,&_up, &T);
}
}

void Camera2::getViewMatrix(D3DXMATRIX* V)
{
D3DXMatrixIdentity(V);
// Keep camera's axes orthogonal to eachother
D3DXVec3Normalize(&_look, &_look);

D3DXVec3Cross(&_up, &_look, &_right);
D3DXVec3Normalize(&_up, &_up);

D3DXVec3Cross(&_right, &_up, &_look);
D3DXVec3Normalize(&_right, &_right);

// Build the view matrix:
float x = -D3DXVec3Dot(&_right, &_pos);
float y = -D3DXVec3Dot(&_up, &_pos);
float z = -D3DXVec3Dot(&_look, &_pos);

(*V)(0,0) = _right.x; (*V)(0, 1) = _up.x; (*V)(0, 2) = _look.x; (*V)(0, 3) = 0.0f;
(*V)(1,0) = _right.y; (*V)(1, 1) = _up.y; (*V)(1, 2) = _look.y; (*V)(1, 3) = 0.0f;
(*V)(2,0) = _right.z; (*V)(2, 1) = _up.z; (*V)(2, 2) = _look.z; (*V)(2, 3) = 0.0f;
(*V)(3,0) = x; (*V)(3, 1) = y; (*V)(3, 2) = z; (*V)(3, 3) = 1.0f;

}

void Camera2::setCameraType(CameraType cameraType)
{
_cameraType = cameraType;
}


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