HolonBody.java 4.4 KB

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