fork(1) download
  1. class Ball {
  2. PVector pos;
  3. PVector vel;
  4. float r;
  5. float d;
  6. color col;
  7. float mass;
  8. boolean isDead = false;
  9. ArrayList<Ball> others;
  10.  
  11. Ball(float r, float x, float y, PVector vel, color col, ArrayList<Ball> others) {
  12. this.r = r;
  13. this.d = 2*r;
  14. this.mass = 10*sqrt(r * r);//r*r//3.21
  15. this.pos = new PVector(x, y);
  16. this.vel = vel;
  17. this.col = col;
  18. this.others = others;
  19. }
  20. void move() {
  21. vel.mult(reduction);
  22. pos.add(vel);
  23. vel.y += gravity;
  24. }
  25.  
  26. void bound() {
  27. if (pos.y + r > height) {
  28. vel.y = -vel.y;
  29. pos.y = height - r;
  30. }
  31. if (pos.y - r < 0) {
  32. vel.y = -vel.y;
  33. pos.y = r;
  34. }
  35. if (pos.x + r > width) {
  36. vel.x = -vel.x;
  37. pos.x = width - r;
  38. }
  39. if (pos.x - r < 0) {
  40. vel.x = -vel.x;
  41. pos.x = r;
  42. }
  43. }
  44. float x() {
  45. return pos.x;
  46. }
  47. float y() {
  48. return pos.y;
  49. }
  50. float d() {
  51. return d;
  52. }
  53. float r() {
  54. return r;
  55. }
  56. void display() {
  57. noStroke();
  58. fill(col);
  59. ellipse(x(), y(), d(), d());
  60. }
  61. void accelaration(PVector delta, float force) {
  62. PVector accel = new PVector(delta.x, delta.y);
  63. accel.mult(force/mass);
  64. vel.add(accel);
  65. }
  66. void deadSmaller(Ball other) {
  67. if (this.d() < other.d()) {
  68. this.dead();
  69. } else if (other.d() < this.d()) {
  70. other.dead();
  71. }
  72. }
  73. void collide() {
  74. for (Ball other : others) {
  75. if (other.isDead())continue;
  76. if (this == other)continue;
  77.  
  78. PVector delta = new PVector(other.x()-x(), other.y()-y());
  79. float rr = other.r() + r();
  80.  
  81. if (delta.mag() < rr) {
  82. float force = spring * (rr - delta.mag());
  83. delta.normalize();
  84. this.accelaration(delta, -force);
  85. other.accelaration(delta, force);
  86. deadSmaller(other);
  87. }
  88. }
  89. }
  90. void dead() {
  91. isDead = true;
  92. }
  93. boolean isDead() {
  94. return isDead;
  95. }
  96. boolean isAlive() {
  97. return !isDead();
  98. }
  99. }
  100.  
  101.  
  102.  
  103. int maxBall = 50;
  104. ArrayList<Ball> balls;
  105.  
  106. float spring = 100;
  107. float reduction = 0.9991;
  108. float gravity = 0.01;
  109.  
  110. void setup() {
  111. size(480, 640);
  112. colorMode(HSB, 360, 100, 100, 100);
  113. smooth();
  114. frameRate(30);
  115.  
  116. balls = new ArrayList<Ball>();
  117. for (int i = 0; i < maxBall; i++) {
  118. addBall();
  119. }
  120. }
  121. void addBall() {
  122. float r = random(8, 12);////2, 4
  123. float x = random(r, width-r);
  124. float y = random(r, height-r);
  125. PVector vel = new PVector(1, 0);
  126. vel.mult(random(5, 10));
  127. vel.rotate(random(TWO_PI));
  128. color c = color(random(360), 100, 100);
  129. balls.add(new Ball(r, x, y, vel, c, balls));
  130. }
  131. void draw() {
  132. background(0, 0, 100);
  133.  
  134. //fill(0, 0, 100, 40);
  135. //rect(0, 0, width, height);
  136.  
  137. for (Ball ball : balls) {
  138. ball.display();
  139. ball.move();
  140. ball.bound();
  141. ball.collide();
  142. }
  143.  
  144. for (int i=balls.size()-1; i>=0; i--) {
  145. if (balls.get(i).isDead()) {
  146. balls.remove(i);
  147. }
  148. }
  149. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty