Advertisement
Guest User

Untitled

a guest
May 21st, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. /**
  2. * Bouncy Bubbles
  3. * based on code from Keith Peters.
  4. *
  5. * Multiple-object collision.
  6. */
  7.  
  8. int numBalls = 1;//numarul de balls
  9. float spring = 0.05;//arc
  10. float gravity = 0.03;//coeficient la gravity
  11. float friction = -0.9;//coeficient de a se indeparta in parte opusa dupa ciocnire
  12. Ball[] balls = new Ball[numBalls];//creaza un array de obiecte de tip ball din numBalls
  13.  
  14. void setup() {
  15. size(300, 300);//size la screen
  16. for (int i = 0; i < numBalls; i++) {//initializeaza mingile
  17. balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls);//initialieaza obiectul in constructor
  18. }
  19. noStroke();//disable la stroke la balls
  20. fill(255, 210);//fill in interiorul la balls
  21. }
  22.  
  23. void draw() {
  24. background(0);//background negru
  25. for (Ball ball : balls) {//for (datatype element : array)
  26. ball.collide();
  27. ball.move();
  28. ball.display();
  29. }
  30. }
  31.  
  32. class Ball {
  33.  
  34. float x, y;
  35. float diameter;
  36. float vx = 0;
  37. float vy = 0;
  38. int id;
  39. Ball[] others;
  40.  
  41. Ball(float xin, float yin, float din, int idin, Ball[] oin) {
  42. x = xin;//coordonata x
  43. y = yin;//cordonata y
  44. diameter = din;//diametru
  45. id = idin;//id la balls
  46. others = oin;//obiect de tip balls
  47. }
  48.  
  49. void collide() {
  50. for (int i = id + 1; i < numBalls; i++) {
  51. float dx = others[i].x - x;//distanta dintra obiecte pe coordonata x
  52. float dy = others[i].y - y;// distanta dintre obiecte pe coordonata y
  53. float distance = sqrt(dx*dx + dy*dy);//formula de calcul dinstanta dintre obiecte
  54. float minDist = others[i].diameter/2 + diameter/2;//mindist- distanta mininam de ciocnire dintre doua obiecte pentru a avea inteactiune
  55. if (distance < minDist) { //daca distanta minima de ciocnire dintre obiecte este mai mare decit distanta dintre obiecte
  56. float angle = atan2(dy, dx);//Calculates the angle (in radians) from a specified point to the coordinate origin as measured from the positive x-axis
  57. float targetX = x + cos(angle) * minDist;//punctul de ciocnire
  58. float targetY = y + sin(angle) * minDist;//punctul de ciocnire
  59. float ax = (targetX - others[i].x) * spring;//
  60. float ay = (targetY - others[i].y) * spring;
  61. vx -= ax;
  62. vy -= ay;
  63. others[i].vx += ax;
  64. others[i].vy += ay;
  65. }
  66. }
  67. }
  68.  
  69. void move() {
  70. int dir=(int)random(0,4);
  71. if(dir==0)
  72. vy -= gravity;
  73. else if(dir==1)
  74. vy += gravity;
  75. else if(dir==2)
  76. vx+=gravity;
  77. else if(dir==3)
  78. vx-=gravity;
  79.  
  80. x += vx;
  81. y += vy;
  82. if (x + diameter/2 > width) {
  83. x = width - diameter/2;
  84. vx *= friction;
  85. }
  86. else if (x - diameter/2 < 0) {
  87. x = diameter/2;
  88. vx *= friction;
  89. }
  90. if (y + diameter/2 > height) {
  91. y = height - diameter/2;
  92. vy *= friction;
  93. }
  94. else if (y - diameter/2 < 0) {
  95. y = diameter/2;
  96. vy *= friction;
  97. }
  98. }
  99.  
  100. void display() {
  101. ellipse(x, y, diameter, diameter);//creaza cerc cu parametri
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement