How to stop a camera from going crazy

Started by
4 comments, last by ViLiO 17 years, 8 months ago
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.
[source lang = "cpp"]
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:
[source lang = "cpp]
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 applogies for cross-posting, this is assignment work and I really need some help pretty soon-ish (it has to be in tomorrow :o
Advertisement
I think your problem is that in the code for rotating the camera you have
angle++
angle is in radians and you are adding it like it's in degrees.

you probably want it to be
angle += DEGTORAD(TURN_SPEED_LEFT)
In your moveForward function you do
position.x = position.x + hTheta;position.y = position.y - vTheta;
...why?

You have a perfectly good look vector, you should use it [smile]

Try changing that to...
position.x += look.x;position.y += look.y;

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
Ok thanks, rotation seems to be alright now (on its own at least) the problem I'm having is moving after the rotations, instead of going the direction the screen is facing it seems to be going in a completely different direction.
Quote:Original post by ViLiO
In your moveForward function you do
position.x = position.x + hTheta;position.y = position.y - vTheta;
...why?

You have a perfectly good look vector, you should use it [smile]

Try changing that to...
position.x += look.x;position.y += look.y;

Regards,
ViLiO


Oh my god that was so simple, thank you sir! You are my new bestest freind! :D

Also, your view matrix doesn't look quite right, try inverting the dot products you calculate for m41, m42 and m43 so it reads ...
matView._41 = -D3DXVec3Dot( &position, &right );matView._42 = -D3DXVec3Dot( &position, &up );matView._43 = -D3DXVec3Dot( &position, &look );matView._44 = 1.0f;
Some other bits look a bit weird too but I am too tired to really examine it [smile]

All the best,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube

This topic is closed to new replies.

Advertisement