Posted By


allnatural on 03/16/09

Tagged


Statistics


Viewed 180 times
Favorited by 0 user(s)

Swinging Pendulum


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //
  2. //
  3. // Initial variables.
  4. var pendX = 250;
  5. var pendY = 250;
  6. var pendRadius = 200;
  7. var pendArc = 135/360;
  8. var pendSpeed = .005;
  9. var pendCount = 0;
  10. //
  11. //
  12. //
  13. // Make a red footbal-shaped clip.
  14. createEmptyMovieClip("Pendu", 10);
  15. with(Pendu){
  16. beginFill(0xFF0000);
  17. moveTo(-20, 0);
  18. curveTo(0, 15, 20, 0);
  19. curveTo(0, -15, -20, 0);
  20. }
  21. //
  22. // Make a clip in which to draw the stick holding the pendulum.
  23. createEmptyMovieClip("Stick", 5);
  24. //
  25. //
  26. //
  27. // Swing a point on an eased arc with the given properties.
  28. function pendulum (centerX, centerY, radius, aoi, completionRatio){
  29. //
  30. // Use magic trig function to turn linear values between 0
  31. // and 1 to eased values between -1 and +1.
  32. var easedOneToNegOne = Math.cos(completionRatio*2*Math.PI);
  33. //
  34. // Convert "Angle Of Inclination" from a ratio between 0
  35. // and 1 to radians.
  36. var aoiRadians = aoi * 2 * Math.PI;
  37. //
  38. // Determine eased rotation of the pendulum in radians.
  39. // Derived from #3 in the above "Conventions" section.
  40. var currentRotation = easedOneToNegOne * aoiRadians;
  41. //
  42. // Get X and Y coordinates of pendulum at eased angle of rotation.
  43. var x = centerX + Math.sin(currentRotation) * radius;
  44. var y = centerY + Math.cos(currentRotation) * radius;
  45. //
  46. // Return both X and Y coordinates inside a single 'point' object.
  47. return {x:x, y:y};
  48. }
  49. //
  50. //
  51. //
  52. // Advance the pendulum to its next position.
  53. function swingPendulum (){
  54. //
  55. // Increment the clock counter.
  56. pendCount += pendSpeed;
  57. pendCount %= 1;
  58. //
  59. // Get the pendulum's new coordinates.
  60. var point = pendulum (pendX, pendY, pendRadius, pendArc, pendCount);
  61. //
  62. // Place the pendulum at its new coordinates.
  63. Pendu._x = point.x;
  64. Pendu._y = point.y;
  65. //
  66. // Draw a line from the center to the pendulum.
  67. with (Stick){
  68. clear();
  69. lineStyle(2, 0);
  70. moveTo(pendX, pendY);
  71. lineTo(point.x, point.y);
  72. }
  73. }
  74. //
  75. // Keep Swinging the pendulum every time we enter a frame.
  76. onEnterFrame = swingPendulum;
  77. //
  78. //
  79. //
  80. //

URL: http://www.pixelwit.com/blog

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.