Advertisement
nutter666

Orbital Rotation Code

Jan 13th, 2016
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. function eFrame(e:Event){
  2. getOrbitAngle();
  3. rotateOrbitAngle();
  4. rotateBlaster();
  5. }
  6.  
  7. // MathX is a class containing a few extra math functions I use a lot
  8.  
  9. function getOrbitAngle(){
  10. var o:int = MathX.angleToObject(orbitPoint,new Point(stage.mouseX,stage.mouseY));
  11. targetAngle = MathX.normaliseAngle(o+90)
  12. }
  13.  
  14. // angleToObject calculates the angle needed to rotate one object towards another
  15. //normaliseAngle returns a value between 0 and 360
  16.  
  17.  
  18. function rotateOrbitAngle(){
  19.  
  20. if(currentAngle>targetAngle){
  21.  
  22. if(currentAngle>targetAngle+180){
  23. rotateInReverse()
  24. }
  25. else{
  26. rotateNormally();
  27. }
  28. }
  29.  
  30. else if(currentAngle<targetAngle-180){
  31. rotateInReverse()
  32. }
  33. else{
  34. rotateNormally();
  35. }
  36. currentAngle = MathX.normaliseAngle(currentAngle)
  37. }
  38.  
  39.  
  40. // rotate normally/
  41. function rotateNormally() {
  42. if(currentAngle > targetAngle){
  43. currentAngle -= playerSpeed
  44. }
  45. else if(currentAngle < targetAngle){
  46. currentAngle += playerSpeed
  47. }
  48. }
  49.  
  50. function rotateInReverse() {
  51. // rotate in reverse
  52. if(currentAngle > targetAngle){
  53. currentAngle += playerSpeed
  54. }
  55. else if(currentAngle < targetAngle){
  56. currentAngle -= playerSpeed
  57. }
  58. }
  59.  
  60. function rotateBlaster(){
  61. player.rotation = currentAngle
  62. var rad = MathX.degsToRads(currentAngle-90)
  63. player.x = orbitPoint.x + radius * Math.cos(rad); // Position The player Along x-axis
  64. player.y = orbitPoint.y + radius * Math.sin(rad); // Position The player Along y-axis
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement