views:

49

answers:

1

I'm trying to come up with a proper algorithm to create a matrix which gets the quad to face the camer straight up but I'm having difficulty.

The quad I'm drawing is facing downwards, here's the code I have so far:

D3DXVECTOR3 pos;
pos = D3DXVECTOR3(-2.0f, 6.0f, 0.1f);
D3DXMATRIX transform;
D3DXMatrixTranslation(&transform, pos.x, pos.y, pos.z);

D3DXVECTOR3 axis = D3DXVECTOR3(0, -1, 0);
D3DXVECTOR3 quadtocam = pos - EmCameraManager::Get().GetPosition();

D3DXVec3Normalize(&quadtocam,&quadtocam);

D3DXVECTOR3 ortho;
D3DXVec3Cross(&ortho, &axis, &quadtocam);

float rotAngle = acos(D3DXVec3Dot(&axis,&quadtocam));
D3DXMATRIX rot;
D3DXMatrixRotationAxis(&rot,&ortho,rotAngle);

transform = rot*transform;

This works when it comes to making the quad face the camera however it doesn't stay up right when facing it from all angles.

In this screen shot: http://imgur.com/hFmzc.png On the left the quad is being viewed straight on (vector is 0,0,1), in the other its being viewed from an arbitrary angle.

Both times it's facing the camera, however when from an arbitrary angle its tilting along its local z axis. I'm not sure how to fix it and was wondering what the next step here would be?

+1  A: 

Rotating around an arbitrary axis will always make that. You should rotate your model around y axis first to make it point(up vector) to your camera and after rotate it around your ortho axis which would be aligned with the model's right vector.

Assuming that z axis is coming out of the screen here's some code:

D3DXVECTOR3 Zaxis = D3DXVECTOR3(0, 1, 0);
D3DXVECTOR3 flattenedQuadtocam = quadtocam;
flattenedQuadtocam.y = 0;
float firstRotAngle = acos(D3DXVec3Dot(&Zaxis,&flattenedQuadtocam));

D3DXMATRIX firstRot;
D3DXMatrixRotationAxis(&firstRot,&Zaxis,firstRotAngle);

transform = firstRot*transform;

This should be put right after this line:

D3DXVec3Normalize(&quadtocam,&quadtocam);

Then the rest of your code should work.

Eric Fortin
Hmm, I can't put the code you have provided underneath the normalize line because 'rotAngle' has not been declared yet.
meds
My mistake, rotAngle was to be firstRotAngle. It's corrected now.
Eric Fortin
Did the solution work ?
Eric Fortin