J_M_S 122 Report post Posted May 5, 2007 Hi everybody, I want to calculate the near plane of my camera, so that it does'nt cut a sphere in the scene that is inside the view frustum. EDIT: The plane should be as near as possible to the sphere, without penetrating it. Given are: - camera position - the three view vectors (right, up, view) - sphere position - sphere radius I tried to calculate the closest point on the sphere, relative to the camera position and then calculating the closest point on the line of the view vector. The results not satisfying, because when the camera doesn't look straight at the sphere, the plane intersects the sphere. I doubt my poor math skills are enough to solve this problem, so any help would be greatly appreciated. 0 Share this post Link to post Share on other sites
nmi 978 Report post Posted May 5, 2007 Your near plane is perpendicular to the view vector (the direction where your camera is looking to). To keep the sphere inside the view frustum, the distance of the center of the sphere to the near plane must be bigger than the radius of the sphere. The dot product can be used to project one vector onto another vector. Here you need to project the vector that points from the camera to the sphere onto the view vector:C = location of cameraP = location of sphered = P - C = camera to sphere vectorv = view vectora = dot(v, d) * v / |v|²b = a * (|a| - r) / |a|, where r is the radius of the sphere|b| is the distance of the near plane to the camera 0 Share this post Link to post Share on other sites
J_M_S 122 Report post Posted May 5, 2007 Quote:Original post by nmiC = location of cameraP = location of sphered = P - C = camera to sphere vectorv = view vectora = dot(v, d) * v / |v|²b = a * (|a| - r) / |a|, where r is the radius of the sphere|b| is the distance of the near plane to the cameraThanks for your kind help! Works like a charm now :)But one thing:Shouldn't the linea = dot(v, d) * v / |v|²be written asa = dot(v, d) * va = a / |a|?Otherwise the plane still cuts into the sphere at almost perpendicular angles of the view vector relative to the camera to sphere vector.EDIT: Typo 0 Share this post Link to post Share on other sites
Zipster 2391 Report post Posted May 5, 2007 No, the original code is correct. However, if you only want the distance then you don't have to calculate any vectors at all. The near-plane distance is just dot(v,d)/|v| - r, which can be even further simplified if v is unit length already.I should add that you'll want to clamp that value so that it's greater than 0, but not too small. 0 Share this post Link to post Share on other sites