HolonBody.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. this.radius = radius;
  35. }
  36. public float getRadius() {
  37. return radius;
  38. }
  39. public boolean colliding(HolonBody body) {
  40. float xd = position.getX() - body.position.getX();
  41. float yd = position.getY() - body.position.getY();
  42. float sumRadius = getRadius() + body.getRadius();
  43. float sqrRadius = sumRadius * sumRadius;
  44. float distSqr = (xd * xd) + (yd * yd);
  45. if (distSqr <= sqrRadius) {
  46. return true;
  47. }
  48. return false;
  49. }
  50. public void resolveCollision(HolonBody body) {
  51. // get the mtd
  52. Vector2d delta = (position.subtract(body.position));
  53. float r = getRadius() + body.getRadius();
  54. float dist2 = delta.dot(delta);
  55. if (dist2 > r * r)
  56. return; // they aren't colliding
  57. float d = delta.getLength();
  58. Vector2d mtd;
  59. if (d != 0.0f) {
  60. // minimum translation distance to push bodies apart after
  61. // intersecting
  62. mtd = delta.multiply(((getRadius() + body.getRadius()) - d) / d);
  63. } else {
  64. // Special case. Bodies are exactly on top of eachother. Don't want
  65. // to divide by zero.
  66. d = body.getRadius() + getRadius() - 1.0f;
  67. delta = new Vector2d(body.getRadius() + getRadius(), 0.0f);
  68. mtd = delta.multiply(((getRadius() + body.getRadius()) - d) / d);
  69. }
  70. // resolve intersection
  71. float im1 = 1 / getMass(); // inverse mass quantities
  72. float im2 = 1 / body.getMass();
  73. // push-pull them apart
  74. position = position.add(mtd.multiply(im1 / (im1 + im2)));
  75. body.position = body.position.subtract(mtd.multiply(im2 / (im1 + im2)));
  76. // impact speed
  77. Vector2d v = (this.velocity.subtract(body.velocity));
  78. float vn = v.dot(mtd.normalize());
  79. // sphere intersecting but moving away from each other already
  80. if (vn > 0.0f)
  81. return;
  82. // collision impulse
  83. float i = (-(1.0f + Constants.restitution) * vn) / (im1 + im2);
  84. Vector2d impulse = mtd.multiply(i);
  85. // change in momentum
  86. this.velocity = this.velocity.add(impulse.multiply(im1));
  87. body.velocity = body.velocity.subtract(impulse.multiply(im2));
  88. }
  89. public void setMass(float mass) {
  90. this.mass = mass;
  91. }
  92. private float getMass() {
  93. return mass;
  94. }
  95. public int compareTo(HolonBody body) {
  96. if (this.position.getX() - this.getRadius() > body.position.getX() - body.getRadius()) {
  97. return 1;
  98. } else if (this.position.getX() - this.getRadius() < body.position.getX() - body.getRadius()) {
  99. return -1;
  100. } else {
  101. return 0;
  102. }
  103. }
  104. }