Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //these are just the rendering controls, using the tickbox, turn them on and off how you want
  2. muscles = false;
  3. skeleton = false;
  4. outline = true;
  5. //this turns wave muscles on or off
  6. wave = false;
  7.  
  8. //verlet integration 2d
  9.  
  10. //how many points you have on the stage;  points should have instances ball1, ball2, ball3 etc.
  11. point_no = 17;
  12.  
  13. //this creates an array containing x, y, oldx, and oldy posiitions for each point
  14. points = new Array();
  15. for(i=1;i<point_no+1;i++){
  16. points[i] = {x:_root["ball"+i]._x, y:_root["ball"+i]._y, x2:_root["ball"+i]._x, y2:_root["ball"+i]._y};
  17. }
  18.  
  19. //iterations is how many times calculations are repeated per frame, should be >4 for best results but depends on what kind of
  20. //object it is
  21. iterations = 4;
  22.  
  23. //universal gravity
  24. gravity = 0.4
  25.  
  26. //on enter frame call each function
  27. function onEnterFrame(){
  28. //clear drawing after frame
  29. _root.clear();
  30. // carry out main functions;
  31. verlet();
  32. render();
  33. constraints();
  34. ai();
  35. keys();
  36. }
  37.  
  38. //the verlet function pretty much moves the points by the difference of the previous two points
  39. verlet = function(){
  40. //for each particle
  41. for(i=1;i<point_no+1;i++){
  42. //store initial point
  43. tempx = points[i].x
  44. tempy = points[i].y
  45. //verlet integration
  46. points[i].x += points[i].x - points[i].x2
  47. points[i].y += points[i].y - points[i].y2 + gravity
  48. //store previous point
  49. points[i].x2 = tempx;
  50. points[i].y2 = tempy;
  51. }
  52. }
  53.  
  54. //this controls where each particle is allowed to go
  55. constraints = function(){
  56. //for each particle carry out distance and boundary functions
  57. for(it=0;it<iterations;it++){
  58. distance();
  59. boundaries();
  60. }
  61. }
  62. //this function connects each particle/point with either a solid line (constrain) or a spring (muscle);
  63. distance = function(){
  64. //torso
  65. muscle(1, 2, 68);
  66. constrain(2, 3, 60);
  67. //chest triangle
  68. constrain(4, 7, 65);
  69. constrain(4, 3, 95);
  70. constrain(7, 3, 95);
  71. //hip triangle
  72. constrain(10, 13, 65);
  73. constrain(10, 2, 95);
  74. constrain(13, 2, 95);
  75. //waist muscles;
  76. muscle(4, 10, 100);
  77. muscle(7, 13, 100);
  78. //neck muscles;
  79. muscle(4, 1, 70);
  80. muscle(7, 1, 70);
  81. muscle(3, 1, 128);
  82. muscle(13, 1, 200);
  83. muscle(10, 1, 200);
  84. //left arm;
  85. constrain(4, 5, 60);
  86. constrain(5, 6, 50);
  87. muscle(1, 5, 130);
  88. muscle(4, 6, 110);
  89. //right arm;
  90. constrain(7, 8, 60);
  91. constrain(8, 9, 50);
  92. muscle(1, 8, 130);
  93. muscle(7, 9, 110);
  94. //left leg;
  95. constrain(10, 11, 85);
  96. constrain(11, 12, 80);
  97. muscle(10, 12, 165);
  98. muscle(3, 10, 40);
  99. //right leg;
  100. constrain(13, 14, 85);
  101. constrain(14, 15, 80);
  102. muscle(13, 15, 165);
  103. muscle(3, 13, 40);
  104. //leg width;
  105. muscle(12, 15, 90);
  106. muscle(11, 14, 100);
  107. muscle(2, 15, 280);
  108. muscle(2, 12, 280);
  109. //feet;
  110. constrain(12, 16, 30);
  111. constrain(15, 17, 30);
  112. muscle(16, 11, 100);
  113. muscle(17, 14, 100);
  114. muscle(17, 16, 170);
  115. //arms up;, the wave function only occurs on clicks
  116. if(wave == true){
  117. muscle(9, 13, 220);
  118. muscle(6, 10, 220);
  119. muscle(15, 9, 350);
  120. muscle(12, 6, 350);
  121. }
  122. }
  123. //edge of the world and friction
  124. boundaries = function(){
  125. for(i=1;i<point_no+1;i++){
  126. if(points[i].x > 540) (points[i].x = 540)
  127. if(points[i].x < 0) (points[i].x = 0)
  128. if(points[i].y > 400){
  129. //friction depends on the normal force ie penetration depth...
  130. depth = points[i].y-400
  131. points[i].x -= depth*(points[i].x-points[i].x2)/10
  132. points[i].y = 400;
  133. }
  134. //move the balls to the positions
  135. _root["ball"+i]._x = points[i].x
  136. _root["ball"+i]._y = points[i].y
  137. }
  138. //position head movie clip (you'll need one of these on the stage);
  139. head._x = ball1._x
  140. head._y = ball1._y
  141. }
  142. //the constrain function
  143. constrain = function(point, target, distance){
  144. r = distance;
  145. dx = points[point].x-points[target].x
  146. dy = points[point].y-points[target].y
  147. d1 = Math.round(Math.sqrt(dx*dx + dy*dy))
  148. d2 = 0.5*(d1-r)/d1
  149. dx = dx*d2;
  150. dy = dy*d2;
  151. points[target].x += dx; points[target].y += dy;
  152. points[point].x -= dx; points[point].y -= dy
  153. if(skeleton==true){
  154. _root.lineStyle(2, 0xFF0000);
  155. _root.moveTo(points[target].x, points[target].y);
  156. _root.lineTo(points[point].x, points[point].y);
  157. }
  158. }
  159. //the muscle function;
  160. muscle = function(point, target, distance){
  161. r = distance;
  162. dx = points[point].x-points[target].x
  163. dy = points[point].y-points[target].y
  164. d1 = Math.round(Math.sqrt(dx*dx + dy*dy))
  165. d2 = 0.05*(d1-r)/d1
  166. dx = dx*d2;
  167. dy = dy*d2;
  168. points[target].x += dx; points[target].y += dy;
  169. points[point].x -= dx; points[point].y -= dy;
  170. if(muscles==true){
  171. _root.lineStyle(2, 0x0000FF);
  172. _root.moveTo(points[target].x, points[target].y);
  173. _root.lineTo(points[point].x, points[point].y);
  174. }
  175. }
  176. //control with the keys
  177. keys = function(){
  178. for(i=1;i<point_no+1;i++){
  179. if(Key.isDown(Key.RIGHT)) points[i].x += gravity*2
  180. if(Key.isDown(Key.LEFT)) points[i].x -= gravity*2
  181. if(Key.isDown(Key.UP)) points[i].y -= gravity*2
  182. if(Key.isDown(Key.DOWN)) points[i].y += gravity*2
  183. }
  184. }
  185. //render it
  186. render = function(){
  187. if(outline==true){
  188. _root.lineStyle(10, 0x000000, 100);
  189. _root.moveTo(ball1._x, ball1._y);
  190. _root.lineTo((ball4._x+ball7._x)/2, (ball4._y+ball7._y)/2);
  191. _root.moveTo(ball4._x, ball4._y);
  192. _root.beginFill(0x000000, 100);
  193. _root.curveTo((ball2._x+ball3._x)/2, (ball2._y+ball3._y)/2, (ball10._x+ball11._x)/2, (ball10._y+ball11._y)/2);
  194. _root.curveTo((ball10._x+ball13._x)/2, (ball10._y+ball13._y)/2, (ball13._x+ball14._x)/2, (ball13._y+ball14._y)/2);
  195. _root.curveTo((ball2._x+ball3._x)/2, (ball2._y+ball3._y)/2, ball7._x, ball7._y);
  196. _root.lineTo(ball4._x, ball4._y);
  197. _root.endFill();
  198. _root.lineTo(ball5._x, ball5._y);
  199. _root.lineTo(ball6._x, ball6._y);
  200. _root.moveTo(ball7._x, ball7._y);
  201. _root.lineTo(ball8._x, ball8._y);
  202. _root.lineTo(ball9._x, ball9._y);
  203. _root.moveTo((ball10._x+ball11._x)/2, (ball10._y+ball11._y)/2);
  204. _root.lineTo(ball11._x, ball11._y);
  205. _root.lineTo(ball12._x, ball12._y);
  206. _root.lineTo(ball16._x, ball16._y);
  207. _root.moveTo((ball13._x+ball14._x)/2, (ball13._y+ball14._y)/2);
  208. _root.lineTo(ball14._x, ball14._y);
  209. _root.lineTo(ball15._x, ball15._y);
  210. _root.lineTo(ball17._x, ball17._y);
  211. }
  212. }
  213. //control the wave function;
  214. _root.onMouseDown = function(){
  215. wave = true;
  216. }
  217. _root.onMouseUp = function(){
  218. wave = false;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement