#### Archived

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

# Rotating view with direct 3d

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

## Recommended Posts

I working on rotating my view (camera source).I found many example on rotating object but couldnt find any on cameras. Remembering stuff my linear algebra class Here is the code I cam up with. D3DXMATRIX matRotation; D3DXVECTOR3 vecToOrign; D3DXVECTOR3 vecRotatedToOrigin; if (rotateX !=0.0f || rotateY !=0.0f) { D3DXVec3Subtract( &vecToOrign, &vecCameraSource, &vecCameraTarget); //move point back to origin if (rotateX !=0.0f) { D3DXMatrixRotationX(&matRotation, rotateX/RADIAN_TO_DEGREES); //calculate rotation on X D3DXVec3TransformNormal(&vecRotatedToOrigin,&vecToOrign,&matRotation); //apply rotation } if (rotateY !=0.0f) { D3DXMatrixRotationY(&matRotation, rotateX/RADIAN_TO_DEGREES); //calculate rotation on Y D3DXVec3TransformNormal(&vecRotatedToOrigin,&vecToOrign,&matRotation); //apply rotation } D3DXVec3Add( &vecCameraSource, &vecRotatedToOrigin, &vecCameraTarget); //move point back } Is this right or did I sleep too much in class? Is there a better way? Thanks, Michael

##### Share on other sites
There is no difference rotation cameras or object. Should work exactly the same. They are both matrix.

void BX_GRAPHICS_CAMERA::Rotate(char axis, float amount){  D3DXMATRIX tempMatrix;  switch(axis)  {    case ''x'':    case ''X'':      D3DXMatrixRotationX(&tempMatrix, amount);      break;    case ''y'':    case ''Y'':      D3DXMatrixRotationY(&tempMatrix, amount);      break;    case ''z'':    case ''Z'':      D3DXMatrixRotationZ(&tempMatrix, amount);      break;  }  D3DXMatrixMultiply(&p_CameraMatrix, &p_CameraMatrix, &tempMatrix);}

##### Share on other sites
So doing this will keep my camera target the same? I dont want to rotate the world view I want to rotate around my target.

##### Share on other sites
Then you might find D3DXMatrixLookAtLH useful. Use whatever for the camera position and this will ensure that you''re looking at the target.

##### Share on other sites
To rotate around the object, just reverse the last 2 parameters around the MatrixMultiply in the example I showed before (or look below)

void BX_GRAPHICS_CAMERA::Orbit(char axis, float amount){  D3DXMATRIX tempMatrix;  switch(axis)  {    case ''x'':    case ''X'':      D3DXMatrixRotationX(&tempMatrix, amount);      break;    case ''y'':    case ''Y'':      D3DXMatrixRotationY(&tempMatrix, amount);      break;    case ''z'':    case ''Z'':      D3DXMatrixRotationZ(&tempMatrix, amount);      break;  }  D3DXMatrixMultiply(&p_CameraMatrix, &tempMatrix, &p_CameraMatrix);}

You will probably need to set the world matrix to where the object is (or place the object at 0,0,0) to make it work.

Every time I "orbit" is during a character select screen (pick your model) and the model is always 0,0,0... And it works. I''ve not actually tried to orbit another object in another location. The code above might not work for that.

##### Share on other sites
Thats why I was translating the source point back to the origin in my code then rotating it and translating it back.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631782
• Total Posts
3002331
×