Skip to main content
gamedev.net
🔒 Locked

Circular camera movement

Started by sumit381 2 replies 1.2k views
Original Post
sumit381
sumit381
I am trying to create a camera where it moves in a circle about a fixed 3D point. I have been using the following code and it results in a square like movement.
if (rotateCamera)

{

if (vEye.Z >= midZ && vEye.X <= midX)

{

//z movement

vEye.Z -= moveSpeed * elapsedTime;

//vLook.Z -= elapsedTime;

//x movement

vEye.X -= moveSpeed * elapsedTime;

//vLook.X += elapsedTime;

}

else if (vEye.Z <= midZ && vEye.X <= midX)

{

vEye.Z -= moveSpeed * elapsedTime;

//vLook.Z += elapsedTime;

vEye.X += moveSpeed * elapsedTime;

//vLook.X -= elapsedTime;

}

else if (vEye.Z <= midZ && vEye.X >= midX)

{

vEye.Z += moveSpeed * elapsedTime;

//vLook.Z -= elapsedTime;

vEye.X += moveSpeed * elapsedTime;

//vLook.X -= elapsedTime;

}

else

{

vEye.Z += moveSpeed * elapsedTime;

//vLook.Z -= elapsedTime;

vEye.X -= moveSpeed * elapsedTime;

//vLook.X += elapsedTime;

}

}
The midX, midY, midZ variables are midpoints on a grid I have set up. I basically want the camera to rotate around the grid while always facing in the middle of the grid. How can I make the camera move in a circle around a fixed point and also always have the camera facing that fixed point? Thank you for your help. EDIT: Please remember 'source' tags in future... [Edited by - jollyjeffers on March 28, 2007 9:33:31 AM]
Mr X
Mr X
Hi sumit381,

you have implemented a (piecewise) linear motion.
A circular motion can be implemented by using sin/cos.
Take a look at the following lines: (I haven't testet it, hope it works)

// Initialization ***************************************// view matrixD3DXMATRIXA16 view;// the point the camara looks at (you can use any proper vector, midX/midY/midZ)D3DXVECTOR3 lookAt = D3DXVECTOR3(0.0f, 0.0f, 0.0f);// camera upD3DXVECTOR3 up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);// initial camera position (you can use any proper vector)D3DXVECTOR3 eye0 = D3DXVECTOR3(10.0f, 10.0f, 0.0f);// distance vector of camera to lookAtD3DXVECTOR3 d = eye0 - lookAt;// distance vector of camera to lookAt projected to X-Z-PlaneD3DXVECTOR2 d2 = D3DXVECTOR2(dist.x, dist.z);// distance of camera to lookAt in X-Z-Planefloat dist = D3DXVec2Length(&d2);float length = 2.0f * DX_PI * dist;// In Loop ***********************************************// coordinates of the rotating camerafloat angle = (moveSpeed * elapsedTime) / length;float x = dist * cos(angle);float z = dist * sin(angle);// rotating camera positionD3DXVECTOR eye = D3DXVECTOR(x, eye0.y, z) + lookAt;// create the view MatrixD3DXMatrixLookAtLH(&view, &eye, &lookAt, &up);



Have fun.


[Edit: Bug fixed]
EDIT: Please remember to use 'source' tags in future...

[Edited by - jollyjeffers on March 28, 2007 9:19:38 AM]
sumit381
sumit381
Thank you for the help everyone provided me. To answer your question Evil Steve, it doesn't really matter to me if the camera moves around the grid or the grid rotates in front of the camera. I just need the effect of the camera rotating around the grid. I chose to move the camera around the grid because I tried doing a world transform rotation but I got undesired results when I did that. Thanks again for your help everyone.

Topic Locked

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

Sign in to reply to this topic.