HolonBody.java 3.5 KB

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