HolonBody.java 4.4 KB

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