// CCamera.h
class CCamera
{
public:
CCamera();
~CCamera();
void SetViewByMouse();
void RotateCamera(double Angle, double x, double y, double z);
D3DXVECTOR3 Position, View, Up;
private:
protected:
// the coordinates of our mouse coordinates
D3DXVECTOR3 Axis;
int MouseX, MouseY;
int MiddleX, MiddleY;
float MouseSensitivity;
float CurrentRotationX;
};
/*************************************************************/
// CCamera.cpp
void CCamera::SetViewByMouse()
{
POINT mouse;
// the middle of the screen in the x direction
MiddleX = 640/2+GetSystemMetrics(SM_CXSCREEN)/4;
// the middle of the screen in the y direction
MiddleY = 480/2+GetSystemMetrics(SM_CYSCREEN)/4;
D3DXVECTOR3 MouseDirection(0, 0, 0);
// static variable to store the rotation about the x-axis, since
// we want to limit how far up or down we can look.
// We don't need to cap the rotation about the y-axis as we
// want to be able to turn around 360 degrees
static double CurrentRotationAboutX = 0.0;
// The maximum angle we can look up or down, in radians
double maxAngle = 85;
GetCursorPos(&mouse);
MouseX=mouse.x;
MouseY=mouse.y;
// if the mouse hasn't moved, return without doing
// anything to our view
if((MouseX == MiddleX) && (MouseY == MiddleY))
{
return;
}
// otherwise move the mouse back to the middle of the screen
SetCursorPos(MiddleX,MiddleY);
// We have to remember that positive rotation is counter-clockwise.
// Moving the mouse down is a negative rotation about the x axis
// Moving the mouse right is a negative rotation about the y axis
MouseDirection.x = (MiddleX - MouseX)/MouseSensitivity;
MouseDirection.y = (MiddleY - MouseY)/MouseSensitivity;
CurrentRotationX += MouseDirection.y;
// We don't want to rotate up more than one radian, so we cap it.
if(CurrentRotationX > 1)
{
CurrentRotationX = 1;
return;
}
// We don't want to rotate down more than one radian, so we cap it.
if(CurrentRotationX < -1)
{
CurrentRotationX = -1;
return;
}
else
{
// get the axis to rotate around the x-axis.
D3DXVECTOR3 res=View-Position;
D3DXVec3Cross(&Axis, &res, &Up);
// To be able to use the quaternion conjugate, the axis to
// rotate around must be normalized.
D3DXVECTOR3 AxisRes;
D3DXVec3Normalize(&AxisRes,&Axis);
// Rotate around the y axis
RotateCamera(MouseDirection.y, AxisRes.x, AxisRes.y, AxisRes.z);
// Rotate around the x axis
RotateCamera(MouseDirection.x, 0, 1, 0);
}
}
void CCamera::RotateCamera(double Angle, double x, double y, double z)
{
D3DXQUATERNION temp, quat_view, result, res2,res3;
temp.x = x * sin(Angle/2);
temp.y = y * sin(Angle/2);
temp.z = z * sin(Angle/2);
temp.w = cos(Angle/2);
quat_view.x = View.x;
quat_view.y = View.y;
quat_view.z = View.z;
quat_view.w = 0;
D3DXQuaternionConjugate(&res2,&temp);
D3DXQuaternionMultiply(&result, &res3, &res2);
D3DXQuaternionMultiply(&res3, &res2, &quat_view);
View.x = result.x;
View.y = result.y;
View.z = result.z;
}
coder requires 0xf00d before continue().Killer Eagle Software [edited by - DevLiquidKnight on December 1, 2003 2:43:13 PM]