AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Calculating distance between aiming line and entity (https://forums.alliedmods.net/showthread.php?t=163618)

lazarev 08-01-2011 11:13

Calculating distance between aiming line and entity
 
Here's an example:
[img]http://img708.**************/img708/8715/requestc.jpg[/img]


red square is some entity, blue is me, aiming is aiming line, distance is the distance I want to get

Ryokin 08-01-2011 11:20

Re: Calculating distance between aiming line and entity
 
PHP Code:

stock get_distance_to_line(Float:pos_start[3], Float:pos_end[3], Float:pos_object[3])  
{  
    new 
Float:vec_start_end[3], Float:vec_start_object[3], Float:vec_end_object[3], Float:vec_end_start[3]
    
xs_vec_sub(pos_endpos_startvec_start_end)         // vector from start to end 
    
xs_vec_sub(pos_objectpos_startvec_start_object)     // vector from end to object 
    
xs_vec_sub(pos_startpos_endvec_end_start)         // vector from end to start 
    
xs_vec_sub(pos_endpos_objectvec_end_object)         // vector object to end 
    
    
new Float:len_start_object getVecLen(vec_start_object
    new 
Float:angle_start floatacos(xs_vec_dot(vec_start_endvec_start_object) / (getVecLen(vec_start_end) * len_start_object), degrees)  
    new 
Float:angle_end floatacos(xs_vec_dot(vec_end_startvec_end_object) / (getVecLen(vec_end_start) * getVecLen(vec_end_object)), degrees)  

    if(
angle_start <= 90.0 && angle_end <= 90.0
        return 
floatround(len_start_object floatsin(angle_startdegrees)) 
    return -
1  



Exolent[jNr] 08-01-2011 12:32

Re: Calculating distance between aiming line and entity
 
Note that the stock will get the line perpendicular to your aim vector for the smallest distance to your aim vector.

SpeeDeeR 08-01-2011 13:25

Re: Calculating distance between aiming line and entity
 
Quote:

Originally Posted by Exolent[jNr] (Post 1523476)
Note that the stock will get the line perpendicular to your aim vector for the smallest distance to your aim vector.

Well, distance from point to line, is the perpendicular to the line drawn from the point.

Exolent[jNr] 08-01-2011 13:34

Re: Calculating distance between aiming line and entity
 
Quote:

Originally Posted by SpeeDeeR (Post 1523518)
Well, distance from point to line, is the perpendicular to the line drawn from the point.

Yes, I know. I was only stating it since his picture does not show that.


All times are GMT -4. The time now is 03:25.

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