Back to class index
| Sphere::IntersectLineSyntaxint Sphere::IntersectLine(const float4 &linePos, const float4 &lineDir, const float4 &sphereCenter, float sphereRadius, float &t1, float &t2); [74 lines of code]Computes the intersection of a line and a sphere. This function solves the points of intersection between a line and a sphere. A line intersects sphere at 0, 1 or 2 points. When only one point of intersection is reported, the given line is tangent to the sphere. Note The outputted variables t1 and t2 always satisfy t1 < t2. This allows distinguishing between the "enter" and "exit" positions of the line, if the line is interpreted more like a ray starting at linePos, and extending towards lineDir. Parametersconst float4 &linePosThe source position of the line. const float4 &lineDirThe direction of the line. This vector must be normalized in advance. const float4 &sphereCenterThe center position of the sphere to test. floatsphereRadiusThe radius of the sphere, which must be >= 0. float &t1 [out]This parameter receives the parametric distance along the line to the first point of intersection. If the sphere and the line do not intersect, this variable is not written to. To receive the actual world space point corresponding to this point of intersection, compute the vector 'linePos + t1 * lineDir'.float &t2 [out]This parameter receives the parametric distance along the line to the second point of intersection. If the sphere and the line do not intersect, this variable is not written to. If the line is tangent to this sphere (one point of intersection), this variable will be set equal to t1, so that the line segment [t1, t2] always forms a line segment completely enclosed inside the sphere. To receive the actual world space point corresponding to this point of intersection, compute the vector 'linePos + t2 * lineDir'. Return ValueThe number of intersection points: 0, 1 or 2. In case of 0, the line and sphere do not intersect. In case of 1, the line is tangent to the sphere. If the value of 2 is returned, the line properly intersects the sphere. |