Legebatterie

p5js--PI-blocks

Mar 28th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. //change masses,speed and framerate
  2. var digits = 2;
  3. var s=-2;
  4. var extraframes = 1;
  5.  
  6.  
  7. counter=0;
  8.  
  9.  
  10. class Block {
  11.  
  12. constructor(x, s, m, v) {
  13.  
  14. this.pos = x;
  15. this.mass = m;
  16. this.size = s;
  17. this.vel = v;
  18. }
  19.  
  20. show() {
  21. fill(0, 0, 150)
  22. square(this.pos, height - this.size, this.size);
  23. textAlign(RIGHT)
  24. fill(0)
  25.  
  26. text(counter / pow(10, digits - 1), width, 100)
  27. }
  28.  
  29. move() {
  30. this.pos = this.pos + this.vel;
  31. }
  32. bounce() {
  33. if (this.pos < 0) {
  34. counter++
  35. this.vel *= -1;
  36.  
  37. }
  38. }
  39. collide(other) {
  40. if (this.pos + this.size > other.pos) {
  41. counter++;
  42. this.temp = this.vel;
  43.  
  44. this.sum1 = (this.mass - other.mass) / (this.mass + other.mass) * this.vel
  45. this.sum2 = (2 * other.mass) / (this.mass + other.mass) * other.vel
  46. this.vel = this.sum1 + this.sum2
  47.  
  48. this.sum1 = (2 * this.mass) / (this.mass + other.mass) * this.temp
  49. this.sum2 = (other.mass - this.mass) / (this.mass + other.mass) * other.vel
  50. other.vel = this.sum1 + this.sum2
  51.  
  52. }
  53.  
  54.  
  55. }
  56.  
  57. finished(that){
  58. if(this.vel>that.vel && this.vel>0){
  59. noLoop()
  60. print("finsihed!")
  61. }
  62. }
  63.  
  64. }
  65.  
  66.  
  67.  
  68. function setup() {
  69. frameRate(60);
  70.  
  71. createCanvas(600, 300);
  72. //x,s,m,v
  73. b1 = new Block(width / 2, 100, pow(100, (digits - 1)), s/extraframes);
  74. b2 = new Block(width / 5, 33, 1, 0);
  75.  
  76. }
  77.  
  78. function draw() {
  79. background(33, 40, 40);
  80. for (let j = 0; j < extraframes; j++) {
  81.  
  82. b1.move();
  83. b2.move();
  84. b1.bounce();
  85. b2.bounce();
  86. b2.collide(b1);
  87. //b1.finished(b2);
  88. }
  89. b1.show();
  90. b2.show();
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment