AABB.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package eu.tankernn.gameEngine.loader.models;
  2. import org.lwjgl.util.vector.Vector3f;
  3. import eu.tankernn.gameEngine.loader.models.obj.ModelData;
  4. public class AABB {
  5. protected Vector3f position;
  6. private final Vector3f halfSize, relativeMiddlePos, relativeLb, relativeRt;
  7. public AABB(Vector3f relativeMiddlePos, Vector3f halfSize) {
  8. this.relativeMiddlePos = relativeMiddlePos;
  9. this.halfSize = halfSize;
  10. relativeLb = Vector3f.sub(relativeMiddlePos, halfSize, null);
  11. relativeRt = Vector3f.add(relativeMiddlePos, halfSize, null);
  12. }
  13. public AABB(ModelData model) {
  14. Vector3f max = new Vector3f(), min = new Vector3f();
  15. float[] vertexArray = model.getVertices();
  16. for (int i = 0; i < vertexArray.length; i += 3) {
  17. if (vertexArray[i] > max.x)
  18. max.setX(vertexArray[i]);
  19. if (vertexArray[i] < min.x)
  20. min.setX(vertexArray[i]);
  21. }
  22. for (int i = 1; i < vertexArray.length; i += 3) {
  23. if (vertexArray[i] > max.y)
  24. max.setY(vertexArray[i]);
  25. if (vertexArray[i] < min.y)
  26. min.setY(vertexArray[i]);
  27. }
  28. for (int i = 2; i < vertexArray.length; i += 3) {
  29. if (vertexArray[i] > max.z)
  30. max.setZ(vertexArray[i]);
  31. if (vertexArray[i] < min.z)
  32. min.setZ(vertexArray[i]);
  33. }
  34. Vector3f fullSize = Vector3f.sub(max, min, null);
  35. this.halfSize = new Vector3f(fullSize.x / 2, fullSize.y / 2, fullSize.z / 2);
  36. relativeLb = min;
  37. relativeRt = max;
  38. this.relativeMiddlePos = Vector3f.add(relativeLb, relativeRt, null);
  39. this.relativeMiddlePos.x /= 2;
  40. this.relativeMiddlePos.y /= 2;
  41. this.relativeMiddlePos.z /= 2;
  42. }
  43. public AABB updatePosition(Vector3f pos) {
  44. this.position = pos;
  45. return this;
  46. }
  47. public static boolean collides(AABB a, AABB b) {
  48. if (Math.abs(a.getMiddlePos().x - b.getMiddlePos().x) < a.halfSize.x + b.halfSize.x) {
  49. if (Math.abs(a.getMiddlePos().y - b.getMiddlePos().y) < a.halfSize.y + b.halfSize.y) {
  50. if (Math.abs(a.getMiddlePos().z - b.getMiddlePos().z) < a.halfSize.z + b.halfSize.z) {
  51. return true;
  52. }
  53. }
  54. }
  55. return false;
  56. }
  57. public static boolean inside(AABB a, Vector3f b) {
  58. if (Math.abs(a.getMiddlePos().x - b.x) < a.halfSize.x) {
  59. if (Math.abs(a.getMiddlePos().y - b.y) < a.halfSize.y) {
  60. if (Math.abs(a.getMiddlePos().z - b.z) < a.halfSize.z) {
  61. return true;
  62. }
  63. }
  64. }
  65. return false;
  66. }
  67. public Vector3f getLb() {
  68. return Vector3f.add(position, relativeLb, null);
  69. }
  70. public Vector3f getRt() {
  71. return Vector3f.add(position, relativeRt, null);
  72. }
  73. public Vector3f getMiddlePos() {
  74. return Vector3f.add(position, relativeMiddlePos, null);
  75. }
  76. public Vector3f getSize() {
  77. return (Vector3f) new Vector3f(halfSize).scale(2);
  78. }
  79. public AABB copy() {
  80. return new AABB(new Vector3f(this.relativeMiddlePos), new Vector3f(this.halfSize));
  81. }
  82. }