• Advertisement
Sign in to follow this  

Billboarding/Rotation Issue

This topic is 2770 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've made a hemisphere of vertices and I'm attempting to get it to rotate so as to always face the camera position. It works for the most part but when the camera position moves into negative X values the rotation seems to go haywire. Negative Y and Z values seem to cause no problems.

It's probably a simple solution but I can't seem to get what the problem is. If anyone could help me I'd greatly appreciate it.

Here's what I'm using to create the rotation matrix. I'm trying to keep the "right" vector on the XZ plane as I only want the hemisphere to pan and tilt, not roll.

origTarget = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
origRight = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
origUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);

D3DXVECTOR3 terrainToCamera = D3DXVECTOR3(GetCamera().position().x - mInfo.PlanetPosition.x, GetCamera().position().y - mInfo.PlanetPosition.y, GetCamera().position().z - mInfo.PlanetPosition.z);

// Project the vector to the XZ plane
D3DXVECTOR3 terrainToCameraXZ = D3DXVECTOR3(terrainToCamera.x, 0.0f, terrainToCamera.z);
D3DXVec3Normalize(&terrainToCameraXZ, &terrainToCameraXZ);
float yRotation = acos( D3DXVec3Dot( &terrainToCameraXZ, &origTarget) );

D3DXMATRIX yRotationMatrix;
D3DXMatrixRotationY(&yRotationMatrix, yRotation);
D3DXVECTOR3 rotatedRight;
D3DXVec3TransformCoord(&rotatedRight, &origRight, &yRotationMatrix);

D3DXVec3Normalize(&rotatedRight, &rotatedRight);
D3DXVec3Normalize(&terrainToCamera, &terrainToCamera);
D3DXVECTOR3 rotatedUp;
D3DXVec3Cross(&rotatedUp, &rotatedRight, &terrainToCamera);
D3DXVec3Normalize(&rotatedUp, &rotatedUp);

D3DXMATRIX terrainWorld = D3DXMATRIX(rotatedRight.x, rotatedRight.y, rotatedRight.z, 0.0f,
rotatedUp.x, rotatedUp.y, rotatedUp.z, 0.0f,
terrainToCamera.x, terrainToCamera.y, terrainToCamera.z, 0.0f,
mInfo.PlanetPosition.x, mInfo.PlanetPosition.y, mInfo.PlanetPosition.z, 1.0f);

[Edited by - FatPope on August 23, 2010 1:40:35 PM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement