Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

first person camera

Started by pikebu Dec 14, 2005 at 10:45 AM 5 replies 850+ views
Original Post
pikebu
pikebu
Hi, I want to create a first person camera view. I created a view with: device.Transform.View = Matrix.LookAtLH( new Vector3(0,0,0), new Vector3(0,0,0), new Vector3(0,1,0)); When I now try to rotate my view (the camera), I always rotate around the origin of the coord system. What I need is to be able to rotate around the center of my camera. How can be this realized? Hope that anyone knows a solution for that? Best regards Pikebu
Blew
Blew
Translate the world to your camera position, rotate it, and then translate it back.
blew
thallish
thallish
Quote:
Original post by Blew
Translate the world to your camera position, rotate it, and then translate it back.


same same but different

translate camera position to origo
rotate
translate back

Which is more used?

regards/thallishI don't care if I'm known, I'd rather people know me
pikebu
pikebu
Thanks for your answers,

how can I get the actual camera position in DirectX?
D_a_M_a_G_e_D
D_a_M_a_G_e_D
//Global variables.
D3DXMATRIXA16 matWorld, matView;

//Void to initialize the matrices;
void InitMatrices()
{
D3DXMatrixIdentity(&matWorld);
Dev->SetTransform(D3DTS_WORLD, &matWorld);

D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0, 0, -1.0f),
&D3DXVECTOR3(0, 0, 0),
&D3DXVECTOR3(0, 1.0f, 0));

Dev->SetTransfrom(D3DTS_VIEW, &matView);
}

//Moves the camera and a model if one.
void MoveTheCamera()
{
//say that we have a variable named bKeys[256];
//that holds the keys are pressed.

D3DXVECTOR3 vT(0, 0, 0); //Translation Vector.
D3DXVECTOR3 vR(0, 0, 0); //Rotation Vector.

if(bKeys[VK_UP] == true) vT.z += 1.0f; //Translation in the z axis.
if(bKeys[VK_DOWN] == true) vT.z -= 1.0f;
if(bKeys[VK_LEFT] == true) vR.x -= 1.0f;//Rotation in the x axis.
if(bKeys[VK_RIGHT]== true) vR.x += 1.0f;
if(bKeys['W'] == true) vR.y += 1.0f;//Rotation in the y axis.
if(bKeys['S'] == true) vR.y -= 1.0f;
if(bKeys['A'] == true) vT.x -= 1.0f;//Translation int the x axis.
if(bKeys['D'] == true) vT.x += 1.0f;

//Anyway put as many transformations as you want.


D3DXMATRIXA16 matT, matR; //Matrices for translation and rotation.
D3DXQUATERNION qR; //The quaternion, Rotates around an axis.

//move to our new position.
D3DXMatrixTranslation(&matT, vT.x, vT.y, vT.z);
D3DXMatrixMultiply(&matWorld, &matT, &matWorld);

//Rotate to our new position.
D3DXQuaternionRotationYawPitchRoll(&qR, vR.x, vR.y, vR.z);
D3DXMatrixRotationQuaternion(&matR, &qR);
D3DXMatrixMultiply(&matWorld, &matR, &matWorld);

//Make our camera view.
D3DXMatrixInverse(&matView, NULL, &matWorld);

//This code is for moving around the world with the camera and a model.
//Say that you have a model that you want to move use the matWorld matrix.
//To move the camera use the matView matrix.

//if you want the camera to have a distance with the model do this.

//matView._41 = - or + a number. moves left or right.
//matView._42 = - or + a number. moves down or up.
//matView._43 = - or + a number. moves out or in.
}

void Render()
{
Dev->SetTransform(D3DTS_VIEW, &matView);

Dev->SetTransfrom(D3DTS_WORLD, &matWorld);

//Draw the model;
.....
.....
.....
///
}



Sorry about my english if there is any mistake but nobody is perfect and
i'm not nobody.

If you have any questions let me know.







D_a_M_a_G_e_D
D_a_M_a_G_e_D
//And if you want to get the camera position do this

void GetCameraPos(D3DXMATRIXA16 *matPos)
{
Dev->GetTransform(D3DTS_VIEW, matPos);
}

good luck!!!
D_a_M_a_G_e_D
D_a_M_a_G_e_D
this is c++ code but if you take a look at it you'll understand what i'm
talking about.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.