Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. explosion.js
  2.  
  3.  
  4. /*
  5. * Advanced Explosion effect
  6. * Each particle has a different size, move speed and scale speed.
  7. *
  8. * Parameters:
  9. * x, y - explosion center
  10. * color - particles' color
  11. */
  12. /*
  13. * A single explosion particle
  14. */
  15. var particles = [];
  16.  
  17. function Particle () {
  18. this.scale = 1.0;
  19. this.x = 0;
  20. this.y = 0;
  21. this.radius = 20;
  22. this.color = "#000";
  23. this.velocityX = 0;
  24. this.velocityY = 0;
  25. this.scaleSpeed = 0.5;
  26.  
  27. this.update = function(ms)
  28. {
  29. // shrinking
  30. this.scale -= this.scaleSpeed * ms / 1000.0;
  31.  
  32. if (this.scale <= 0)
  33. {
  34. this.scale = 0;
  35. }
  36. // moving away from explosion center
  37. this.x += this.velocityX * ms/1000.0;
  38. this.y += this.velocityY * ms/1000.0;
  39. };
  40.  
  41. this.draw = function(context2D)
  42. {
  43. // translating the 2D context to the particle coordinates
  44. context2D.save();
  45. context2D.translate(this.x, this.y);
  46. context2D.scale(this.scale, this.scale);
  47.  
  48. // drawing a filled circle in the particle's local space
  49. context2D.beginPath();
  50. context2D.arc(0, 0, this.radius, 0, Math.PI*2, true);
  51. context2D.closePath();
  52.  
  53. context2D.fillStyle = this.color;
  54. context2D.fill();
  55.  
  56. context2D.restore();
  57. };
  58. }
  59.  
  60.  
  61.  
  62. function createExplosion(x, y, color) {
  63. var minSize = 10;
  64. var maxSize = 30;
  65. var count = 10;
  66. var minSpeed = 60.0;
  67. var maxSpeed = 200.0;
  68. var minScaleSpeed = 1.0;
  69. var maxScaleSpeed = 4.0;
  70.  
  71. for (var angle=0; angle<360; angle += Math.round(360/count))
  72. {
  73. var particle = new Particle();
  74.  
  75. particle.x = x;
  76. particle.y = y;
  77.  
  78. particle.radius = randomFloat(minSize, maxSize);
  79.  
  80. particle.color = color;
  81.  
  82. particle.scaleSpeed = randomFloat(minScaleSpeed, maxScaleSpeed);
  83.  
  84. var speed = randomFloat(minSpeed, maxSpeed);
  85.  
  86. particle.velocityX = speed * Math.cos(angle * Math.PI / 180.0);
  87. particle.velocityY = speed * Math.sin(angle * Math.PI / 180.0);
  88.  
  89. particles.push(particle);
  90. }
  91. }
  92. function update (frameDelay)
  93. {
  94. // draw a white background to clear canvas
  95. context2D.fillStyle = "#FFF";
  96. //context2D.fillRect(0, 0, context2D.canvas.width, context2D.canvas.height);
  97.  
  98. // update and draw particles
  99. for (var i=0; i<particles.length; i++)
  100. {
  101. var particle = particles[i];
  102.  
  103. particle.update(frameDelay);
  104. particle.draw(context2D);
  105. }
  106.  
  107. }
  108.  
  109. function randomFloat (min, max) {
  110. return min + Math.random()*(max-min);
  111. }
  112.  
  113.  
  114.  
  115. --------------------------------------------------------------------------------
  116.  
  117.  
  118.  
  119.  
  120. physics.js
  121.  
  122.  
  123.  
  124. console.log("Physics loaded...");
  125.  
  126.  
  127. // given: xInitial, timeElapsed (Δt), velocity (v), angle (θ)
  128. // we want: xCurrent
  129.  
  130. // xCurrent == xInitial + Δt * vHorizontal
  131. // vHorizontal == v * Math.cos(angleInRadians)
  132. // angleInRadians == (Math.PI/180) * θ
  133.  
  134. function radians(angle) {
  135. return (Math.PI/180) * angle;
  136. }
  137.  
  138. function vHorizontal(v, angle) {
  139. return v * Math.cos(radians(angle));
  140. }
  141.  
  142. function xCurrent(xInitial, timeElapsed, v, angle) {
  143. return xInitial + (timeElapsed * vHorizontal(v, angle));
  144. }
  145.  
  146.  
  147. // yCurrent == yInitial + yDistance + gravityDistance
  148. // gravityDistance == 0.5 * g * (Δt * Δt)
  149. // g == 0.0004 px/(ms^2) // FROM PhysicsJS
  150. // yDistance == -vVertical * Δt
  151. // vVertical == v * Math.sin(angleInRadians)
  152.  
  153. Gravity = 0.0004;
  154.  
  155. function vVertical(v, angle) {
  156. return v * Math.sin(radians(angle));
  157. }
  158.  
  159. function yDistance(timeElapsed, v, angle) {
  160. return -(timeElapsed * vVertical(v, angle));
  161. }
  162.  
  163. function gravityDistance(timeElapsed, g) {
  164. return 0.5 * g * (timeElapsed * timeElapsed);
  165. }
  166.  
  167. function yCurrent(yInitial, timeElapsed, v, angle) {
  168. return yInitial + yDistance(timeElapsed, v, angle) + gravityDistance(timeElapsed, Gravity);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement