Archived

This topic is now archived and is closed to further replies.

Want 2 move camera target with camera

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

I want to move the target point of my camera whenever I move my cameras position. Its weird having to move and remain focused at just one point in the entire scene; The following is my code ProcessKeyboard() { BYTE KeyboardState[256]; lpKeyboard->GetDeviceState(sizeof(KeyboardState),(LPVOID)&KeyboardState); if(KEYDOWN(KeyboardState, DIK_LEFT)) { CurrentPos.xPos--; } else if(KEYDOWN(KeyboardState, DIK_UP)) { CurrentPos.zPos++; } else if(KEYDOWN(KeyboardState, DIK_DOWN)) { CurrentPos.zPos--; } else if(KEYDOWN(KeyboardState, DIK_RIGHT)) { CurrentPos.xPos++; } }; Current is the struct variable holding the coordinates of my camera position, I know I will have to use another variable to hold my target coordinates, but how do I set them so that the camera is always facing ahead.

Share this post


Link to post
Share on other sites
You need to move the camera target point along with the camera position. Do something like this

f(KEYDOWN(KeyboardState, DIK_LEFT))
{
CameraEye++;
CameraAt++;
}

Best is if you make a function for it. ie This way you can move left and right with the same function buy making the speed < 0 and > 0

f(KEYDOWN(KeyboardState, DIK_LEFT))
{
MoveCam(float speed); // ie speed is 0.01f
}

voic MoveCam(float speed)
{
CameraEye += speed;
CameraAt += speed;
}

Ofcourse the code above isnt complete, but its just to give you an idea!?

[edited by - D3DXVECTOR3 on November 10, 2003 6:32:22 AM]

Share this post


Link to post
Share on other sites
Thats a neat piece of code, thanks.

I got the target coordinates move with my camera but now I actually want to make the target view rotate on the Y axis when I press the left right keys and like rotate upwards downwards when I press the up and down keys.

You know like in some 3D first person perspective games and walkthroughs. (except that I'm using the keyboard instead of my mouse)

I understand theres some trigonometry involved but don't seem to be getting the right functions to work this out, any help here please

[edited by - ParityCheck on November 10, 2003 9:25:22 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The easiest way is to multiply the object''s world matrix against the Camera''s matrix. Or, if you don''t want the objects rotation included, multiply the objects world position vector and the camera''s matrix. These examples will keep the camera centered on the object

- i might have the operation order reversed - i don''t think so tho.

hope that helped

Share this post


Link to post
Share on other sites
Actually right now I just want to rotate my cameras view target coordinates with the press of the arrow keys. at the moment they r just mapped onto the same coordinates as the camera position, this doesn''t allow for rotation and ''turning'' around.. I''m not trying to focus on any objet at the moment though just want to turn my camera around.

Share this post


Link to post
Share on other sites
Here''s what I did:

int KeyLeft()
{
LookAtAngle -= TurnSpeed;
return 1;
}
int KeyRight()
{ LookAtAngle += TurnSpeed;
return 1;
}


and then for the matrices...


void SetupMatrices()
{
D3DXMATRIXA16 matWorld;
D3DXMATRIXA16 matTemp;
D3DXMATRIXA16 matView;
D3DXMATRIXA16 matProj;
D3DXVECTOR3 LPos, CameraPos, CameraLPos;
D3DXMatrixIdentity( &matWorld );
D3DXMatrixRotationX( &matWorld, 0.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
LPos.x = (float)sin(LookAtAngle);
LPos.y = sinf(LookAtUpAngle);
LPos.z = (float)cos(LookAtAngle);
CameraPos = g_Player.GetPosition();
CameraLPos = LPos + CameraPos;
D3DXVECTOR3 vLookatPt(LPos.x + CameraPos.x, LPos.y + CameraPos.y, LPos.z + CameraPos.z);
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &matView, &CameraPos, &CameraLPos, &up );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 0.001f, 10000.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}


Hope that helps

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
it''s the same example I gave before, just multiply the position you want to rotate around by the camera''s matrix - nice and simple, and it should work great!

Share this post


Link to post
Share on other sites
Thanks a lot man, That code is a life saver man. Now all I got to do is to make my camera move back and forth in the direction its facing, I''m trying out some substitutions but I would most gratefully welcome any suggestions :D

Share this post


Link to post
Share on other sites
Alright my camera is now rotating perfectly HURRAY !!! But somehow I''m having trouble trying to get my camera to move in the direction of its view target. (I never was very good at maths tHat''ll remind me to pay more attention in class ). I''ve tried deputing my pg up and pg down keys to move towards and away from the direction of the target coordinates respectively, but It appears that just incrememnting along the cameras coordinates are not enough. Any suggestions guys , I know I''m missing something ..

Share this post


Link to post
Share on other sites