HolonBody.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package classes;
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. public class HolonBody implements Comparable<HolonBody> {
  5. public Vector2d velocity;
  6. public Vector2d position;
  7. private float mass;
  8. private float radius;
  9. private Color color;
  10. public Color getColor() {
  11. return color;
  12. }
  13. public HolonBody(float x, float y, float radius, float mass, Color color) {
  14. this.velocity = new Vector2d(0, 0);
  15. this.position = new Vector2d(x, y);
  16. this.setMass(mass);
  17. this.setRadius(radius);
  18. this.color = color;
  19. }
  20. public void draw(Graphics2D g2, int holonBodyScale) {
  21. g2.setColor(color);
  22. g2.fillOval((int) (position.getX() - getRadius()), (int) (position.getY() - getRadius()),
  23. (int) (2 * getRadius()), (int) (2 * getRadius()));
  24. }
  25. public void setRadius(float radius) {
  26. this.radius = radius;
  27. }
  28. public float getRadius() {
  29. return radius;
  30. }
  31. public boolean colliding(HolonBody body) {
  32. float xd = position.getX() - body.position.getX();
  33. float yd = position.getY() - body.position.getY();
  34. float sumRadius = getRadius() + body.getRadius();
  35. float sqrRadius = sumRadius * sumRadius;
  36. float distSqr = (xd * xd) + (yd * yd);
  37. if (distSqr <= sqrRadius) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. public void resolveCollision(HolonBody body) {
  43. // get the mtd
  44. Vector2d delta = (position.subtract(body.position));
  45. float r = getRadius() + body.getRadius();
  46. float dist2 = delta.dot(delta);
  47. if (dist2 > r * r)
  48. return; // they aren't colliding
  49. float d = delta.getLength();
  50. Vector2d mtd;
  51. if (d != 0.0f) {
  52. // minimum translation distance to push bodies apart after
  53. // intersecting
  54. mtd = delta.multiply(((getRadius() + body.getRadius()) - d) / d);
  55. } else {
  56. // Special case. Bodies are exactly on top of eachother. Don't want
  57. // to divide by zero.
  58. d = body.getRadius() + getRadius() - 1.0f;
  59. delta = new Vector2d(body.getRadius() + getRadius(), 0.0f);
  60. mtd = delta.multiply(((getRadius() + body.getRadius()) - d) / d);
  61. }
  62. // resolve intersection
  63. float im1 = 1 / getMass(); // inverse mass quantities
  64. float im2 = 1 / body.getMass();
  65. // push-pull them apart
  66. position = position.add(mtd.multiply(im1 / (im1 + im2)));
  67. body.position = body.position.subtract(mtd.multiply(im2 / (im1 + im2)));
  68. // impact speed
  69. Vector2d v = (this.velocity.subtract(body.velocity));
  70. float vn = v.dot(mtd.normalize());
  71. // sphere intersecting but moving away from each other already
  72. if (vn > 0.0f)
  73. return;
  74. // collision impulse
  75. float i = (-(1.0f + Constants.restitution) * vn) / (im1 + im2);
  76. Vector2d impulse = mtd.multiply(i);
  77. // change in momentum
  78. this.velocity = this.velocity.add(impulse.multiply(im1));
  79. body.velocity = body.velocity.subtract(impulse.multiply(im2));
  80. }
  81. public void setMass(float mass) {
  82. this.mass = mass;
  83. }
  84. private float getMass() {
  85. return mass;
  86. }
  87. public int compareTo(HolonBody body) {
  88. if (this.position.getX() - this.getRadius() > body.position.getX() - body.getRadius()) {
  89. return 1;
  90. } else if (this.position.getX() - this.getRadius() < body.position.getX() - body.getRadius()) {
  91. return -1;
  92. } else {
  93. return 0;
  94. }
  95. }
  96. }