在3d空间中的两个向量之间的角度

时间:2013-05-17 10:37:52

标签: c++ vector geometry directx

我想在游戏中做一些鱼眼剔除。因此,对于列出的每个对象,我想检查是否在相机的截头。 我是这样做的:

D3DXVECTOR3 cameraPos;
D3DXVECTOR3 pos;
D3DXVECTOR3 cameraVector;//where camera is looking( camera->eye() - camera->pos() )
D3DXVECTOR3 direction = pos - cameraPos;
normalize( &direction );
normalize( &cameraVector );
float dot = cameraVector.x * direction.x + cameraVector.y * direction.y + cameraVector.z * direction.z;

//float cosvalue = cos( dot ); // i was calculatin cos of cos :)
float cosvalue = dot;
float angle = acos (cosvalue) * 180.0f / PI;

if( angle < 45.0f ) draw();

但我得到了奇怪的结果。例如(角度<50.0f)到处都是,但没有我想要的地方,所以鱼眼是空的。 !(角度<50.0f)绘制我想要的东西。但是(角度<40)什么都没有:( 如果这是我的角度计算或它的浮动问题我不是舒尔:(任何人?

1 个答案:

答案 0 :(得分:5)

dot_product = a.x * b.x + a.y * b.y + a.z * b.z = a.len() * b.len * cos(angle)

因此:

cos(angle) = dot_product / (a.len * b.len) 

你的代码做了一件奇怪的事情:你实际上是在计算点积的余弦!