Jump to content
  • Advertisement
Sign in to follow this  
littletray26

A small bug with my First person camera

This topic is 2271 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

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);
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • 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!