# How to Make the Screen NOT Pass through the Object

I wrote a programme with a big scene,in which
there are a lot of spheres and cubes.Now if a sphere is far in front of my screen,then i control the screen to step towards the sphere,if the distance between the screen and the sphere is NOT bigger than the radius of the sphere,the screen can not go forward!
I need this feature in my programme,but do not known any about the subject,can you give me some code or advice?thank you very much!

you may want to take a look here:
http://www.gametutorials.com/Tutorials/OpenGL/OpenGL_Pg3.htm

ray-sphere collision detection is what you are looking for

btw it can be found at http://www.google.com

i think what you need is even simpler, seems like you only want to know the distance to the sphere. calc the dist from your point of view to the sphere, subtract its radius and you’ve got the distance to your sphere hull.

Thank you very much!I have done what did you said but now encounter a new problem:my screen(my programme runs as full-screen) seems go into the sphere in some degree even when the distance is bigger than the radius!
My code is just as below:
// m_fValue store the coordinate and radius of the sphere,m_fCameraPos store the coordinate of the camera
// use x,y,z to math the distance
float x=m_fValue[0]+m_fCameraPos[0];
float y=m_fValue[1]+m_fCameraPos[1];
float z=m_fValue[2]+m_fCameraPos[2];
if(sqrt(xx+yy+z*z)<m_fValue[3])
// some code for the case of in the sphere

Let me state clearly your problem. You don’t want the viewport to interfere your sphere wich has a known radius, your viewport stops right now, but a bit too late…

Maybe it is a clipping issue. I presume you did set a gluPerspective or glFrustrum, well, over there you have to specify the distance of your near and far clipping plane.
If your near clipping plane is close enough it can clip (= make dissappear) a viewable part of your scene, really close to where you’re viewing from.

gluPerspective(45,w/h, ---->1.0 , 100);

the 1.0 is the near clipping plane value, try adding this value to your radius so you can’t get closer to the sphere than it’s radius + this clipping distance

Thanks!Now i have a new idea:Can we make collision detection between front-clipping plane£¨NOT the position of camera£©and the sphere?if the plane has a intersection with the sphere,we believe that the “screen” is in the sphere?