Jump to content
  • Advertisement
Sign in to follow this  
littletray26

Direct3D 9 First Person Camera help?

This topic is 2336 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, 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 tongue.png

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===


//Camera header

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

Share this post


Link to post
Share on other sites
Advertisement
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);
}

Share this post


Link to post
Share on other sites
Hey Hupsilardee, could you tell me what this part of your code means/does?

Thanks for replying :)


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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites
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!