Raised This Month: $ Target: $400
 0% 

Get the angle of a line between two origins


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 02-19-2009 , 21:54   Get the angle of a line between two origins
Reply With Quote #1

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)


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
__________________

Last edited by Bugsy; 02-20-2009 at 10:25.
Bugsy is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-20-2009 , 12:23   Re: Get the angle of a line between two origins
Reply With Quote #2

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 02-20-2009 at 12:40.
Exolent[jNr] is offline
stupok
Veteran Member
Join Date: Feb 2006
Old 02-20-2009 , 18:16   Re: Get the angle of a line between two origins
Reply With Quote #3

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
__________________
stupok is offline
Old 02-20-2009, 19:01
fysiks
This message has been deleted by fysiks. Reason: oops
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 02-20-2009 , 22:01   Re: Get the angle of a line between two origins
Reply With Quote #4

Quote:
Originally Posted by stupok View Post
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) ); 
__________________

Last edited by Bugsy; 02-21-2009 at 00:56.
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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