Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. class Ball {
  2.  
  3. ArrayList<Ball> others;
  4. PVector pos, vel, acc, siz;
  5. PImage img;
  6. color col = color(255, 0, 0);
  7. float startSpeed = 5;
  8.  
  9. float maxSpeed = 10;
  10. int posMode = CENTER;
  11. boolean gravity = false;
  12. boolean bounded = true;
  13. boolean selfReflect = true;
  14. boolean speedEnforced = true;
  15. boolean offScreen = false;
  16.  
  17. public Ball() {
  18. pos = new PVector(width/2, height/2);
  19. vel = new PVector(-5, 5);
  20. siz = new PVector(40, 40);
  21. vel.limit(startSpeed);
  22. }
  23.  
  24. public Ball(PVector start) {
  25. pos = start;
  26. vel = new PVector(-5, 5);
  27. siz = new PVector(40, 40);
  28. vel.limit(startSpeed);
  29. }
  30.  
  31. public Ball(PVector start, PVector startVel) {
  32. pos = start;
  33. vel = startVel;
  34. siz = new PVector(40, 40);
  35. vel.limit(startSpeed);
  36. }
  37. public Ball(PVector start, PVector startVel, color _col) {
  38. pos = start;
  39. vel = startVel;
  40. siz = new PVector(40, 40);
  41. vel.limit(startSpeed);
  42. col = _col;
  43. }
  44.  
  45. public Ball(ArrayList<Ball> _others, PVector start, PVector startVel, color _col) {
  46. others = _others;
  47. pos = start;
  48. vel = startVel;
  49. siz = new PVector(40, 40);
  50. vel.limit(startSpeed);
  51. col = _col;
  52. }
  53.  
  54. void update() {
  55. //vel.add(acc);
  56. pos.add(vel);
  57. }
  58.  
  59. void check() {
  60. if (speedEnforced) {
  61. vel.limit(maxSpeed);
  62. }
  63. if (bounded) {
  64. if (abs(pos.x - width/2)>width/2-siz.x/2) {
  65. //undo last step
  66. pos.sub(vel);
  67. vel.x*=-1;
  68. pos.add(vel);
  69. }
  70. if (abs(pos.y - height/2)>height/2-siz.y/2) {
  71. //undo last step
  72. pos.sub(vel);
  73. vel.y*=-1;
  74. pos.add(vel);
  75. }
  76. }
  77. else{
  78. //if the ball is not bounded by the screen and it leaves far enough, remove it
  79. offScreen = (abs(pos.x - width/2)>width/2-siz.x/2)&&(abs(pos.y - height/2)>height/2-siz.y/2);
  80. //Move responibility to removal to outside of class for safety
  81. //if (offScreen &&others != null){
  82. // others.remove(this);
  83. //}
  84.  
  85. }
  86. //check other balls
  87. if (selfReflect && others != null) {
  88. for (Ball b : others) {
  89. if (b != this) {//don't let it bounce with it self
  90. if (pos.dist(b.pos)< (siz.x+b.siz.x)/2) {
  91. PVector normal = PVector.sub(b.pos, pos);
  92. float theta = PVector.angleBetween(vel, normal);
  93. //draw an effect at the point of impact
  94. //hitEffectPos = temp;
  95. //starting size
  96. //hitEffectSize = 5;
  97. //remove the last step (?)
  98. //bPos.sub(bVel);
  99. //apply the angle of reflection
  100. vel.rotate(-2*theta);
  101. pos.add(vel);
  102. theta = PVector.angleBetween( normal,b.vel);
  103. b.vel.rotate(2*theta);
  104. b.pos.add(b.vel);
  105. //move the
  106.  
  107. }
  108. }
  109. }
  110. }
  111. }
  112.  
  113. void draw() {
  114. fill(col);
  115. ellipse(pos.x, pos.y, siz.x, siz.y);
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement