HolonBody.java 3.6 KB

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