# camera questions

## Recommended Posts

Hello there, its me and my camera again..
D3DXMatrixLookAtLH(&ViewMatrix,
&D3DXVECTOR3(vCamPos.x, vCamPos.y, vCamPos.z),// the camera position
&D3DXVECTOR3 (vLookAt.x, vLookAt.y, vLookAt.z),	// the look-at position
&D3DXVECTOR3 (0.0f, 1.0f,0.0f));	// the up direction

D3DXMatrixTranslation(&matTranslateX, input.streckeY, 0.0f, 0.0f);
mview = matRotateY * matTranslateX * ViewMatrix;
pd3ddev->SetTransform(D3DTS_VIEW, &mview);


So thats my code. My problem is, that the camera rotates around the origin after I translated the camera. Yes, I know, I have to translate the camera back to the origin, rotate then and afterwards I can translate back to the position where I was before. So I could rotate the camere around the point it is. So far the theory but I can't handle it :( Can someone help me please?

##### Share on other sites
Show the code that you use to change the translation in response to user input.

##### Share on other sites
Thanks for answering so fast. Heres the code:

//input.cpp//...case DIMOFS_BUTTON1:	if(od[i].dwData & 0x80)	{	  mbrightdown = true;	  rightMouseFirstPress = true;	}	else	  mbrightdown = false;	break;case DIMOFS_BUTTON2:	if(od[i].dwData & 0x80)	{	  mbmiddledown = true;	  middleMouseFirstPress = true;	}	else	  mbmiddledown = false;	break;//...if(mbrightdown && rightMouseFirstPress){	  helpY = static_cast<float>(CMainApp.getMouseCoordsInputX());  rightMouseFirstPress = false;}if(mbrightdown){  merkerY = (static_cast<float>(CMainApp.getMouseCoordsInputX())) - helpY;  if(merkerY != 0)  {				    if(merkerY > 150)//helpY)      streckeY += 80.0f * CMainApp.getTime();    if(merkerY > 100 && merkerY < 150)      streckeY += 60.0f * CMainApp.getTime();    if(merkerY > 50 && merkerY < 100)      streckeY += 40.0f * CMainApp.getTime();    if(merkerY > 20 && merkerY < 50)      streckeY += 20.0f * CMainApp.getTime();    if(merkerY > 10 && merkerY < 20)      streckeY += 10.0f * CMainApp.getTime();      if(merkerY < -150)      streckeY -= 80.0f * CMainApp.getTime();    if(merkerY < -100 && merkerY > -150)      streckeY -= 60.0f * CMainApp.getTime();    if(merkerY < -50 && merkerY > -100)      streckeY -= 40.0f * CMainApp.getTime();    if(merkerY < -20 && merkerY > -50)      streckeY -= 20.0f * CMainApp.getTime();    if(merkerY < -10 && merkerY > -20)      streckeY -= 10.0f * CMainApp.getTime();  }}if(mbmiddledown && middleMouseFirstPress){	  helpX = static_cast<float>(CMainApp.getMouseCoordsInputX());  middleMouseFirstPress = false;}if(mbmiddledown){  merkerX = (static_cast<float>(CMainApp.getMouseCoordsInputX())) - helpX;  if(merkerX != 0)  {				    streckeX += merkerX * CMainApp.getTime() * 0.5f;//ROTATEMOUSESPEED;    if(streckeX > 360.0f)      streckeX = 0.0f;    if(streckeX < 0.0f)      streckeX = 360.0f;  }}//...

##### Share on other sites
I'm a little confused by your math there. What is the look-at vector looking at? What is the difference between vCamPos.y and streckeY? Under normal circumstances, you'd use either a translation and rotation OR a look-at matrix, never both.

##### Share on other sites
If you're going to use regular translations and rotations to make your view matrix, you have to work with the inverse of the view matrix (AKA, the camera's world matrix). Usually I just store a world matrix for my camera and work with that, then invert it whenever I need a view matrix.

worldMatrix._41 += input.streckeY;  // ._41, ._42, ._43 == the X, Y, and Z position of the cameraD3DXMatrixRotationY(&matRotateY, D3DXToRadian(input.streckeX));worldMatrix  = matRotateY * worldMatrix;D3DXMatrixInverse(&mview, NULL, &worldMatrix);pd3ddev->SetTransform(D3DTS_VIEW, &mview);

##### Share on other sites
First I show you the start lookAt and vCamPos vectors:

vCamPos.x = 0.0f;
vCamPos.y = 10.0f;
vCamPos.z = -10.0f;
vLookAt.x = 0.0f;
vLookAt.y = 0.0f;
vLookAt.z = 0.0f;

@ Sneftel:
I use vCamPos to "zoom" in and out scrolling down/up (vCamPos.y+=1 or vCamPos.y-=1).
"streckeY" is the range I want to translate the camera along the x-axis.
"streckeX" is the value I want to rotate the camera around itself.

@ MJP:
I tried what you posted and saw that I already had this effect while playing around. The effect is that the camera rotates now around itself but translates only around the origin x-axis and not around the new one after it has been rotated.

If someone has an idea how I can manage this I would even do without the "D3DXMatrixLookAtLH" function...

Ok, one more question: how do I translate to the origin, rotate then and translate back to the first position? I read this so often but don't really know how to do it...

Thanks very much for the replies so far!

##### Share on other sites
// Rotate the camera's matrixD3DXMatrixRotationY(&matRotateY, D3DXToRadian(input.streckeX));worldMatrix  = matRotateY * worldMatrix;// Create a translation vector and rotate it, so that the translation// is relative to the camera's current orientationD3DXVECTOR3 translation (input.streckeY, 0, 0);D3DXVec3TransformNormal(&translation, &translation, &worldMatrix);worldMatrix._41 += translation.x;worldMatrix._42 += translation.y;worldMatrix._43 += translation.z;// Invert to make the view matrixD3DXMatrixInverse(&mview, NULL, &worldMatrix);pd3ddev->SetTransform(D3DTS_VIEW, &mview);

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627651
• Total Posts
2978397

• 10
• 12
• 22
• 13
• 33