简体   繁体   中英

Angle between two vectors in 3d space

I want to do kind of fish-eye culling in my game. So for each object listed to draw I wanna check if is in frustrum of camera. I do it this way:

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();

But I get weird results. For example ( angle < 50.0f) draws everywhere but no where I want so fish eye is empty. !(angle < 50.0f) draws what i want. But (angle < 40) draws nothing :( I am not shure if it's my angle calculation or it's floats problem :( Anyone?

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

thus:

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

Your code does a strange thing: you're actually calculating the cosine of the dot product instead!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM