Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. //create game object
  2. game = {dragging:false, clip:_root.game_mc};
  3. //create object
  4. //point p0 is its starting point in the coordinates x/y
  5. game.myOb = {clip:game.clip.ob_mc, r:40};
  6. game.myOb.p1 = {x:150, y:80};
  7. //create first vector
  8. //point p0 is its starting point in the coordinates x/y
  9. //point p1 is its end point in the coordinates x/y
  10. game.v1 = {p0:{x:50, y:120}, p1:{x:250, y:120}};
  11. game.v2 = {};
  12. //function to draw the points, lines and show text
  13. function drawAll(v)
  14. {
  15. //place ob mc
  16. v.clip._x = v.p1.x;
  17. v.clip._y = v.p1.y;
  18. //drawing the walls
  19. game.clip.p1._x = v.p1.x;
  20. game.clip.p1._y = v.p1.y;
  21. game.clip.p2._x = game.v1.p0.x;
  22. game.clip.p2._y = game.v1.p0.y;
  23. game.clip.p3._x = game.v1.p1.x;
  24. game.clip.p3._y = game.v1.p1.y;
  25. game.clip.dragger1._x = v.p1.x;
  26. game.clip.dragger1._y = v.p1.y;
  27. game.clip.dragger2._x = game.v1.p0.x;
  28. game.clip.dragger2._y = game.v1.p0.y;
  29. game.clip.dragger3._x = game.v1.p1.x;
  30. game.clip.dragger3._y = game.v1.p1.y;
  31. game.clip.v1._x = game.v1.p0.x;
  32. game.clip.v1._y = game.v1.p0.y;
  33. game.clip.v1._xscale = game.v1.vx;
  34. game.clip.v1._yscale = game.v1.vy;
  35. game.clip.v2._x = v.p1.x;
  36. game.clip.v2._y = v.p1.y;
  37. game.clip.v2._xscale = game.v2.vx;
  38. game.clip.v2._yscale = game.v2.vy;
  39. game.clip.v3._x = game.v3.p0.x;
  40. game.clip.v3._y = game.v3.p0.y;
  41. game.clip.v3._xscale = game.v3.vx;
  42. game.clip.v3._yscale = game.v3.vy;
  43. }
  44. //main function
  45. function runMe()
  46. {
  47. //check if point is dragged
  48. if (game.dragging)
  49. {
  50. var v = game.myOb;
  51. var x = Math.round(game.clip.dragger1._x);
  52. var y = Math.round(game.clip.dragger1._y);
  53. v.p1.x = x;
  54. v.p1.y = y;
  55. var v = game.v1;
  56. var x = Math.round(game.clip.dragger2._x);
  57. var y = Math.round(game.clip.dragger2._y);
  58. v.p0.x = x;
  59. v.p0.y = y;
  60. var x = Math.round(game.clip.dragger3._x);
  61. var y = Math.round(game.clip.dragger3._y);
  62. v.p1.x = x;
  63. v.p1.y = y;
  64. updateVector(v, true);
  65. }
  66. //start to calculate movement
  67. var ob = game.myOb;
  68. //check the wall for collisions
  69. var w = game.v1;
  70. var v = findIntersection(ob, w);
  71. v = updateVector(v, false);
  72. var pen = ob.r-v.len;
  73. //if we have hit the wall
  74. if (pen>=0)
  75. {
  76. //move object away from collision point
  77. game.v2.vx = v.dx*pen;
  78. game.v2.vy = v.dy*pen;
  79. }
  80. else
  81. {
  82. //hide this vector so the example look nicer
  83. game.v2.vx = 0;
  84. game.v2.vy = 0;
  85. }
  86. //drawing the line which shows closest distance to the wall
  87. game.v3 = v;
  88. game.v3.p0 = {};
  89. game.v3.p0.x = ob.p1.x-game.v3.vx;
  90. game.v3.p0.y = ob.p1.y-game.v3.vy;
  91. //draw it
  92. drawAll(ob);
  93. }
  94. //function to find all parameters for the vector
  95. function updateVector(v, frompoints)
  96. {
  97. //x and y components
  98. if (frompoints)
  99. {
  100. v.vx = v.p1.x-v.p0.x;
  101. v.vy = v.p1.y-v.p0.y;
  102. }
  103. else
  104. {
  105. v.p1.x = v.p0.x+v.vx;
  106. v.p1.y = v.p0.y+v.vy;
  107. }
  108. //length of vector
  109. v.len = Math.sqrt(v.vx*v.vx+v.vy*v.vy);
  110. //normalized unti-sized components
  111. if (v.len>0)
  112. {
  113. v.dx = v.vx/v.len;
  114. v.dy = v.vy/v.len;
  115. }
  116. else
  117. {
  118. v.dx = 0;
  119. v.dy = 0;
  120. }
  121. //right hand normal
  122. v.rx = -v.dy;
  123. v.ry = v.dx;
  124. //left hand normal
  125. v.lx = v.dy;
  126. v.ly = -v.dx;
  127. return v;
  128. }
  129. //find intersection point of 2 vectors
  130. function findIntersection(v1, v2)
  131. {
  132. //vector between center of ball and starting point of wall
  133. var v3 = {};
  134. v3.vx = v1.p1.x-v2.p0.x;
  135. v3.vy = v1.p1.y-v2.p0.y;
  136. //check if we have hit starting point
  137. var dp = v3.vx*v2.dx+v3.vy*v2.dy;
  138. if (dp<0)
  139. {
  140. //hits starting point
  141. var v = v3;
  142. }
  143. else
  144. {
  145. var v4 = {};
  146. v4.vx = v1.p1.x-v2.p1.x;
  147. v4.vy = v1.p1.y-v2.p1.y;
  148. //check if we have hit side or endpoint
  149. var dp = v4.vx*v2.dx+v4.vy*v2.dy;
  150. if (dp>0)
  151. {
  152. //hits ending point
  153. var v = v4;
  154. }
  155. else
  156. {
  157. //it hits the wall
  158. //project this vector on the normal of the wall
  159. var v = projectVector(v3, v2.lx, v2.ly);
  160. }
  161. }
  162. return v;
  163. }
  164. //project vector v1 on unit-sized vector dx/dy
  165. function projectVector(v1, dx, dy)
  166. {
  167. //find dot product
  168. var dp = v1.vx*dx+v1.vy*dy;
  169. var proj = {};
  170. //projection components
  171. proj.vx = dp*dx;
  172. proj.vy = dp*dy;
  173. return proj;
  174. }
  175. //calculate all parameters for the wall vectors
  176. updateVector(game.v1, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement