View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Direct3D 9 First Person Camera help?

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.

4 replies to this topic

### #1littletray26  Members

Posted 28 May 2012 - 01:32 AM

Hey, I'm new to gamedev.net and would love some help!

I've just started writing a game which I plan to be FPS. I've got the Camera class done which I'll dump the code for below.
I was just wondering if anyone could help me (like walk me through and explain each step) to convert this camera to a First Person Camera? Or link me to a tutorial that would? I started not long ago so I have a basic understanding of Vectors but I don't really understand anything like "normalizing" so I do need a bit of assistance

Also some basic tips on improving what I have and making it generally better. I'm a beginner so please go easy :3

Thanks!

===Camera.cpp===

//Camera source file
#include "Camera.h"
Camera::Camera(D3DXVECTOR3 pos, D3DXVECTOR3 lookAt, D3DXVECTOR3 up, float FOV, float aspect)
{
Camera::pos = pos;
Camera::lookAt = lookAt;
Camera::up = up;
D3DXMatrixPerspectiveFovLH(&projection, FOV, aspect, 0, 100);
}
void Camera::UpdateLook()
{
D3DXMatrixLookAtLH(&view, &pos, &lookAt, &up);
}


===Camera.h===


#include "Main.h"

class Camera
{
private:
D3DXVECTOR3 pos;
D3DXVECTOR3 lookAt;
D3DXVECTOR3 up;

public:
D3DXMATRIX view;
D3DXMATRIX projection;

Camera(D3DXVECTOR3 pos, D3DXVECTOR3 lookAt, D3DXVECTOR3 up, float FOV, float aspect);
void UpdateLook();

};



===Cameras place in main file

void Draw(LPDIRECT3DDEVICE9 rd3dDevice)
{
//Draw Code
camera.UpdateLook();
rd3dDevice->SetTransform(D3DTS_VIEW, &camera.view);
rd3dDevice->SetTransform(D3DTS_PROJECTION, &camera.projection);
}


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

### #2hupsilardee  Members

Posted 28 May 2012 - 02:36 AM

If I was going to make a FPS camera class it would look something like yours but with a different internal representation of the look vector as spherical coordinates. This makes the logic for mouse-look much easier to understand and requires a simple spherical-to-Cartesian coordinate transformation whenever you construct the View matrix

// Camera.h
struct Camera
{
D3DXVECTOR3 Position;
float RotX;
float RotY;

D3DXMATRIX View;
D3DXMATRIX Proj;

Camera(D3DXVECTOR3& position, float fov, float aspect);
void Update();
};


// Camera.cpp
Camera::Camera(D3DXVECTOR3& position, float fov, float aspect)
{
Position = position;
RotX = 0;
RotY = 0;
D3DXMatrixPerspectiveFovLH(&Proj, fov, aspect, 0, 100);
}
void Camera::Update()
{
POINT mousePos;
GetCursorPos(&mousePos);
SetCursorPos(640, 480);
RotX += (float)(mousePos.y - 480) / 500.f;
RotY += (float)(mousePos.x - 640) / 500.f;
// change 500.f to any value to adjust sensitivity
// change += to -= if it rotates the wrong way
D3DXVECTOR3 look = D3DXVECTOR3(cosf(RotY) * sinf(RotX), cosf(RotX), sinf(RotY) * sinf(RotX));
D3DXVECTOR3 target = Position + look;
D3DXVECTOR3 up = D3DXVECTOR3(0, 1, 0);
D3DXMatrixLookAtLH(&View, &Position, &target, &up);
}


### #3littletray26  Members

Posted 28 May 2012 - 03:32 AM

Hey Hupsilardee, could you tell me what this part of your code means/does?

D3DXVECTOR3 look = D3DXVECTOR3(cosf(RotY) * sinf(RotX), cosf(RotX), sinf(RotY) * sinf(RotX));

Edited by littletray26, 28 May 2012 - 03:33 AM.

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

### #4hupsilardee  Members

Posted 28 May 2012 - 04:00 AM

First, did the code work as you want?

http://en.wikipedia.org/wiki/Spherical_coordinates

Basically, instead of storing a vector that tells me the direction the camera is looking, I store two angles - one is the "up/down" angle and the other is the "left/right" angle. In a spherical coordinate system, there is a third number - r, the distance from the origin - but that is always 1 in this case so I leave it out.

The advantage of storing the angles rather than the vector are that it is very easy to adjust the up/down/left/right angles through which the camera is looking - as you can see, I use the mouse like most FPS games do.

Now, to build the View matrix I need to pass in a target vector, described by the camera position + look vector, so I need to calculate the look vector from the spherical coordinates (RotX, RotY, 1). That line of code gives the look vector in terms of RotX and RotY (multiplied by r = 1)

### #5littletray26  Members

Posted 28 May 2012 - 04:42 PM

First, did the code work as you want?

Nope, I tried it out and it didn't do anything. The camera stayed in one place. I may have put it in wrong but I'm not sure. I'd dump the code but I'm at school.
I'll have another look at it when I get home and I'll let you know
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

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.