计算 3D 对象与点之间的角度
Calculating the Angle Between a 3D Object and a Point
我在 DirectX11 中有一个 3D 对象,它有一个位置矢量和一个面向方向的旋转角度(它只绕 Y 轴旋转)。
D3DXVECTOR3 m_position;
float m_angle;
如果我想旋转对象以面对某些东西,那么我需要使用两个归一化向量上的点积找到它面向的方向和它需要面对的方向之间的角度。
我遇到的问题是我如何找到物体当前面向的方向,只是它的位置和角度。我目前拥有的是:
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的帮助下):
// 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;
如果将
方向存储为角度,则必须在角度为 0 时采用某种默认方向,将该默认方向表示为矢量(如 (1.0, 0.0, 0.0),如果您假设 x+ 方向为默认值),然后按角度度旋转该矢量(直接使用 sin/cos 或使用由 D3DXMatrixRotation* 函数之一创建的旋转矩阵),生成的矢量将是当前方向。
编辑:
在回答您的评论时,您可以像这样确定旋转方向。在您的情况下,这转化为类似(未经测试)的内容:
D3DXVECTOR3 cross;
D3DXVec3Cross(&cross, &normDirection, &normTarget);
float dot;
D3DXVec3Dot(&dot, &cross, &normal);
if (dot < 0) {
// angle is negative
} else {
// angle is positive
}
其中normal
很可能是 (0, 1, 0) 向量(因为您说对象绕 Y 轴旋转)。
如果,如你所说,0 度 == (0,0,1),你可以使用:
范数方向 = ( sin( 角度 ), 0, cos( 角度 ) )
因为旋转总是围绕 Y 轴。
您将不得不根据您的系统(左撇子或右撇子)更改 sin(角度)的符号。
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 无法将结构注册为增强几何体3D点
- 在cuda线程之间共享大量常量数据
- 计算每个维度的两个 3D 矢量之间的角度
- C++ 和 Lua 函数之间的交互与 3D 矢量参数
- 在 3D 骨架系统 DirectX 之间转换
- c++ 从大型数组中读取 3D 坐标并计算它们之间的距离
- C 计算两个3D向量之间的角度(0至360)
- 检查 3D 点是否位于给定的 3D 线上(在两个 3D 点之间)
- C++ 如何计算两个 3D 点之间的弧
- 如何检测两个3D点云之间的差异
- opencv:两个 3D 点云之间的刚性变换
- 计算两个 3D 矢量之间的角度 - 如何实现
- 计算 3D 对象与点之间的角度
- c++在3d数组之间传递双值
- 如何在3D模型格式之间进行转换?(Collada *.到ACIS *.例如Sat)
- 如何获得两个3D矢量之间的角度(俯仰/偏航)为自动瞄准
- 使用pThreads在线程之间共享3D数组
- 求 2 个 3D 点之间的距离