SA-MP Forums Archive
HOWTO/Tutorial: find if a point is near a line in 3D - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: HOWTO/Tutorial: find if a point is near a line in 3D (/showthread.php?tid=168053)



HOWTO/Tutorial: find if a point is near a line in 3D - JernejL - 14.08.2010

Sometimes you need laser precision detection when a point (player or a turtle) is near a arbitrary line (example line: near a train, or ON a straight long diagonal road)

Following Paul Bourke's excellent advanced math tutorial here, i wrote a pawn version of the example code from his website:
http://local.wasp.uwa.edu.au/~pbourk...try/pointline/

Code:
// thanks pbourke: http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/

stock Float:Magnitude( Float:Point1[3], Float:Point2[3] ) {
    new Float:Vector[3];
    
    Vector[0] = Point2[0] - Point1[0];
    Vector[1] = Point2[1] - Point1[1];
    Vector[2] = Point2[2] - Point1[2];
    
    return floatsqroot( Vector[0] * Vector[0] + Vector[1] * Vector[1] + Vector[2] * Vector[2] );
}

stock Float:DistancePointLine( Float:Point[3], Float:LineStart[3], Float:LineEnd[3]) {
    new Float:LineMag;
    new Float:U;
    new Float:Intersection[3];
    
    LineMag = Magnitude( LineEnd, LineStart );
    LineMag *= LineMag;
    
    if (LineMag < 0.001) // prevent div by zero below, in pawn this equals to "the shit hits the fan" or "all hell breaks loose"
    	return 10000000.0;
    
    U = ( ( ( Point[0] - LineStart[0] ) * ( LineEnd[0] - LineStart[0] ) ) +
        (   ( Point[1] - LineStart[1] ) * ( LineEnd[1] - LineStart[1] ) ) +
        (   ( Point[2] - LineStart[2] ) * ( LineEnd[2] - LineStart[2] ) ) ) / LineMag;
    
    if ( U < 0.0 || U > 1.0 )
        return 10000000.0;   // closest point does not fall within the line segment
    
    Intersection[0] = LineStart[0] + U * ( LineEnd[0] - LineStart[0] );
    Intersection[1] = LineStart[1] + U * ( LineEnd[1] - LineStart[1] );
    Intersection[2] = LineStart[2] + U * ( LineEnd[2] - LineStart[2] );
    
    return Magnitude( Point, Intersection );
    
}
The function returns absurd high value if the point is not close to the line, and the real distance to the line perpendicular to it - as long as the point falls within the line segment. This way you can easily use it to detect if a player is near a line in your script.


Re: HOWTO/Tutorial: find if a point is near a line in 3D - Flake. - 15.08.2010

woah thanks for this JernejL looks good


Re: HOWTO/Tutorial: find if a point is near a line in 3D - DiddyBop - 16.08.2010

Wowze. Good job man!


Re: HOWTO/Tutorial: find if a point is near a line in 3D - BloodyEric - 06.12.2010

This is sooo awesome and exactly what I need now...thanks, turtleman