Advertisement
Guest User

GAAAAAAAYBE

a guest
Nov 24th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. public class Ball
  6. {
  7.  
  8. private Vector2 m_Velocity;
  9. private Vector2 m_m_Position;
  10.  
  11. public Ball(Vector2 m_Position)
  12. {
  13. m_m_Position = m_Position;
  14. m_Velocity = 0;
  15. }
  16.  
  17. public void Update()
  18. {
  19. m_m_Position += m_Velocity;
  20. }
  21.  
  22. public boolean colliding(Ball ball)
  23. {
  24. float xd = m_m_Position.getX() - ball.m_m_Position.getX();
  25. float yd = m_m_Position.getY() - ball.m_m_Position.getY();
  26.  
  27. float sumRadius = getRadius() + ball.getRadius();
  28. float sqrRadius = sumRadius * sumRadius;
  29.  
  30. float distSqr = (xd * xd) + (yd * yd);
  31.  
  32. if (distSqr <= sqrRadius)
  33. {
  34. return true;
  35. }
  36.  
  37. return false;
  38. }
  39.  
  40. public void resolveCollision(Ball ball)
  41. {
  42. // get the mtd
  43. Vector2d delta = (m_Position.subtract(ball.m_Position));
  44. float d = delta.getLength();
  45. // minimum translation distance to push balls apart after intersecting
  46. Vector2d mtd = delta.multiply(((getRadius() + ball.getRadius()) - d) / d);
  47.  
  48.  
  49. // resolve intersection --
  50. // inverse mass quantities
  51. float im1 = 1 / getMass();
  52. float im2 = 1 / ball.getMass();
  53.  
  54. // push-pull them apart based off their mass
  55. m_Position = m_Position.add(mtd.multiply(im1 / (im1 + im2)));
  56. ball.m_Position = ball.m_Position.subtract(mtd.multiply(im2 / (im1 + im2)));
  57.  
  58. // impact speed
  59. Vector2d v = (this.velocity.subtract(ball.velocity));
  60. float vn = v.dot(mtd.normalize());
  61.  
  62. // sphere intersecting but moving away from each other already
  63. if (vn > 0.0f) return;
  64.  
  65. // collision impulse
  66. float i = (-(1.0f + Constants.restitution) * vn) / (im1 + im2);
  67. Vector2d impulse = mtd.multiply(i);
  68.  
  69. // change in momentum
  70. this.velocity = this.velocity.add(impulse.multiply(im1));
  71. ball.velocity = ball.velocity.subtract(impulse.multiply(im2));
  72.  
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement