Advertisement
Guest User

Untitled

a guest
May 27th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. import java.awt.geom.Ellipse2D;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4.  
  5. public class DummyModel implements IBouncingBallsModel {
  6.  
  7. private final double areaWidth;
  8. private final double areaHeight;
  9.  
  10. private Ball small, large;
  11.  
  12. public DummyModel(double width, double height) {
  13. this.areaWidth = width;
  14. this.areaHeight = height;
  15. large = new Ball(2, 6, 3, 7, 1.8, 1.8);
  16. small = new Ball(8, 4.3, -2, 2, 0.4, 0.4);
  17. }
  18.  
  19. public boolean hasCollided(Ball b1, Ball b2){
  20. // Calculate the relationship to ensure they are moving towards each other
  21. double xDist = b1.x - b2.x;
  22. double yDist = b1.y - b2.y;
  23. double xVelocity = b2.vx - b1.vx;
  24. double yVelocity = b2.vy - b1.vy;
  25. /* With these variables we can calculate a relationship
  26. relationship = (x1 - x2)(vx2 - vx1) + (y1 - y2)(vy2 - vy1)
  27. (1) (2) (3) (4)
  28. (1) + Ball 1 is to the right of ball 2
  29. - Ball 2 is to the right of ball 1
  30. (2) + Ball 2 is faster than ball 1 (in the x direction)
  31. - Ball 1 is faster than ball 2 (in the x direction)
  32. (3) + Ball 1 is above ball 2
  33. - Ball 2 is above ball 1
  34. (4) + Ball 2 is faster than ball 1 (in the y direction)
  35. - Ball 1 is faster than ball 2 (in the Y direction)
  36. This tells us that if relationship is smaller than or equal to zero there are three possible scenarios:
  37. 1. The balls are moving away in both x and y direction.
  38. 2. One ball is moving towards the other in x direction but not in the y direction and the velocity in y is larger than or equal to x
  39. 3. One ball is moving towards the other in y direction but not in the x direction and the velocity in x is larger than or equal to y
  40. In these scenarios the balls are not moving towards each other.
  41. */
  42. double relationship = xDist*xVelocity + yDist*yVelocity;
  43. boolean movingTowardsEachOther = relationship > 0;
  44.  
  45. // Calculate if they are touching each other
  46. double ballDistance = getPolarR(b2.x - b1.x, b2.y - b1.y);
  47. boolean intersecting = ballDistance <= b1.r + b2.r;
  48.  
  49. // They can only be considered collided if they are both touching each other and are moving towards each other
  50. return intersecting && movingTowardsEachOther;
  51. }
  52.  
  53. public void collisionConvert(Ball b1, Ball b2){
  54. // Calculate variables needed for the calculations
  55. double pi = Math.PI;
  56. double m1 = b1.weight, m2 = b2.weight, u1x = b1.vx, u1y = b1.vy, u2x = b2.vx, u2y = b2.vy;
  57. double phi = getPolarV(b2.x - b1.x, b2.y - b1.y);
  58. double theta1 = getPolarV(u1x, u1y), theta2 = getPolarV(u2x, u2y);
  59. double u1 = getPolarR(u1x, u1y), u2 = getPolarR(u2x, u2y);
  60.  
  61. // Calculate the rotated velocities
  62. double u1r = u1 * Math.cos(theta1 - phi);
  63. double u2r = u2 * Math.cos(theta2 - phi);
  64.  
  65. // Calculate the new velocities
  66. double v1 = (u1r * (m1 - m2) + 2 * m2 * u2r) / (m1 + m2);
  67. double v2 = (u2r * (m2 - m1) + 2 * m1 * u1r) / (m1 + m2);
  68.  
  69. // Divide the new velocities in x and y and add the rotated original velocity
  70. // (the "remaining" velocity not included in the collision is added)
  71. b1.vx = v1 * Math.cos(phi) + u1 * Math.sin(theta1 - phi) * Math.cos(phi + pi / 2);
  72. b1.vy = v1 * Math.sin(phi) + u1 * Math.sin(theta1 - phi) * Math.sin(phi + pi / 2);
  73. b2.vx = v2 * Math.cos(phi) + u2 * Math.sin(theta2 - phi) * Math.cos(phi + pi / 2);
  74. b2.vy = v2 * Math.sin(phi) + u2 * Math.sin(theta2 - phi) * Math.sin(phi + pi / 2);
  75. }
  76.  
  77. @Override
  78. public void tick(double deltaT) {
  79. ballTick(large, deltaT);
  80. ballTick(small, deltaT);
  81. if(hasCollided(small, large)) {
  82. collisionConvert(small, large);
  83. small.onGround = false;
  84. large.onGround = false;
  85. }
  86. }
  87.  
  88. public boolean wallCollisionX(Ball ball){
  89. return ball.x <= ball.r || ball.x >= areaWidth - ball.r;
  90. }
  91.  
  92. public boolean wallCollisionY(Ball ball){
  93. return ball.y <= ball.r || ball.y >= areaHeight - ball.r;
  94. }
  95.  
  96. void ballTick(Ball ball, double deltaT){
  97. if(wallCollisionX(ball)){
  98. ball.vx *= -1;
  99. }
  100. if(wallCollisionY(ball)){
  101. ball.vy *= -1;
  102.  
  103. if(ball.onGround){
  104. ball.vy = 0;
  105. ball.y = ball.r;
  106. }
  107. ball.onGround = true;
  108. }else {
  109. ball.onGround = false;
  110. }
  111. if(!(ball.vy == 0 && ball.y == ball.r))
  112. ball.vy -= 9.82 * deltaT;
  113.  
  114. ball.x += ball.vx * deltaT;
  115. ball.y += ball.vy * deltaT / 2;
  116. }
  117.  
  118. public double getPolarR(double x, double y){
  119. return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  120. }
  121.  
  122. public double getPolarV(double x, double y){
  123. return Math.atan2(y, x);
  124. }
  125.  
  126. @Override
  127. public List<Ellipse2D> getBalls() {
  128. List<Ellipse2D> myBalls = new LinkedList<Ellipse2D>();
  129. large = new Ball(large);
  130. small = new Ball(small);
  131. myBalls.add(large);
  132. myBalls.add(small);
  133. return myBalls;
  134. }
  135.  
  136. private class Ball extends Ellipse2D.Double{
  137. double x, y, vx, vy, r, weight;
  138. boolean onGround;
  139.  
  140. Ball(double x, double y, double vx, double vy, double r, double weight){
  141. super(x - r, y - r, 2 * r, 2 * r);
  142. this.x = x;
  143. this.y = y;
  144. this.vx = vx;
  145. this.vy = vy;
  146. this.r = r;
  147. this.weight = weight;
  148. this.onGround = false;
  149. }
  150.  
  151. Ball(Ball ball){
  152. super(ball.x - ball.r, ball.y - ball.r, 2 * ball.r, 2 * ball.r);
  153. this.x = ball.x;
  154. this.y = ball.y;
  155. this.vx = ball.vx;
  156. this.vy = ball.vy;
  157. this.r = ball.r;
  158. this.weight = ball.weight;
  159. this.onGround = ball.onGround;
  160. }
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement