# rotating the camera around the origin

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone. I'm trying to build do a simple rotation on a camera that I'm keeping track of using a lookat, eye and up vector point. I want to rotate the camera around the origin on certain mouse inputs so that it will spin horizontally around it when the mouse moves left/right, and vertically around it when the mouse moves up/down. Every time I mess with this, I get a little closer, but its not working, and I think it has everything to do with the axis I'm rotating around. I've thought about it for two days, but my brain isn't coming up with the answer. For reference, m_pCameraLoc et al are pointers to either 4D or 3D vectors storing the vectors needed by the D3DXMatrixLookAtLH function. Here's a small example, moving down (all the camera movement code is nearly identical to this):
// if mouse is in the 1/8th border top or bottom, rotate the camera a little.
if (mousey &lt; (int)(.125 * (float)m_d3dpp.BackBufferHeight)) {
// rotate down about X axis
D3DXMatrixRotationX(&matRot, 0.0124f);
//D3DXMatrixMultiply(&matWorld, &matWorld, &matRot);
D3DXVec3Transform(m_pCameraLoc, m_pCameraLoc3D, &matRot);
D3DXVec3Transform(m_pUpVector,  m_pUpVector3D,  &matRot);

m_pCameraLoc3D-&gt;x = m_pCameraLoc-&gt;x;
m_pCameraLoc3D-&gt;y = m_pCameraLoc-&gt;y;
m_pCameraLoc3D-&gt;z = m_pCameraLoc-&gt;z;

m_pUpVector3D-&gt;x = m_pUpVector-&gt;x;
m_pUpVector3D-&gt;y = m_pUpVector-&gt;y;
m_pUpVector3D-&gt;z = m_pUpVector-&gt;z;

D3DXMatrixLookAtLH(&matView, m_pCameraLoc3D, m_pLookatPt, m_pUpVector3D);

}

If anyone can point me in the right direction, I would really appreciate it. [Edited by - stromdotcom on November 29, 2005 8:01:29 PM]

##### Share on other sites
Here is an awesome example of a camera system using the components you are talking about.
ToyMakers camera tutorial
Apron's Camera tutorials

I hope this helps.
Take care.

##### Share on other sites
thanks for the links. it seems that i should not be rotating about the x and y axis, but about the up vector and right vector (i currently do not keep track of a right vector) using D3DXMatrixRotationAxis.

##### Share on other sites
Home now, and it turns out that what I said earlier was exactly the solution. I'm going to post this here in case someone searches for it in the future. What I was trying to do wasn't quite a 3rd person camera, but rather a camera which moved along a sphere in an intuitive way.

The answer was to keep an Up, Right and Position vector. Because some functions require a D3DXVECTOR3 and some a D3DXVECTOR4, I kept one of each for each vector (except the lookat point, since it is the origin and never changes) and made sure to keep them updated:

// Fix the initial camera locationm_pCameraLoc		= new D3DXVECTOR4(0.0f, 0.0f, -48.0f, 1.0f);m_pCameraLoc3D		= new D3DXVECTOR3(0.0f, 0.0f, -48.0f);m_pLookatPt		= new D3DXVECTOR3(0.0f, 0.0f, 0.0f);m_pUpVector		= new D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f);m_pUpVector3D		= new D3DXVECTOR3(0.0f, 1.0f, 0.0f);m_pRightVector		= new D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);m_pRightVector3D	= new D3DXVECTOR3(1.0f, 0.0f, 0.0f);

Then the rotation of the camera is about the right vector when moving up or down, and the up vector when moving left or right. When rotating about the up vector, the right vector gets rotated as well as the camera location vector, and similarly for rotating about the right vector, rotate the up vector along with the camera vector.

Code for moving left:

if (mousex < (int)(.125 * (float)m_d3dpp.BackBufferWidth)) {	// rotate left about Y axis	D3DXMatrixRotationAxis(&matRot, m_pUpVector3D, 0.03f);	D3DXVec3Transform(m_pCameraLoc,    m_pCameraLoc3D,    &matRot);	D3DXVec3Transform(m_pRightVector,  m_pRightVector3D,  &matRot);	m_pCameraLoc3D->x = m_pCameraLoc->x;	m_pCameraLoc3D->y = m_pCameraLoc->y;	m_pCameraLoc3D->z = m_pCameraLoc->z;	m_pRightVector3D->x = m_pRightVector->x;	m_pRightVector3D->y = m_pRightVector->y;	m_pRightVector3D->z = m_pRightVector->z;	D3DXMatrixLookAtLH(&matView, m_pCameraLoc3D, m_pLookatPt, m_pUpVector3D);}

Thanks again for the help, Armadon!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 28
• 15
• 39
• ### Forum Statistics

• Total Topics
634835
• Total Posts
3019539
×