   Collision: closest distance between two lines?
  I'm looking for a way to determine the closest distance reached by two trajectory vectors each having a start point in 3d space and an endpoint.  In other words, what is the closest distance the two lines ever come to eachother?Thanks for any direction, help,Devin   
  This is similar to the 'closest pair of points on two lines problem', except that the parameter (time) is the same for both lines. So let's see:f1(t) = p1+t*v1f2(t) = p2+t*v2d(t) = |f1(t)-f2(t)|d2(t) =(f1(t)-f2(t))^2 =((p1+t*v1)-(p2+t*v2))^2 =(p1+t*v1-p2-t*v2)^2 =(p1-p2+t*v1-t*v2)^2 =(m+tw)^2 = [ m = p1-p2, w = v1-v2 ]w^2t^2+2mwt+m^2d2'(t) = 2w^2t+2mw2w^2t+2mw = 02w^2t = -2mwt = -2mw/2w^2 = -mw/w^2In code:vector3 m = p1 - p2;vector3 w = v1 - v2;float t = -dot(m,w)/dot(w,w);float closest_dist = length((p1+t*v1)-(p2+t*v2));I probably screwed something up somewhere in there, but the solution should be something along those lines.[Edit: Just to be clear, this is the solution for the closest distance between the two objects; the solution for the closest distance between their paths is slightly different.]   ;  Try Here....  Softsurfers - Distance between Lines;Quote:Original post by daftasbrushTry Here....  Softsurfers - Distance between LinesIf it's the closest distance between two moving objects that the OP is looking for, then the above link is not the correct solution.   ;Quote:Original post by jykIf it's the closest distance between two moving objects that the OP is looking forOriginal post by devroniousIn other words, what is the closest distance the two lines ever come to eachother? I read this as "independent of time".... maybe OP can clarify.;Quote:Original post by daftasbrushI read this as "independent of time".... maybe OP can clarify.You're right, it's not clear from the original post. @The OP: If it is independent of time, the link daftasbrush posted is what you're looking for. Otherwise, try what I posted above.   ;  Thanks for your help.  Time is a consideration as it will effect the position of each object thru it's velocity.  I should probably say that velocity is a consideration as time is a component of that.  Any two trajectories are subject to each's velcoity and coincidently their nearest distance acheived along their trajectories.But for clarification, time will be equal for both, 1 unit.  But velocities will variy and subsequently, their position delta along that 1 unit of time.   ;  Then jyk mostly answered it.To embellish:float dist(  vector3 const& p1, vector3 const& v1,  vector3 const& p2, vector3 const& v2,   float time){  return length((p1+t*v1)-(p2+t*v2));}float bound( float Min, float value, float Max ) {  return min( Max, max(value, Min) );}// ...vector3 m = p1 - p2;vector3 w = v1 - v2;float t0 = -dot(m,w)/dot(w,w);t0 = bound(0., t0, 1.);float t1 = 0;float t2 = 1;float closest_dist0 = dist( p1, v1, p2, v2, t0 );float closest_dist1 = dist( p1, v1, p2, v2, t1 );float closest_dist2 = dist( p1, v1, p2, v2, t2 );float closest_dist = min( closest_dist0, min( closest_dist1, closest_dist2 ) );The closest dist happens either when the derivative of distance is 0, or at one of the endpoints of the problem.Care must be taken to avoid the location where the derivative is 0 to be kept within the bounds of the problem.;  Cool, thanks for your help.  This will help me to understand more about how time effects the relation between two objects having velocity in space.  I couldn't find anything on the web about that.  But anyways, I should be able to calc the moment of impact, if any, and then go with that.-Devin   ;  jyk,Quote:[Edit: Just to be clear, this is the solution for the closest distance between the two objects; the solution for the closest distance between their paths is slightly different.]I'm not sure, but I think you're refering to the closest distance between two lines in space, removing consideration for position and velocity, correct?  (simply the distance between two 3 dimensional lines)?  Cause that would be useful to me as well.Thanks,Devin