bool Geometry::DistancePointLine( const Vector3& p3, const Vector3& p1, const Vector3& p2, float32 *distance )
bool withinLine = IntersectionPointLine( p3, p1, p2, &intersection );
if ( distance != NULL )
*distance = ( p3 - intersection ).Magnitude();
bool Geometry::IntersectionPointLine( const Vector3& p3, const Vector3& p1, const Vector3& p2, Vector3 *intersection )
Vector3 diff = p3 - p1;
Vector3 dir = p2 - p1;
float u = Vector3::DotProduct( diff, dir ) / Vector3::DotProduct( dir, dir );
if ( intersection != NULL )
*intersection = p1 + dir * u;
if ( ( u < 0.0f ) || ( u > 1.0f ) )
// closest point does not fall within the line segment
I needed to accurately determine the distance of particles from other scene geometry in order to fade out particles that are too close to things (which gets rid of nasty seams in the particles.) I'd used a method copied from somewhere but I noticed that particles got faded the further you got from them. This felt like a math error.
This week I did some digging around and found not one, but two methods to get the distance (in world coordinates) of a pixel from the camera.