Sign in to follow this  
ph33r

Troubles rotating my view matrix.(FPS style)

Recommended Posts

I'm working on an FPS type camera system where when you move the mouse left and right it rotates on the worlds axis. Though I can't get it to work properly it always rotates on the local axis. If I rotate my camera when its at its identity, it rotates correctly. If look up slightly and rotate my camera it rotates around it's local Y axis, to give this wierd rolling rotation. Here is my code
	D3DXMATRIX local = *( ((D3DXMATRIX*)(&m_local)) );

	// vec.y is the mouse's delta value in y direction
	// vec.x is the mouse's delta value in x direction
	// z is always 0
	D3DXMATRIX rotationMatrix;
	D3DXMatrixRotationYawPitchRoll( &rotationMatrix, vec.y, vec.x, vec.z );

	// Pre-concat
	D3DXMatrixMultiply( &local, &local, &rotationMatrix );

	// Create the world up
	D3DXVECTOR3 zAt, xRight, yUp, worldUp;
	worldUp.x = 0; worldUp.y = 1; worldUp.z = 0;
	
	// Get the look at
	zAt.x = local._31; zAt.y = local._32; zAt.z = local._33;
	D3DXVec3Normalize(&zAt, &zAt);
    
	// Get the right vector by crossing with world up
	D3DXVec3Cross( &xRight, &worldUp, &zAt );
	D3DXVec3Normalize( &xRight, &xRight );

	// Get the up vector
	D3DXVec3Cross( &yUp, &zAt, &xRight );
	D3DXVec3Normalize( &yUp, &yUp );

	// Set the x.
	local._11 = xRight.x;
	local._12 = xRight.y;
	local._13 = xRight.z;	

	// Set the y
	local._21 = yUp.x;
	local._22 = yUp.y;
	local._23 = yUp.z;

	// Set the z
	local._31 = zAt.x;
	local._32 = zAt.y;
	local._33 = zAt.z;

	memcpy( &m_local, &local, sizeof(D3DXMATRIX) );

Share this post


Link to post
Share on other sites
Truthfully, I don't understand what you're doing after the
D3DXMatrixMultiply( &local, &local, &rotationMatrix );
line.

If you want to rotate around the world axis, I'd recommend you'd seperate your matrix into two, one for rotation and one for translation, since if you try to rotate a translated matrix, you'd affect the translation in the process.

Keep a rotation matrix. If you want to turn, multiply it by another rotation matrix (the order affects whether you use local axis or world axis).

Also keep a translation matrix. If you want to move, multiply this by another translation matrix (for local axis movement, you'd need to transform the vector to local axis using D3DXVec3TransformCoord with the rotation matrix).

Then, when you need to pass this to DX, multiply the two before passing them along, and you should avoid nearly all problems.

(you can also add scaling, if you need it).

Hope this helps :).

Share this post


Link to post
Share on other sites
it looks like you're trying to directly set the variables when this is not even needed. You only need 1 matrix. Call the matrix yaw pitch roll on it, set the _41, _42, _43 to the translation xyz respectively and then simply inverse the matrix with D3DXMatrixInverse() and that'll be it.

Quote:

// vec.y is the mouse's delta value in y direction
// vec.x is the mouse's delta value in x direction


it accepts angles in radians. Add v.xyz to saved angles.xyz instead.

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