HolonBody.java 3.2 KB

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