locked
Setting up first person camera

    Question

  • I'm making a first person shooter game and I'm using this code

    // Initializes view parameters when the window size changes.
    void Game::CreateWindowSizeDependentResources()
    {
    	m_miscConstants.ViewportHeight = m_deviceResources->GetScreenViewport().Height;
    	m_miscConstants.ViewportWidth = m_deviceResources->GetScreenViewport().Width;
    	m_graphics.UpdateMiscConstants(m_miscConstants);
    
    	m_graphics.GetCamera().SetOrientationMatrix(m_deviceResources->GetOrientationTransform3D());
    
    	Size outputSize = m_deviceResources->GetOutputSize();
    
    	// Setup the camera parameters for our scene.
    	m_graphics.GetCamera().SetViewport((UINT)outputSize.Width, (UINT)outputSize.Height);
    	m_graphics.GetCamera().SetPosition(XMFLOAT3());
    	m_graphics.GetCamera().SetLookAt(XMFLOAT3(0.0f, 0.0f, 0.0f));
    
    	float aspectRatio = outputSize.Width / outputSize.Height;
    	float fovAngleY = 70.0f * XM_PI / 180.0f;
    
    	if (aspectRatio < 1.0f)
    	{
    		// Portrait or snap view
    		m_graphics.GetCamera().SetUpVector(XMFLOAT3(1.0f, 0.0f, 0.0f));
    		fovAngleY = 120.0f * XM_PI / 180.0f;
    	}
    	else
    	{
    		// Landscape view.
    		m_graphics.GetCamera().SetUpVector(XMFLOAT3(0.0f, 1.0f, 0.0f));
    	}
    
    	m_graphics.GetCamera().SetProjection(XM_PI / 2, 1.0f, 0.01f, 100.0f);
    
    	// Setup lighting for our scene.
    	static const XMVECTORF32 s_vPos = { 5.0f, 5.0f, -2.5f, 0.f };
    
    	XMFLOAT4 dir;
    	DirectX::XMStoreFloat4(&dir, XMVector3Normalize(s_vPos));
    
    	m_lightConstants.ActiveLights = 1;
    	m_lightConstants.Ambient = XMFLOAT4(0.3f, 0.3f, 0.3f, 1.0f);
    	m_lightConstants.IsPointLight[0] = false;
    	m_lightConstants.LightColor[0] = XMFLOAT4(0.8f, 0.8f, 0.8f, 1.0f);
    	m_lightConstants.LightDirection[0] = dir;
    	m_lightConstants.LightSpecularIntensity[0].x = 2;
    
    	m_graphics.UpdateLightConstants(m_lightConstants);
    }


    How do I set the cameras position to first person?

    Thursday, April 16, 2015 4:34 AM

Answers

  • What does setting the cameras position to first person mean?

    Talk through the answer to that question and then think about how to implement that in code.

    For example, if you define a first person camera as:
    "A first person camera is positioned at head height and moves around with the player, aiming in the direction the user looking."

    Then you need to set two properties on the camera:

    1. The match the player's position, at head height
    2. The direction it is looking: to match the direction the player is looking

    And if you look at the Camera you'll see it has functions to set each of these, so as the user moves you can call SetPosition and SetLookAt to match the player's position and direction.

    Thursday, April 16, 2015 6:31 AM
    Owner

All replies

  • What does setting the cameras position to first person mean?

    Talk through the answer to that question and then think about how to implement that in code.

    For example, if you define a first person camera as:
    "A first person camera is positioned at head height and moves around with the player, aiming in the direction the user looking."

    Then you need to set two properties on the camera:

    1. The match the player's position, at head height
    2. The direction it is looking: to match the direction the player is looking

    And if you look at the Camera you'll see it has functions to set each of these, so as the user moves you can call SetPosition and SetLookAt to match the player's position and direction.

    Thursday, April 16, 2015 6:31 AM
    Owner
  • Like this?

    void UpdateCamera()
    {
    	// Rotate target around camera
    	/*camRotationMatrix = XMMatrixRotationRollPitchYaw(camPitch, camYaw, 0);
    	camTarget = XMVector3TransformCoord(DefaultForward, camRotationMatrix );
    	camTarget = XMVector3Normalize(camTarget);*/
    
    	/*XMMATRIX RotateYTempMatrix;
    	RotateYTempMatrix = XMMatrixRotationY(camYaw);
    
    	// Walk
    	camRight = XMVector3TransformCoord(DefaultRight, RotateYTempMatrix);
    	camForward = XMVector3TransformCoord(DefaultForward, RotateYTempMatrix);
    	camUp = XMVector3Cross(camForward, camRight);*/
    
    	/*// Free Cam
    	camRight = XMVector3TransformCoord(DefaultRight, camRotationMatrix);
    	camForward = XMVector3TransformCoord(DefaultForward, camRotationMatrix);
    	camUp = XMVector3Cross(camForward, camRight);*/
    
    	/*camPosition += moveLeftRight*camRight;
    	camPosition += moveBackForward*camForward;
    
    	moveLeftRight = 0.0f;
    	moveBackForward = 0.0f;
    
    	camTarget = camPosition + camTarget;*/
    
    	
    	// This line is because this lessons model was set to stand on the point (0,0,0) (my bad), and we
    	// don't want to just be looking at the models feet, so we move the camera's target vector up 5 units
    	camTarget = XMVectorSetY(camTarget, XMVectorGetY(camTarget) + 5.0f);
    
    	// Unlike before, when we rotated the cameras target vector around the cameras position,
    	// we are now rotating the cameras position around it's target (which is the character)
    	// Rotate camera around target
    	camRotationMatrix = XMMatrixRotationRollPitchYaw(-camPitch, camYaw, 0);
    	camPosition = charPosition;
    
    	// Set our cameras position to rotate around the character. We need to add 5 to the characters
    	// position's y axis because i'm stupid and modeled the character in the 3d modeling program
    	// to be "standing" on (0,0,0), instead of centered around it ;) Well target her head here though
    	camPosition = (camPosition * charCamDist) + camTarget;
    
    	// We need to set our cameras forward and right vectors to lay
    	// in the worlds xz plane, since they are the vectors we will
    	// be using to determine the direction our character is running
    	camForward = XMVector3Normalize(camTarget - camPosition);	// Get forward vector based on target
    	camForward = XMVectorSetY(camForward, 0.0f);	// set forwards y component to 0 so it lays only on
    	// the xz plane
    	camForward = XMVector3Normalize(camForward);
    	// To get our camera's Right vector, we set it's x component to the negative z component from the
    	// camera's forward vector, and the z component to the camera forwards x component
    	camRight = XMVectorSet(-XMVectorGetZ(camForward), 0.0f, XMVectorGetX(camForward), 0.0f);
    
    	// Our camera does not "roll", so we can safely assume that the cameras right vector is always
    	// in the xz plane, so to get the up vector, we just get the normalized vector from the camera
    	// position to the cameras target, and cross it with the camera's Right vector
    	camUp = XMVector3Cross(XMVector3Normalize(camPosition - camTarget), camRight);
    
    	camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);
    }



    Monday, May 18, 2015 3:57 AM