AABB.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 void updatePosition(Vector3f pos) {
  44. this.position = pos;
  45. }
  46. public static boolean collides(AABB a, AABB b) {
  47. if (Math.abs(a.getMiddlePos().x - b.getMiddlePos().x) < a.halfSize.x + b.halfSize.x) {
  48. if (Math.abs(a.getMiddlePos().y - b.getMiddlePos().y) < a.halfSize.y + b.halfSize.y) {
  49. if (Math.abs(a.getMiddlePos().z - b.getMiddlePos().z) < a.halfSize.z + b.halfSize.z) {
  50. return true;
  51. }
  52. }
  53. }
  54. return false;
  55. }
  56. public static boolean inside(AABB a, Vector3f b) {
  57. if (Math.abs(a.getMiddlePos().x - b.x) < a.halfSize.x) {
  58. if (Math.abs(a.getMiddlePos().y - b.y) < a.halfSize.y) {
  59. if (Math.abs(a.getMiddlePos().z - b.z) < a.halfSize.z) {
  60. return true;
  61. }
  62. }
  63. }
  64. return false;
  65. }
  66. public Vector3f getLb() {
  67. return Vector3f.add(position, relativeLb, null);
  68. }
  69. public Vector3f getRt() {
  70. return Vector3f.add(position, relativeRt, null);
  71. }
  72. public Vector3f getMiddlePos() {
  73. return Vector3f.add(position, relativeMiddlePos, null);
  74. }
  75. public AABB copy() {
  76. return new AABB(new Vector3f(this.relativeMiddlePos), new Vector3f(this.halfSize));
  77. }
  78. }