View Full Version : 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!

satan

05-21-2002, 11:38 PM

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

and i think i know something that might help you

it is called google http://www.opengl.org/discussion_boards/ubb/smile.gif

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

hoshi55

05-22-2002, 03:19 AM

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(x*x+y*y+z*z)<m_fValue[3])

// some code for the case of in the sphere

JeeTriX

05-22-2002, 06:31 AM

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 http://www.opengl.org/discussion_boards/ubb/smile.gif

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?

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.