Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. Ball b;
  2. PVector t1;
  3. boolean grabbed;
  4. ArrayList<particle> ps;
  5. float pmx, pmy;
  6. void setup() {
  7. fullScreen(P3D);
  8. orientation(LANDSCAPE);
  9. b = new Ball();
  10. t1 = new PVector();
  11. grabbed = false;
  12. ps = new ArrayList<particle>();
  13. pmx=mouseX;
  14. pmy=mouseY;
  15. }
  16. void draw() {
  17. noStroke();
  18. beginShape();
  19. fill(202, 251, 122, 128);
  20. vertex(0, 0);
  21. fill(85, 247, 175, 128);
  22. vertex(0, height);
  23. fill(135, 235, 234, 128);
  24. vertex(width, height);
  25. fill(236, 240, 239, 128);
  26. vertex(width, 0);
  27. endShape(CLOSE);
  28. b.run();
  29. for (int i = ps.size()-1; i >=0; i--) {
  30. ps.get(i).run();
  31. if (ps.get(i).vel.mag() <= 0.1) {
  32. ps.remove(i);
  33. }
  34. }
  35. if (grabbed) {
  36. strokeWeight(10);
  37. stroke(255);
  38. line(b.loc.x, b.loc.y, mouseX, mouseY);
  39. }
  40. pmx=mouseX;
  41. pmy=mouseY;
  42. }
  43. class Ball {
  44. PVector loc, acc, vel;
  45. float size;
  46. Ball() {
  47. size = min(width, height)/4;
  48. loc = new PVector(random(size/2, width-size/2), random(size/2, height-size/2));
  49. vel = new PVector(0, 0);
  50. acc = new PVector(0, 0);
  51. }
  52. void run() {
  53. noStroke();
  54. fill(255);
  55. vel.add(acc).mult(0.98);
  56. loc.add(vel);
  57. ellipse(loc.x, loc.y, size, size);
  58. loc.x = constrain(loc.x, size/2, width-size/2);
  59. loc.y = constrain(loc.y, size/2, height-size/2);
  60. if (loc.x <= size/2 || loc.x >= width-size/2) {
  61. vel.x *= -1;
  62. }
  63. if (loc.y <= size/2 || loc.y >= height-size/2) {
  64. vel.y *= -1;
  65. }
  66. acc.mult(0);
  67. }
  68. }
  69. void mousePressed() {
  70. if (dist(mouseX, mouseY, b.loc.x, b.loc.y) < b.size/2) {
  71. t1 = new PVector(mouseX, mouseY);
  72. grabbed = true;
  73. }
  74. }
  75. void mouseReleased() {
  76. if (grabbed) {
  77. b.acc = new PVector(mouseX, mouseY).sub(t1).div(8);
  78. grabbed = false;
  79. }
  80. }
  81. void mouseDragged() {
  82. if (grabbed) {
  83. t1=b.loc.copy();
  84. ps.add(new particle(new PVector(mouseX, mouseY).sub(new PVector(pmx, pmy))));
  85. }
  86. }
  87. class particle {
  88. PVector loc, vel;
  89. particle(PVector v) {
  90. loc = new PVector(mouseX, mouseY);
  91. vel = v.copy();
  92. }
  93. void run() {
  94. vel.mult(0.9);
  95. loc.add(vel);
  96. fill(255);
  97. noStroke();
  98. ellipse(loc.x, loc.y, vel.mag(), vel.mag());
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement