Archived

This topic is now archived and is closed to further replies.

Camera problems..

This topic is 5124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been trying to get my camera to work like in your a virutal world or any game like quake, ect. And the code im using isnt working very well i got it from one of the tutorials on the site it was orginally in opengl and i tried to convert it into DirectX instead. So heres what i got so far thats not working very well, I don't know why though it looks just like the one I saw but uses DirectX functions instead. I used the article on this site called The quaternion camera, it can be found here: http://www.gamedev.net/reference/programming/features/quatcam/page3.asp Does anyone know why my camera isn't working like as it would work in a 3d game? Any help would be great!
// 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]

Share this post


Link to post
Share on other sites
The EASIEST way (ie probablky not the most efficient) is to use a matrix and translate and rotate it like you would for any object in your world. But before you set it as the View matrix, inverse it and set the inverted matrix as the view. This has the benefit of being able to use the matrix of any object in your world and make it have a camera attached very easily.

Neil

WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!

Share this post


Link to post
Share on other sites