# Calculating plane on sphere

This topic is 4121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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

##### Share on other sites
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 camera

Thanks for your kind help! Works like a charm now :)

But one thing:
Shouldn't the line
a = dot(v, d) * v / |v|²

be written as
a = 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

##### Share on other sites
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.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
Rutin
10

• 18
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632140
• Total Posts
3004355

×