C++ 计算三维对象与点之间的角度

C++ 计算三维对象与点之间的角度,c++,vector,3d,rotation,dot-product,C++,Vector,3d,Rotation,Dot Product,我在DirectX11中有一个3D对象,它有一个位置向量和它所面对的方向的旋转角度(它只绕Y轴旋转) 如果我想旋转物体来面对某个物体,那么我需要使用两个标准化向量上的点积来找到它面对的方向和它需要面对的方向之间的角度 我遇到的问题是,我如何找到物体当前所面对的方向,只是它的位置和角度。我目前拥有的是: D3DXVECTOR3 normDirection, normTarget; D3DXVec3Normalize( &normDirection, ????); D3DXVec3Norma

我在DirectX11中有一个3D对象,它有一个位置向量和它所面对的方向的旋转角度(它只绕Y轴旋转)

如果我想旋转物体来面对某个物体,那么我需要使用两个标准化向量上的点积来找到它面对的方向和它需要面对的方向之间的角度

我遇到的问题是,我如何找到物体当前所面对的方向,只是它的位置和角度。我目前拥有的是:

D3DXVECTOR3 normDirection, normTarget;
D3DXVec3Normalize( &normDirection, ????);
D3DXVec3Normalize( &normTarget, &(m_position-target));

// I store the values in degrees because I prefer it
float angleToRotate = D3DXToDegree( acos( D3DXVec3Dot( &normDirection, &normTarget)));
有人知道如何从我的值中得到当前方向的向量吗,或者我需要重新写入它以便跟踪对象的方向向量吗

编辑:将“cos”更改为“acos”

解决方案(在用户2802841的帮助下):

//假设这些是成员变量
D3DX矢量器3 m_位置;
d3dx矢量3 m_旋转;
d3dx矢量3 m_方向;
//这些是局部变量
D3DXVECTOR3目标;//作为参数传入
D3DXVECTOR3目标规范;
D3DXVECTOR3上向量;
漂浮角旋转;
//查找要旋转的数量
D3DXVEC3规范化(&targetNorm,&target-m_位置);
角度旋转=D3DXToDegree(acos(D3DXVec3Dot(&targetNorm,&m_方向));
//计算两个向量之间的上方向向量
D3DXVec3Cross(&upVector,&m_方向,&targetNorm);
//如果上方向向量向下,则将角度切换为负值
if(上方向向量y<0)
角旋转*=-1;
//将旋转添加到对象的整体旋转中
m_旋转。y+=角度旋转;

如果将方向存储为角度,则当角度为0时,必须采用某种默认方向,将该默认方向表示为向量(如(1.0,0.0,0.0),如果假设x+方向为默认方向,则将该向量旋转角度(或使用函数创建的旋转矩阵)结果向量就是你当前的方向

编辑:

在回答您的评论时,您可以确定旋转方向。在您的情况下,这会转化为(未经测试):

D3DXVECTOR3交叉;
D3DXVec3Cross(&cross,&normDirection,&normTarget);
浮点;
D3DXVec3Dot(&dot,&cross,&normal);
if(点<0){
//角度为负
}否则{
//角度为正
}

其中,
normal
最有可能是一个(0,1,0)向量(因为您说过对象绕Y轴旋转)。

如果,如您所说,0度==(0,0,1),您可以使用:

法线方向=(sin(角度),0,cos(角度))

因为旋转总是围绕y轴


您必须根据您的系统(左手或右手)更改sin(角度)的符号。

如何通过旋转矩阵精确旋转向量?据我所知,您无法执行D3DXVECTOR3=D3DXVECTOR3*D3DXMATRIX。是的,我现在意识到,至少在我的程序中,0度==(0,0,1)。@Edward Try谢谢,这正是我需要的。然而,由此产生了另一个问题。如果我将位置设置为(5,3,0),目标位置设置为(7.5,3,2.5),则正确计算出的角度为45度,但当我将目标设置为(7.5,3,-2.5)时,角度仍然为45度。有没有办法确定旋转的方向?@Edward I编辑了答案,其中的信息应该对你有所帮助。确实如此。我已经测试过了,确信交叉积正是我所需要的。(更新了我的问题以包含完整的解决方案)。
D3DXVECTOR3 normDirection, normTarget;
D3DXVec3Normalize( &normDirection, ????);
D3DXVec3Normalize( &normTarget, &(m_position-target));

// I store the values in degrees because I prefer it
float angleToRotate = D3DXToDegree( acos( D3DXVec3Dot( &normDirection, &normTarget)));
// assuming these are member variables
D3DXVECTOR3 m_position;
D3DXVECTOR3 m_rotation;
D3DXVECTOR3 m_direction;

// and these are local variables
D3DXVECTOR3 target;  // passed in as a parameter
D3DXVECTOR3 targetNorm;
D3DXVECTOR3 upVector;
float angleToRotate;

// find the amount to rotate by
D3DXVec3Normalize( &targetNorm, &(target-m_position));
angleToRotate = D3DXToDegree( acos( D3DXVec3Dot( &targetNorm, &m_direction)));

// calculate the up vector between the two vectors
D3DXVec3Cross( &upVector, &m_direction, &targetNorm);

// switch the angle to negative if the up vector is going down
if( upVector.y < 0)
    angleToRotate *= -1;

// add the rotation to the object's overall rotation
m_rotation.y += angleToRotate;
D3DXVECTOR3 cross;
D3DXVec3Cross(&cross, &normDirection, &normTarget);
float dot;
D3DXVec3Dot(&dot, &cross, &normal);
if (dot < 0) {
    // angle is negative
} else {
    // angle is positive
}