Archived

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

Rotating view with direct 3d

This topic is 5433 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 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites