AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Distance between two lines (https://forums.alliedmods.net/showthread.php?t=196926)

meTaLiCroSS 09-26-2012 19:08

Distance between two lines
 
I've tried to find such thing in the forum and I would not able to find something to answer my question, at least I've found this(http://forums.alliedmods.net/showpos...28&postcount=2) and it's useful at least, but it's close of what I want.

I wanted to check with a math code (welcome, not my speciality) the "distance" between two lines on the game, for being accurate, 2 players and 2 objects flying on air, both players owns one of these entities, and I wanted to check (by think forward) if one of these objects intersects with anyother's player object, a virtual collision, to be specific, to check if it crosses anyother's players invisible line to it's flying object, I'm not sure if I spelled it right what i've tried to say, but would be nice to see some math art in action here n.n

Anyway, thanks in advance! :)

gladius 09-26-2012 19:24

Re: Distance between two lines
 
I have an idea of what you want but I am in doubt. You could put a picture to understand.

ConnorMcLeod 09-27-2012 01:25

Re: Distance between two lines
 
Try this :

PHP Code:

Float:lines_distance(Float:flPoint1[3], Float:vecDir1[3], Float:flPoint2[3], Float:vecDir2[3])
{
    
xs_vec_normalize(vecDir1vecDir1)
    
xs_vec_normalize(vecDir2vecDir2)
    if( 
xs_vec_equal(vecDir1vecDir2) )
    {
        new 
Float:vecPoint1Point2[3]
        
xs_vec_sub(flPoint2flPoint1vecPoint1Point2)
        
        new 
Float:vecDirCross[3]
        
xs_vec_cross(vecPoint1Point2vecDir1vecDirCross)
        
        return 
xs_vec_len(vecDirCross) / xs_vec_len(vecDir1)
    }

    new 
Float:vecPoint1Point2[3]
    
xs_vec_sub(flPoint2flPoint1vecPoint1Point2)
    
    new 
Float:vecDirCross[3]
    
xs_vec_cross(vecDir1vecDir2vecDirCross)

    return 
floatabsdet3(vecPoint1Point2vecDir1vecDir2) ) / xs_vec_len(vecDirCross)
}

Float:det3Float:vec0[3] , Float:vec1[3] , Float:vec2[3] )
{
    return ( 
vec0[0]*vec1[1]*vec2[2] + vec1[0]*vec2[1]*vec0[2] + vec2[0]*vec0[1]*vec1[2] - vec0[0]*vec2[1]*vec1[2] - vec1[0]*vec0[1]*vec2[2] - vec2[0]*vec1[1]*vec0[2] )



meTaLiCroSS 09-27-2012 19:09

Re: Distance between two lines
 
Quote:

Originally Posted by ConnorMcLeod (Post 1807329)
Try this :

PHP Code:

Float:lines_distance(Float:flPoint1[3], Float:vecDir1[3], Float:flPoint2[3], Float:vecDir2[3])
{
    
xs_vec_normalize(vecDir1vecDir1)
    
xs_vec_normalize(vecDir2vecDir2)
    if( 
xs_vec_equal(vecDir1vecDir2) )
    {
        new 
Float:vecPoint1Point2[3]
        
xs_vec_sub(flPoint2flPoint1vecPoint1Point2)
        
        new 
Float:vecDirCross[3]
        
xs_vec_cross(vecPoint1Point2vecDir1vecDirCross)
        
        return 
xs_vec_len(vecDirCross) / xs_vec_len(vecDir1)
    }

    new 
Float:vecPoint1Point2[3]
    
xs_vec_sub(flPoint2flPoint1vecPoint1Point2)
    
    new 
Float:vecDirCross[3]
    
xs_vec_cross(vecDir1vecDir2vecDirCross)

    return 
floatabsdet3(vecPoint1Point2vecDir1vecDir2) ) / xs_vec_len(vecDirCross)
}

Float:det3Float:vec0[3] , Float:vec1[3] , Float:vec2[3] )
{
    return ( 
vec0[0]*vec1[1]*vec2[2] + vec1[0]*vec2[1]*vec0[2] + vec2[0]*vec0[1]*vec1[2] - vec0[0]*vec2[1]*vec1[2] - vec1[0]*vec0[1]*vec2[2] - vec2[0]*vec1[1]*vec0[2] )



The bad thing is that I need to pass a direction instead of another position, there exists a possibility for making it with and start pos and a end post for both? :(

gladius 09-28-2012 12:24

Re: Distance between two lines
 
If I am right, this should be fine.

PHP Code:

Float:lines_distance(Float:flLine1Point1[3], Float:flLine1Point2[3], Float:flLine2Point1[3], Float:flLine2Point2[3]) 

    new 
Float:vecDir1[3], Float:vecDir2[3]
    
    
xs_vec_sub(flLine1Point1flLine1Point2vecDir1)
    
xs_vec_sub(flLine2Point1flLine2Point2vecDir2)
    
    
xs_vec_normalize(vecDir1vecDir1
    
xs_vec_normalize(vecDir2vecDir2
    
    new 
Float:vecLine1Line2[3
    
xs_vec_sub(flLine2Point1flLine1Point1vecLine1Line2)
    
    if(
xs_vec_equal(vecDir1vecDir2)) 
    {         
        new 
Float:vecDirCross[3
        
xs_vec_cross(vecLine1Line2vecDir1vecDirCross
        
        return 
xs_vec_len(vecDirCross) / xs_vec_len(vecDir1
    } 
    
    new 
Float:vecDirCross[3
    
xs_vec_cross(vecDir1vecDir2vecDirCross

    return 
xs_vec_mul_escalar2(vecLine1Line2vecDirCross) / xs_vec_len(vecDirCross)
}

xs_vec_mul_escalar2(Float:vec1[], Float:vec2[])
{
    return 
vec1[0] * vec2[0] + vec1[1] * vec2[1] + vec1[2] * vec2[2]



meTaLiCroSS 09-29-2012 15:41

Re: Distance between two lines
 
Quote:

Originally Posted by gladius (Post 1808283)
If I am right, this should be fine.

PHP Code:

Float:lines_distance(Float:flLine1Point1[3], Float:flLine1Point2[3], Float:flLine2Point1[3], Float:flLine2Point2[3]) 

    new 
Float:vecDir1[3], Float:vecDir2[3]
    
    
xs_vec_sub(flLine1Point1flLine1Point2vecDir1)
    
xs_vec_sub(flLine2Point1flLine2Point2vecDir2)
    
    
xs_vec_normalize(vecDir1vecDir1
    
xs_vec_normalize(vecDir2vecDir2
    
    new 
Float:vecLine1Line2[3
    
xs_vec_sub(flLine2Point1flLine1Point1vecLine1Line2)
    
    if(
xs_vec_equal(vecDir1vecDir2)) 
    {         
        new 
Float:vecDirCross[3
        
xs_vec_cross(vecLine1Line2vecDir1vecDirCross
        
        return 
xs_vec_len(vecDirCross) / xs_vec_len(vecDir1
    } 
    
    new 
Float:vecDirCross[3
    
xs_vec_cross(vecDir1vecDir2vecDirCross

    return 
xs_vec_mul_escalar2(vecLine1Line2vecDirCross) / xs_vec_len(vecDirCross)
}

xs_vec_mul_escalar2(Float:vec1[], Float:vec2[])
{
    return 
vec1[0] * vec2[0] + vec1[1] * vec2[1] + vec1[2] * vec2[2]



As from I see you're getting the direction between these 2 points and that can result in a true statement when 2 lines can "intersect only when extended" but not as this real length, or i'm just saying bullshit? haha

gladius 09-29-2012 16:08

Re: Distance between two lines
 
I am getting director vector from two points in one line and getting director vector from two lines.

joropito 10-01-2012 17:02

Re: Distance between two lines
 
Put a drawing about what you need.

You write the distance between 2 lines, so you have to get 2 vectors and get the difference. The result will be a vector (vector - vector = vector). If you want a distance in units, just get the lenght of the resulting vector.

Remember that 2 lines will not always be parallel. If you wan't distance between 2 "always parallel" lines, then it's fair easy to get it but it's the same principle.

gladius 10-01-2012 17:48

Re: Distance between two lines
 
Quote:

Originally Posted by joropito (Post 1810692)
Put a drawing about what you need.

You write the distance between 2 lines, so you have to get 2 vectors and get the difference. The result will be a vector (vector - vector = vector). If you want a distance in units, just get the lenght of the resulting vector.

Remember that 2 lines will not always be parallel. If you wan't distance between 2 "always parallel" lines, then it's fair easy to get it but it's the same principle.

Two lines are parallel if their direction vectors are equal.

PHP Code:

if( xs_vec_equal(vecDir1vecDir2) ) 

About ...distance between 2 lines, so you have to get 2 vectors and get the difference. The result will be a vector (vector - vector = vector)...

This subtraction only finds a relationship between lines.

Real distance are calculate like I did in my example. Also it return a number, you must proportionate the correct unit type.

joropito 10-01-2012 19:03

Re: Distance between two lines
 
Quote:

Originally Posted by gladius (Post 1810723)
Two lines are parallel if their direction vectors are equal.

PHP Code:

if( xs_vec_equal(vecDir1vecDir2) ) 

About ...distance between 2 lines, so you have to get 2 vectors and get the difference. The result will be a vector (vector - vector = vector)...

This subtraction only finds a relationship between lines.

Real distance are calculate like I did in my example. Also it return a number, you must proportionate the correct unit type.

Sure, you can get a distance from 2 points, or at least a minimum distance from a point and a line.
What happens if both lines are not parallel? Somewhere they will be closer and somewhere not.


All times are GMT -4. The time now is 08:20.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.