{"name":"atan2","title":"atan2","type":"command","url":"https://community.bistudio.com/wiki/atan2","revid":363173,"updated":"2024-03-25T08:38:17Z","groups":["Math - Geometry"],"status":"ok","games":{"ofp":"1.00","ofpe":"1.00","arma1":"1.00","arma2":"1.00","arma2oa":"1.50","tkoh":"1.00","arma3":"0.50"},"since":"ofp 1.00","arma3_since":"0.50","description":"ArcTangent of *y/x*. Used to determine the angle of a vector *[y,x]*. Result in Degrees between -180 and 180.\n\n> [!NOTE]\n> This command can handle *x* being 0, unlike when using atan, and will return 90\n\n> [!IMPORTANT]\n> This command has higher precedence than the select command, therefore beware of the case below:\n>\n> ```sqf\n>  _pos select 0  atan2 (_pos select 1) // error\n> (_pos select 0) atan2 (_pos select 1) // OK\n> ```\n>\n> Alternatively, consider using the # operator: `_pos # 0 atan2 _pos # 1`","syntaxes":[{"syntax":"y atan2 x","params":[{"name":"y","type":"Number","type_expr":"Number"},{"name":"x","type":"Number","type_expr":"Number"}],"returns":{"type":"Number","type_expr":"Number"},"form":"binary","left":{"text":"y"},"right":{"text":"x"}}],"examples":[{"code":["_yx = [5,3];\n_degrees = (_yx select 0) atan2 (_yx select 1); // 59.0362"]},{"text":"Get direction from _obj1 to _obj2:\n\n```sqf\n_vd = getPosASL _obj2 vectorDiff getPosASL _obj1;\n_dir = (_vd select 0) atan2 (_vd select 1); // _dir range from -180 to +180\n_dir = (_dir + 360) % 360; // _dir range from 0 to 360\n```","code":["_vd = getPosASL _obj2 vectorDiff getPosASL _obj1;\n_dir = (_vd select 0) atan2 (_vd select 1); // _dir range from -180 to +180\n_dir = (_dir + 360) % 360; // _dir range from 0 to 360"]},{"text":"Get relative direction from _obj1 to _obj2:\n\n```sqf\n_yx = _obj1 worldToModel getPosASL _obj2;\n_dir = (_yx select 0) atan2 (_yx select 1); // _dir range from -180 to +180\n_dir = (_dir + 360) % 360; // _dir range from 0 to 360\n```","code":["_yx = _obj1 worldToModel getPosASL _obj2;\n_dir = (_yx select 0) atan2 (_yx select 1); // _dir range from -180 to +180\n_dir = (_dir + 360) % 360; // _dir range from 0 to 360"]}],"see_also":["atan","tan","sin","cos","asin","acos","rad","pi","vectorCos","BIS_fnc_dirTo","getRelPos","getRelDir"]}