Line-Sphere intersection (special requirements, weird case)

kzaurckichz

New member
Joined
Feb 2, 2020
Messages
4
I know about existing formulas to intersect a vector with a sphere, but it is not efficient because I already have some of the information that I am sure can make it much, much simpler

I need to find the distance to the closest point on a sphere, based only on the following information that I already have :

* the distance to the furthest point (second hit point)
* the direction of the line
* the distance from the center of the sphere
* the radius of the sphere
* the normal direction of the second hit

Again, I need to find the closest hit Distance, no need for actual position.

Is it possible ?

I already have a temporary formula that kinda works but is not exactly that, there seem to be an distortion..
What I have is : hit1Distance = hit2Distance + sphereRadius * 2 * dot(lineDirection, hit2Normal)
I do not think it is a good formula, I basically invented it.
 
Last edited:
Can you explain what this point you are calling "second hit point" is. What do you mean that it's the furthest point?
 
Can you explain what this point you are calling "second hit point" is. What do you mean that it's the furthest point?

When you intersect a line with a sphere, you should have two intersection points : hit1 is the closest to the beginning of the line, hit2 is the farthest.

I have the distance of hit2, and I want the distance of hit1.
 
when you say you have the direction of the line that these hits are on do you mean you have a unit vector in that direction?
 
when you say you have the direction of the line that these hits are on do you mean you have a unit vector in that direction?

Yes, I have a vector (x,y,z) of the direction of the line that points towards somewhere on the sphere, and I also have the normal direction vector of the second hit (farthest) on the sphere (pointing outwards from the center of the sphere)
 
Long ago I saw a very short and elegant solution for the nearest approach of two lines in three dimensions, but I don't remember exactly where I saw it or how the formula worked. It took a point on each line and a vector describing each line's direction, then found the distance along each line where the point of nearest approach was located. It involved dot and cross product, but I forget the exact arrangement. The reason I never fully understood it was because it sets the two equations "equal" even though the lines in question might not intersect.

A line segment of zero length could substitute for the sphere in this problem.

Researching it now I am unable to find that particular reference, but something similar did turn up instead:
 
I already have a temporary formula that kinda works but is not exactly that, there seem to be an distortion..
What I have is : hit1Distance = hit2Distance + sphereRadius * 2 * dot(lineDirection, hit2Normal)

Consider the following planar slice through the points C,X,F where...

text851.png
C is the centre of the sphere
X a point on the line where distance=0
F the far intersection point, "hit2"
N the near intersection point, "hit1"

Let "hit2Normal" be a unit vector in the direction CF. Let "lineDirection" be a unit vector in the direction XF. Then cos(t) = dot(lineDirection, hit2Normal). The magnitude of PF is sphereRadius*cos(t). Also NP = PF. Therefore

hit1Distance = hit2Distance - sphereRadius * 2 * dot(lineDirection, hit2Normal)

So I think OP's equation was correct except that the plus ought to be a minus.
 
Consider the following planar slice through the points C,X,F where...

View attachment 16462
C is the centre of the sphere
X a point on the line where distance=0
F the far intersection point, "hit2"
N the near intersection point, "hit1"

Let "hit2Normal" be a unit vector in the direction CF. Let "lineDirection" be a unit vector in the direction XF. Then cos(t) = dot(lineDirection, hit2Normal). The magnitude of PF is sphereRadius*cos(t). Also NP = PF. Therefore

hit1Distance = hit2Distance - sphereRadius * 2 * dot(lineDirection, hit2Normal)

So I think OP's equation was correct except that the plus ought to be a minus.

Okay, yes the + ought to be a minus, I made a mistake in the post but it's al right in my code.
Also, since then I used the standard (complex) formula of line-sphere intersection using the usual parameters that I had to calculate from hit2 distance, and I figured out that the distortion was still there, then I figured that distortion was caused by the view matrix not giving me the correct hit2 distance in the first place...

So you are saying that my original take on this problem was correct ?
It was merely a quick approximation that I thought in the first minute of figuring it out and I didn't give it too much thought.. it just made sense to me that I should at least get the correct values for when the two normal vectors (hit2Normal and lineDirection) would be equal and when it would be perpendicular, but I did not think it would be accurate without distortions for any other point within the sphere...

So if you are sure that my formula was correct, I shall mark this as Solved.
 
So you are saying that my original take on this problem was correct ?

Yes I think your formula is correct. I'm not always right though! More important is that you think your problem is solved (and hopefully you can understand how it works). Feel free to wait until you've sorted out the view matrix, and when you're sure, then you can perhaps mark this solved.
 
Top