Sign in to follow this  
J_M_S

Calculating plane on sphere

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 this post


Link to post
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 camera
P = location of sphere
d = P - C = camera to sphere vector
v = view vector
a = 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 this post


Link to post
Share on other sites
Quote:
Original post by nmi

C = location of camera
P = location of sphere
d = P - C = camera to sphere vector
v = view vector
a = 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) * v
a = 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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this