AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Get the angle of a line between two origins (https://forums.alliedmods.net/showthread.php?t=86043)

Bugsy 02-19-2009 21:54

Get the angle of a line between two origins
 
I am trying to get the angle of a line between 2 origins. I have searched and tried quite a few ways but nothing is working for me. I was able to get the angle of my aim using pev_v_angle but that will only return the angle I am currently aiming; I need it to compute the angle based on 2 inputted origins. Is there a way to do this?

Example, I would need the angle of the line shown below (would prob be ~45 degree or so)
http://img.photobucket.com/albums/v2...rona/angle.jpg

Among a few other ways, I've tried using fm_get_view_angle_diff() and the below code with no luck.

PHP Code:

new Float:fOrigin1[3];
new 
Float:fOrigin2[3];
pev(id,pev_originfOrigin1);
fm_get_aim_origin(idfOrigin2 );
fAngle xs_vec_anglefOrigin1fOrigin2);
client_print(0,print_chat,"Angle: %f" fAngle ); 

+karma for anyone who can help

Exolent[jNr] 02-20-2009 12:23

Re: Get the angle of a line between two origins
 
EDIT: Nvm.

Here is how I found the angle between 2 vectors in my Pre-Calculus class:
Code:
Float:AngleBetweenVectors(Float:vector1[3], Float:vector2[3], dimensions, anglemode:angletype) {     if( !(1 <= dimensions <= 3) ) return 0.0;         new Float:v1[3] = vector1;     new Float:v2[3] = vector2;         for( new i = dimensions; i < 3; i++ )     {         v1[i] = v2[i] = 0.0;     }         // cos theta = (v1 * v2) / (v1.length * v2.length);     return floatacos(((v1[0]*v2[0] ) + (v1[1]*v2[1]) + (v1[2]*v2[2])) / (vector_length(v1) * vector_length(v2)), angletype); }

Here is an example of the angle between a player's aim direction and their movement:
Code:
new Float:head_origin[3], Float:aim_origin[3], Float:velocity[3]; engfunc(EngFunc_GetBonePosition, client, 8, head_origin, velocity); fm_get_aim_origin(client, aim_origin); aim_origin[0] -= head_origin[0]; aim_origin[1] -= head_origin[1]; aim_origin[2] -= head_origin[2]; pev(client, pev_velocity, velocity); client_print(client, print_chat, "Angle between aim & velocity = %f", AngleBetweenVectors(aim_origin, velocity, 3, degrees));

Note: The angle will always be 0 - 180 in degrees, 0 - pi in radians, etc.

stupok 02-20-2009 18:16

Re: Get the angle of a line between two origins
 
Code:

new Float:fOrigin1[3]
new Float:fOrigin2[3]
new Float:fVector[3]
new Float:fAngle[3]

xs_vec_sub(fOrigin2, fOrigin1, fVector)
vector_to_angle(fVector, fAngle)

xs_vec_angle is for vectors, not origins

Bugsy 02-20-2009 22:01

Re: Get the angle of a line between two origins
 
Quote:

Originally Posted by stupok (Post 765611)
Code:

new Float:fOrigin1[3]
new Float:fOrigin2[3]
new Float:fVector[3]
new Float:fAngle[3]

xs_vec_sub(fOrigin2, fOrigin1, fVector)
vector_to_angle(fVector, fAngle)

xs_vec_angle is for vectors, not origins

Thanks stupok & exolent. +karma to both

I put it into a function to return 89 when aiming straight up, 0 when aiming perfectly straight, and -89 aiming straight down (when the aiming players origin is passed as first origin param). For anyone else that needs this:

PHP Code:

FloatGetAngleOrigins(Float:fOrigin1[3], Float:fOrigin2[3] )
{
    new 
Float:fVector[3];
    new 
Float:fAngle[3];
    new 
Float:fLineAngle;
    
    
xs_vec_sub(fOrigin2fOrigin1fVector);
    
vector_to_angle(fVectorfAngle);
    
    if( 
fAngle[0] > 90.0 )
        
fLineAngle = -(360.0 fAngle[0]);
    else
        
fLineAngle fAngle[0];
    
    return 
fLineAngle;


To see it in action:
PHP Code:

new Float:fOrigin1[3];
new 
Float:fOrigin2[3];
pevid pev_originfOrigin1 );
fm_get_aim_origin(idfOrigin2 );
        
client_print(id,print_chat,"Angle of line from you to where aiming: %f" GetAngleOrigins(fOrigin1,fOrigin2) ); 



All times are GMT -4. The time now is 17:07.

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