Advertisement

Vectors to find out if an object's in view

Started by June 15, 2001 01:47 PM
3 comments, last by Nazrix 23 years, 7 months ago
I am trying to find out if an object is in view in 3D by finding the angle between the vectors. What I am doing is drawing a vector from the camera to the object and a vector from the camera to a point just in front of the camera with a radius of 1 (so I should only have to normalize the vector that goes toward the object). The problem is that it works only on half of the world, but which half it works on seems to depend upon whether the object I am testing is at a negative z or postitve. So, at the beginning the camera is at 0,0,0. If the object is at 0,0,-20 which means it is going to be right in front of my camera at the start and I move toward and past that object everything works great. But if I go the opposite way as in from the start I turn completely around and head the other way it works the opposite way as in it thinks the angle between the vectors is larger than 90 when it should be less and it thinks that the angle is less than 90 when it should be larger. But it works great on the other side. Then this whole situation is the opposite if the z of the object is positive. I have no idea what I am doing wrong.
  
inline bool Clip(float x, float y, float z)
{
	struct Point cam,normal,normalcam,camvec,obj;

//this is my vector that is right in front of the camera. Gry is the amount that the camera has rotated in radians

	camvec.x=cos(-gry);
	camvec.y=0;
	camvec.z=sin(-gry);
	
//this is the object I want to look at the x,y,z are passed through the function. 

	obj.x=x-Cam.camtx;
	obj.y=y-Cam.camty;
	obj.z=z-Cam.camtz;


//the length of the vector that goes to the object

	float length=sqrt((obj.x*obj.x)+(obj.y*obj.y)+(obj.z*obj.z));

	//normalize it

	normal.x=obj.x/length;
	normal.y=obj.y/length;
	normal.z=obj.z/length;

// then I got the dot product

	float dot=(camvec.x*normal.x)+(camvec.y*normal.y)+(camvec.z*normal.z);
	if (dot>0) return true;
	else return false;
}
  


...A CRPG in development...
Need help? Well, go FAQ yourself.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Just a guess, maybe you dont calculate camvec correctly. Try reversing cos and sin, using +gry and -gry and combinations.

Other than that I dont see anything that could be wrong here.
Advertisement
Yeah I tried using positive gry instead but the only difference is I have to test

if (dot<0) return true;

I just have to use < if it's positive > if it's negative

it works great if the camera is still, and it works on one side but once I cross over to the other side everything's reversed..so odd




...A CRPG in development...

Need help? Well, go FAQ yourself.


Edited by - Nazrix on June 16, 2001 1:47:41 AM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
OK, more data needed.
- the rendering code
- the debug output of the vars used in Clip for correct and incorrect tests.


Also, it might be interesting to test using a very limited field of vision :

return (dot > 0.8);  



Okay thanks to the aid of the one known as pouya, we figured it out. It wasn't a math problem, but the way I was translating the camera position using Nebula (3d engine I am using). Nebula has wierd camera movement and it was messing me up.

plus it's supposed to be:
camvec.x=sin(-gry); camvec.y=0; camvec.z=cos(-gry);
not
camvec.x=cos(-gry); camvec.y=0; camvec.z=sin(gry);


I used the sin and cos wrong and I shouldn't have used a negative inside the () but before the sin / cos instead

I should have known better But we fixed it now!


Thanks for your help though, Diodor




...A CRPG in development...

Need help? Well, go FAQ yourself.


Edited by - Nazrix on June 16, 2001 4:33:31 AM

Edited by - Nazrix on June 16, 2001 4:34:47 AM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi

This topic is closed to new replies.

Advertisement