# Camera Rotation Around a Point

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

## Recommended Posts

Sorry for posting two new topics so fast. This one isn't a major concern but I'd appreciate some help. I've Googled and looked it up to no avail. Again I'm in D3D8 and VB6. I have rendered a person and pushed my camera back along the +Z at (0,0,15) and had it face (0,0,0), such that I am looking at the top of the person's head; the person himself is facing in the +Y direction, his back to the -Y, etc. The up-vector is purely in the Y direction (0,1,0). I want the user to be able to rotate down to the person's feet using the up and down keys on the keyboard (that is, rotate around the X-axis), then rotate around the person himself (the length of the person is along the Z-axis). In other words, I want my two axes of rotation to be around the X and the Z. Around the X works, around the Z does not. I've looked this up and tried a few people's solutions, but none of them seem to be working. It's probably a math error. Here's the code:
If KeyCode = vbKeyUp Then
icamx = icamx - 5
ElseIf KeyCode = vbKeyDown Then
icamx = icamx + 5
ElseIf KeyCode = vbKeyLeft Then
icamz = icamz - 5
Else
icamz = icamz + 5
End If

CameraX = 15 * Sin(icamz * 3.14 / 180)
CameraY = 15 * Sin(icamx * 3.14 / 180) * Cos(icamz * 3.14 / 180)
CameraZ = 15 * Cos(icamx * 3.14 / 180)

MyEngine.SetCameraPosition CameraX, CameraY, CameraZ

In that, icamx is the angle from the X axis, and icamz is the angle from the Z axis. MyEngine is just a wrapper class I made for the boring stuff; SetEnginePosition just changes the View matrix's position to the X, Y, Z passed to it. That math actually does work for meshes when I move them around, just not the camera. I think it may be related to the view matrix. What should I do? Is combining the world and view matrices a better choice than setting the view matrix like I'm doing, or is there a fix for my trig (a trig fix would be easier because combining the view and world matrices would mean modifying my precious wrapper class I made :D). Thanks for any suggestions! - Steve.

##### Share on other sites
Here's a typical setup I use for setting up viewing...

D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProj;

D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4.0f, 1.0f, 1.0f, 10000.0f);

D3DXVECTOR3 v3Camera = D3DXVECTOR3(0.0f, 0.0f, 15.0f);
D3DXVECTOR3 v3Up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 v3At = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

D3DXMatrixLookAtLH(&matView, &v3Camera, &v3At, &v3Up);
D3DXMatrixIdentity(&matWorld);

m_pDev->SetTransform(D3DTS_WORLD, &matWorld);
m_pDev->SetTransform(D3DTS_VIEW, &matView);
m_pDev->SetTransform(D3DTS_PROJECTION, &matProj);

The camera in this case will always be pointing to the v3At point. The basic calls you're looking for when moving the camera around are:

D3DXVECTOR3 v3Camera = D3DXVECTOR3(0.0f, 0.0f, 15.0f); // To move the camera
D3DXMatrixLookAtLH(&matView, &v3Camera, &v3At, &v3Up); // Set the matrix
m_pDev->SetTransform(D3DTS_VIEW, &matView); // Apply the matrix

##### Share on other sites

I got that far for the most part; that setup is almost identical to mine. I think my question is math/theory related.

Using this code:

If KeyCode = vbKeyUp Then    icamx = icamx - 5ElseIf KeyCode = vbKeyDown Then    icamx = icamx + 5ElseIf KeyCode = vbKeyLeft Then    icamz = icamz - 5Else    icamz = icamz + 5End IfCameraX = 15 * Sin(icamz * 3.14 / 180)CameraY = 15 * Sin(icamx * 3.14 / 180) * Cos(icamz * 3.14 / 180)CameraZ = 15 * Cos(icamx * 3.14 / 180)MyEngine.SetCameraPosition CameraX, CameraY, CameraZ

The camera started at (0,0,15) facing (0,0,0).

Now imagine icamx = -90. This would mean the camera is at (0,15,0) (still facing (0,0,0)).

Now I start to change icamz, but instead it looks like instead of the camera revolving about the Z-axis it starts to go upside-down and to one side, and eventually things get very hairy.

So any suggestions with those maths would be appreciated!

Thanks again for the reply, though, Bill!

- Steve.

##### Share on other sites
All right guys, I figured it out (I think).

The problem was I was putting the camera out along its up-vector axis and telling it to look at the origin, and for some reason that doesn't work. So when it got to that point, I had to change the camera's up-vector to keep everything right-side-up and in order.

But the original problem arose because my model was created in Blender which uses a right-handed coordinate system and D3D uses a left-handed coord system. So I was forcing my camera out along its up-vector axis to compensate for Blender's switching the axes (not knowing). Now all is well though!

- Steve.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633682
• Total Posts
3013314
×