0 Replies - 2029 Views - Last Post: 24 August 2006 - 06:00 PM Rate Topic: -----

#1 -pete-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 06

How to stop a camera going crazy?

Post icon  Posted 24 August 2006 - 06:00 PM

Hey all, hope you can help me here!

Alright so I am trying to create a user input class for my amazing pryamid collection game, the problem is -well- the camera is all screwy, if i turn in some angles it goes forward like it should, at other angles it just goes backwards?

This is the code that i have so far, bear in mine that my level is incorrectly aligned so -z = up, +y = look and -x = right;

Here is what i do to create the camera.

void CCamera::setupCamera(LPDIRECT3DDEVICE9 gD3dDevice)
{
	mD3dDevice = gD3dDevice;
	
	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 5000.0f );
	gD3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
	
	D3DXMatrixIdentity(&matView);

	matView._11 = right.x; matView._12 = up.x; matView._13 = look.x;
	matView._21 = right.y; matView._22 = up.y; matView._23 = look.y;
	matView._31 = right.z; matView._32 = up.z; matView._33 = look.z;
	matView._41 = D3DXVec3Dot( &position, &right ); matView._42 = D3DXVec3Dot( &position, &up ); matView._43 = D3DXVec3Dot( &position, &look ); 1.0f;

	gD3dDevice->SetTransform(D3DTS_VIEW,&matView ); 

	// World Matrix
	// Just using identity (so triangle is at origin of world)
	
	D3DXMatrixIdentity(&matWorld);
	gD3dDevice->SetTransform(D3DTS_WORLD, &matWorld ); 
	
	
}


So once its created I have rotate functions that look like this:

void CCamera::panLeft (void)
{
	
	D3DXMATRIX yawMatrix;
	D3DXMatrixRotationAxis (&yawMatrix, &up, DEGTORAD(TURN_SPEED_LEFT));
	D3DXVec3TransformCoord (&look, &look, &yawMatrix);
	D3DXVec3TransformCoord (&right, &right, &yawMatrix);
	setupCamera (mD3dDevice);
	angle++;
	char buf[2048];
	sprintf(buf,"eyeZ is %f \n", angle);
	OutputDebugString(buf);


}

void CCamera::panRight(void)
{
	
	D3DXMATRIX yawMatrix;
	D3DXMatrixRotationAxis (&yawMatrix, &up, DEGTORAD(TURN_SPEED_RIGHT));
	D3DXVec3TransformCoord (&look, &look, &yawMatrix);
	D3DXVec3TransformCoord (&right, &right, &yawMatrix);
	setupCamera (mD3dDevice);
	
	angle--;
	char buf[2048];
	sprintf(buf,"eyeZ is %f \n", angle);
	OutputDebugString(buf);
}


and finally the function that is causing all the trouble:

void CCamera::moveForward (void)
{
	hTheta = 1*sin(angle);
	vTheta = 1*cos(angle);
	if (hTheta < 0)
	{hTheta = fabs (hTheta);}
	if (vTheta < 0)
	{vTheta = fabs (vTheta);}
	D3DXMatrixRotationX( &matWorld, vTheta );
	position.x = position.x + hTheta;
	position.y = position.y - vTheta;
	setupCamera (mD3dDevice);

	char buf[2048];
	sprintf(buf,"angle %f | hTheta %f | vTheta %f \n X %f | Z %f \n", angle, hTheta, vTheta, position.x, position.z);
	OutputDebugString(buf);
	

}


If someone could help me work this out I would be very VERY thankful, I'm totally stuck and have being for about 4 hours now. :( :(

If you need any more source code its online here

Is This A Good Question/Topic? 0
  • +

Page 1 of 1