Jump to content
  • Advertisement

Archived

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

Bacardi34

First person view of meshes

This topic is 5762 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 am trying to design a first person view of a world, where you see the things you are holding, like a FPS, but im not using guns. I have the camera all setup and you can move around the world, but i cant get objects, like arms/hands/weapons, to be "attached" to the camera and move with it. What i tried to do, is when the scene inits, the camera is setup, and the objects attached to it are setup just infront of the camera. Then a position matrix is created and stored for each one. Then when you press forward/back/left/right, a move vector is used to create a translation/rotation. Ok, thats the easy part. So i tried to use the same move vectors to translate the camera and the objects attached to it. But nothing is happening to the objects, they are just staying where they were first created. Here is the code i am using for movement.
  
// This function will get the input from the keyboard and acts on it


	D3DXVECTOR3		vTrans( 0.0, 0.0, 0.0 );				// Tracks camera point movement

	D3DXVECTOR3		vRot( 0.0, 0.0, 0.0 );				// Tracks camera rotation

	D3DXMATRIX		matView;									// Will be the View Matrix

	float			time	= g_Timer.GetTimeSinceLastFrame();
	D3DXMatrixIdentity( &matView );

	// First get the keyboard input

	g_pdiKeyboard->GetDeviceState( sizeof( g_keyboardBuffer ), ( LPVOID )&g_keyboardBuffer );
	// now act on the keyboard input

	// If the escape key is pressed, then quit the siulation

	if( KEYDOWN( g_keyboardBuffer, DIK_ESCAPE ) )
	{
		//MessageBox( NULL, "ESCAPE", "ESCAPING!", NULL );

		PostMessage( g_hwnd, WM_DESTROY, 0, 0 );
		return;
	}

	// if the up key is pressed, then move the camera forward.

	if( KEYDOWN( g_keyboardBuffer, DIK_UP ))
	{
		vTrans.z += MOVESPEED * time;		
	}
	// if down is pressed, move the camera back

	if( KEYDOWN( g_keyboardBuffer, DIK_DOWN ))
	{
		vTrans.z -= MOVESPEED * time;		
	}
	// right is pressed, rotate the camera right

	if( KEYDOWN( g_keyboardBuffer, DIK_RIGHT ))
	{
		vRot.x += ROTATIONSPEED * time;
	}
	// left is pressed, rotate left

	if( KEYDOWN( g_keyboardBuffer, DIK_LEFT ))
	{
		vRot.x -= ROTATIONSPEED * time;
	}

	// Now translate the camera

	D3DXMATRIX matTrans, matRot, matPosition;
	D3DXQUATERNION qRot;

	// First translate the position of the camera

	matPosition = g_Camera.GetPositionMatrix();
	D3DXMatrixTranslation( &matTrans, vTrans.x, vTrans.y, vTrans.z );
	D3DXMatrixMultiply( &matPosition, &matTrans, &matPosition );
	// Next handle the rotation of the camera

	D3DXQuaternionRotationYawPitchRoll( &qRot, vRot.x, vRot.y, vRot.z );
	D3DXMatrixRotationQuaternion( &matRot, &qRot );
	D3DXMatrixMultiply( &matPosition, &matRot, &matPosition );
	D3DXMatrixInverse( &matView, NULL, &matPosition );
	// Reset the camera view and position matrices

	g_Camera.SetPositionMat( matPosition );
	g_Camera.SetViewMat( matView );

	g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
	g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &g_Camera.GetProjMatrix() );

	// Now translate the sword position

	// First translate the position 

	matPosition = g_Sword.GetWorldMatrix();
	D3DXMatrixTranslation( &matTrans, vTrans.x, vTrans.y, vTrans.z );
	D3DXMatrixMultiply( &matPosition, &matTrans, &matPosition );
	// Next handle the rotation 

	D3DXQuaternionRotationYawPitchRoll( &qRot, vRot.x, vRot.y, vRot.z );
	D3DXMatrixRotationQuaternion( &matRot, &qRot );
	D3DXMatrixMultiply( &matPosition, &matRot, &matPosition );
	// Reset the position matrices

	g_Sword.SetWorldMatrix( matPosition );
	g_Sword.SetMatrix( &matPosition );
  
This is the first 3D game i have attempted, and thus really the first time i have been exposed to such in depth matrix math, so i am prolly just missing something. Can anyone make suggestions on what i am doing wrong, or a better way to get this done. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I believe that is done with the stencil buffer. My knowledge of graphics isn''t very strong though. Basically any time you want to draw directly on the screen plane rather than in the scene you use the stencil buffer.

Share this post


Link to post
Share on other sites
Draw entire scean then reset entire matrix so you are at origin and looking forward then draw hands and stuff over the world.

and the stencil buffer is used to cut out secions of the screen so you don't affect areas on it that would not be desireable, such as with a mirror.

[edited by - Xero-X2 on February 8, 2003 11:44:16 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!