Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. "use strict";
  2.  
  3. var coRestitution = 0.018;
  4. var coFriction = 0.01;
  5.  
  6. class Particle {
  7. constructor(x, y) {
  8. this.pos = createVector(x, y);
  9. this.vel = createVector(random(-10, 10), random(-10, 10));
  10. this.acc = createVector(0, 0);
  11.  
  12. this.targetPos = createVector(width / 2, height / 2);
  13. this.lerpSpeed = random(0.05, 0.1);
  14. //255, 120, 20, 70
  15. this.r = 200; //random(255);
  16. this.g = 120; //random(255);
  17. this.b = 20; //random(255);
  18.  
  19. this.rad = random(2, 3.5);
  20. }
  21.  
  22. moveWithLerp() {
  23. this.pos = p5.Vector.lerp(this.pos, this.targetPos, this.lerpSpeed);
  24. }
  25.  
  26. applyForce(f) {
  27. this.acc.add(f)
  28. }
  29.  
  30. update() {
  31. this.vel.add(this.acc);
  32. this.pos.add(this.vel);
  33. this.acc.mult(0);
  34. }
  35.  
  36. display() {
  37. push();
  38. translate(this.pos.x, this.pos.y);
  39. noStroke();
  40. //stroke(0);
  41. //strokeWeight(1);
  42. fill(this.r, this.g, this.b);
  43. blendMode(ADD);
  44. ellipse(0, 0, this.rad * 2, this.rad * 2);
  45. pop();
  46. }
  47.  
  48. checkBoundaries() {
  49. var force = createVector(0, 0);
  50. //x
  51. if (this.pos.x < 0 + this.rad) {
  52. this.pos.x = 0 + this.rad;
  53. this.vel.x = -this.vel.x * 0.3;
  54. } else if (this.pos.x > width - this.rad) {
  55. this.pos.x = width - this.rad;
  56. this.vel.x = -this.vel.x * 0.3;
  57. }
  58. // y
  59. if (this.pos.y < 0 + this.rad) {
  60. this.pos.y = 0 + this.rad;
  61. this.vel.y = -this.vel.y * 0.3;
  62. } else if (this.pos.y > height - this.rad) {
  63. this.pos.y = height - this.rad;
  64. this.vel.y = -this.vel.y * 0.3;
  65. }
  66. }
  67.  
  68. /*
  69. checkCollision(other) {
  70. var distance = this.pos.dist(other.pos);
  71. if (distance < this.rad + other.rad) {
  72. // this
  73. var force = p5.Vector.sub(other.pos, this.pos);
  74. force.normalize();
  75. force.mult(-1);
  76. force.mult(other.vel.mag());
  77. this.applyForce(force);
  78. this.vel.mult(coRestitution);
  79.  
  80. var friction = p5.Vector.mult(this.vel, -1);
  81. friction.normalize();
  82. friction.mult(coFriction);
  83. friction.limit(this.vel.mag());
  84. other.applyForce(friction);
  85.  
  86. // other
  87. var force = p5.Vector.sub(this.pos, other.pos);
  88. force.normalize();
  89. force.mult(-1);
  90. force.mult(this.vel.mag());
  91. other.applyForce(force);
  92. other.vel.mult(coRestitution);
  93.  
  94. var friction = p5.Vector.mult(other.vel, -1);
  95. friction.normalize();
  96. friction.mult(coFriction);
  97. friction.limit(other.vel.mag());
  98. other.applyForce(friction);
  99. }
  100. }
  101. */
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement