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

Mac*_*zny 0 c++ directx geometry vector

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

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();
Run Code Online (Sandbox Code Playgroud)

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

And*_*rew 5

dot_product = a.x * b.x + a.y * b.y + a.z * b.z = a.len() * b.len * cos(angle)
Run Code Online (Sandbox Code Playgroud)

从而:

cos(angle) = dot_product / (a.len * b.len) 
Run Code Online (Sandbox Code Playgroud)

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