Advertisement
abobich

calculate_angles.lua

Apr 16th, 2024
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.27 KB | Gaming | 0 0
  1. function rad_to_deg(angle) return angle/math.pi*180 end
  2.  
  3. function calculate_angles(x, y, z, ex, ey, ez)
  4.  
  5. local pos =
  6. {
  7. ['x']=x,
  8. ['y']=y,
  9. ['z']=z
  10. }
  11.  
  12. local enemy_pos =
  13. {
  14. ['x']=ex,
  15. ['y']=ey,
  16. ['z']=ez
  17. }
  18.  
  19. local displacement =
  20. {
  21. ['x']=pos['x']-enemy_pos['x'],
  22. ['y']=pos['y']-enemy_pos['y'],
  23. ['z']=pos['z']-enemy_pos['z']
  24. }
  25.  
  26. local unsigned_aim_angles =
  27. {
  28. ['horizontal']=rad_to_deg(math.abs(math.atan(displacement['x']/displacement['z']))),
  29. ['vertical']=rad_to_deg(math.abs(math.atan(displacement['y']/math.sqrt(math.pow(displacement['x'], 2)+math.pow(displacement['z'], 2)))))
  30. }
  31.  
  32. local aim_angles =
  33. {
  34. ['horizontal']=0,
  35. ['vertical']=0
  36. }
  37.  
  38. if displacement['x'] < 0 then
  39.     aim_angles['horizontal'] = -unsigned_aim_angles['horizontal']
  40.     print('-x')
  41.     if displacement['z'] > 0 then
  42.         aim_angles['horizontal'] = -180 - aim_angles['horizontal']
  43.     end
  44. else
  45.     aim_angles['horizontal'] = unsigned_aim_angles['horizontal']
  46.     print('+x')
  47.     if displacement['z'] > 0 then
  48.         aim_angles['horizontal'] = 180 - aim_angles['horizontal']
  49.     end
  50. end
  51.  
  52. if displacement['y'] < 0 then
  53.     aim_angles['vertical'] = unsigned_aim_angles['vertical']
  54. else
  55.     aim_angles['vertical'] = -unsigned_aim_angles['vertical']
  56. end
  57.  
  58. return aim_angles
  59.  
  60. end
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement