Jump to content

  • Log In with Google      Sign In   
  • Create Account


A small bug with my First person camera


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 littletray26   Members   -  Reputation: 267

Like
0Likes
Like

Posted 31 May 2012 - 03:44 AM

Hey guys :)

I finally worked out a basic FPS Camera but I have a small bug. It's a quite common one I know.

When I go in circles my camera ends up rolling. I thought using the world up vector in my Yaw function would stop this but apparently not.

Mind the bad code, it's my first camera :) I'll improve


//Camera source file
#include "Camera.h"

	 float rotX = 0;
	 float rotY = 0;

Camera::Camera(D3DXVECTOR3 pos, D3DXVECTOR3 lookAt, D3DXVECTOR3 up, float FOV)
{
	 Camera::pos = pos;
	 Camera::lookAt = lookAt;
	 Camera::lookDir = Camera::lookAt - Camera::pos;
	 Camera::up = up;
	 playerSpeed = 0.3f;

	 D3DXMatrixPerspectiveFovLH(&projection, FOV, 800.0f / 600.0f, 0, 100);
}

void Camera::UpdateLook()
{ 
	 D3DXVec3Normalize(&lookDir, &lookDir);
	 D3DXVec3Cross(&right, &up, &lookDir); 

	 ///////////////////////////////
	 if (GetAsyncKeyState(0x57))
		  pos += D3DXVECTOR3(lookDir.x, 0, lookDir.z) * playerSpeed;
	 else
	 if (GetAsyncKeyState(0x53))
		  pos -= D3DXVECTOR3(lookDir.x, 0, lookDir.z) * playerSpeed;

	 if (GetAsyncKeyState(0x41))
		  pos -= D3DXVECTOR3(right.x, 0, right.z) * playerSpeed;
	 else
     if (GetAsyncKeyState(0x44))
	 pos += D3DXVECTOR3(right.x, 0, right.z) * playerSpeed;

	 if (GetAsyncKeyState(VK_LEFT))
		  rotX -= 0.04f;
	 else
	 if (GetAsyncKeyState(VK_RIGHT))
		  rotX += 0.04f;
	 if (GetAsyncKeyState(VK_UP))
		  rotY -= 0.04f;
	 else
     if (GetAsyncKeyState(VK_DOWN))
			   rotY += 0.04f;
	 //////////////////////////////// 

	 pitch(rotY);
	 yaw(rotX);

	 D3DXMatrixLookAtLH(&view, &pos, &(pos + lookDir), &up);
	 rotX = 0;
	 rotY = 0;
}

void Camera::pitch(float A)
{
	 D3DXMATRIX T;
	 D3DXMatrixRotationAxis(&T, &right, A);

	 D3DXVec3TransformCoord(&up, &up, &T);
	 D3DXVec3TransformCoord(&lookDir, &lookDir, &T);
}

void Camera::yaw(float A)
{
	 D3DXMATRIX T;
	 D3DXMatrixRotationY(&T, A);

	 D3DXVec3TransformCoord(&right, &right, &T);
	 D3DXVec3TransformCoord(&lookDir, &lookDir, &T);
}

The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky :)

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS