Guest User

Angle Functions

a guest
Aug 9th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. //NormalizeAngle(angle).
  2.  
  3. //Converts the passed in angle into an angle between 0 and 360, not including 360.
  4. //Examples: NormalizeAngle(540) = 180, NormalizeAngle(-270) = 90, NormalizeAngle(360) = 0.
  5.  
  6.  
  7. function NormalizeAngle(angle){
  8. angle %= 360;
  9. if(angle<0){ angle += 360; }
  10. return angle;
  11. }
  12.  
  13. //AngularDistance(angle1, angle2).
  14.  
  15. //Gets the smallest angle between angle1 and angle2. Will always return a number between -180 and 180, not including //-180. A positive number means the shortest way from angle1 to angle2 goes clockwise, a negative number means the //shortest way goes counterclockwise. Also uses the NormalizeAngle function from above.
  16. //Examples: AngularDistance(90, 180) = 90, AngularDistance(180, 90) = -90, AngularDistance(1, 359) = -2.
  17.  
  18.  
  19. function AngularDistance(angle1, angle2){
  20. let distance = NormalizeAngle(angle2 - angle1);
  21. if(distance>180){ distance-=360; }
  22. return distance;
  23. }
  24.  
  25. //Function for objects: TurnToAngle.
  26.  
  27. //Three Parameters, which are id, speed and angle.
  28.  
  29. //Makes an object bullet/laser turn till it's angle is equal to the number input as the parameter angle. Doesn't loop //by itself. Speed is the speed it turns at. Inputting a negative value for speed gives you awkward results. Id is //(obviously) the id of the object.
  30.  
  31.  
  32. function TurnToAngle(id, speed, angle){
  33. let current_angle = Obj_GetAngle(id);
  34. let target_angle = angle;
  35. let adist = AngularDistance(current_angle, target_angle);
  36.  
  37. if(absolute(adist) < speed){
  38. Obj_SetAngle(id, target_angle);
  39. }
  40. else {
  41. if(adist > 0){
  42. Obj_SetAngle(id, Obj_GetAngle(id)+speed);
  43. }
  44. else if(adist < 0){
  45. Obj_SetAngle(id, Obj_GetAngle(id)-speed);
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment