Math 为带广告牌的四边形创建矩阵?

Math 为带广告牌的四边形创建矩阵?,math,matrix,directx,sprite,Math,Matrix,Directx,Sprite,我正试图想出一个合适的算法来创建一个矩阵,让四边形面对摄像机,但我有困难 我正在绘制的四边形面朝下,以下是我迄今为止的代码: 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); D3DXVEC

我正试图想出一个合适的算法来创建一个矩阵,让四边形面对摄像机,但我有困难

我正在绘制的四边形面朝下,以下是我迄今为止的代码:

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;
当要使四边形面朝相机时,这是可行的,但是当从各个角度面对它时,它不会保持直立

在这个屏幕截图中:在左边,四元体被直接查看(向量为0,0,1),在另一边,四元体被从任意角度查看


但是,当从任意角度沿局部z轴倾斜时,这两次都是面向摄影机的。我不知道如何修复它,我想知道下一步是什么?

绕任意轴旋转总是可以做到这一点。首先,应围绕y轴旋转模型,使其指向摄影机(上方向向量),然后围绕与模型右方向向量对齐的正交轴旋转模型

假设z轴从屏幕中出来,下面是一些代码:

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;
这应该放在这行后面:

D3DXVec3Normalize(&quadtocam,&quadtocam);

那么剩下的代码应该可以工作。

Hmm,我不能将您提供的代码放在规范化行下面,因为还没有声明“rotAngle”。我的错误是,rotAngle是firstRotAngle。现在更正了。