1 package com.irtimaled.bbor.common.models;
3 import com.irtimaled.bbor.client.RenderCulling;
4 import com.irtimaled.bbor.client.renderers.AbstractRenderer;
5 import com.irtimaled.bbor.client.renderers.CuboidRenderer;
6 import com.irtimaled.bbor.common.BoundingBoxType;
7 import com.irtimaled.bbor.common.MathHelper;
8 import com.irtimaled.bbor.common.TypeHelper;
9 import com.irtimaled.bbor.common.interop.CommonInterop;
11 public class BoundingBoxCuboid extends AbstractBoundingBox {
12 private static final AbstractRenderer<BoundingBoxCuboid> RENDERER = CommonInterop.registerRenderer(BoundingBoxCuboid.class, () -> new CuboidRenderer());
14 private final Coords minCoords;
15 private final Coords maxCoords;
17 protected BoundingBoxCuboid(Coords minCoords, Coords maxCoords, BoundingBoxType type) {
19 this.minCoords = minCoords;
20 this.maxCoords = maxCoords;
23 public static BoundingBoxCuboid from(Coords minCoords, Coords maxCoords, BoundingBoxType type) {
24 return new BoundingBoxCuboid(minCoords, maxCoords, type);
28 public int hashCode() {
29 return TypeHelper.combineHashCodes(minCoords.hashCode(), maxCoords.hashCode());
33 public boolean equals(Object obj) {
34 if (this == obj) return true;
35 if (obj == null || getClass() != obj.getClass()) return false;
36 BoundingBoxCuboid other = (BoundingBoxCuboid) obj;
37 return minCoords.equals(other.minCoords) && maxCoords.equals(other.maxCoords);
40 public Coords getMinCoords() {
44 public Coords getMaxCoords() {
49 public Boolean intersectsBounds(int minX, int minZ, int maxX, int maxZ) {
50 boolean minXWithinBounds = isBetween(minCoords.getX(), minX, maxX);
51 boolean maxXWithinBounds = isBetween(maxCoords.getX(), minX, maxX);
52 boolean minZWithinBounds = isBetween(minCoords.getZ(), minZ, maxZ);
53 boolean maxZWithinBounds = isBetween(maxCoords.getZ(), minZ, maxZ);
55 return (minXWithinBounds && minZWithinBounds) ||
56 (maxXWithinBounds && maxZWithinBounds) ||
57 (minXWithinBounds && maxZWithinBounds) ||
58 (maxXWithinBounds && minZWithinBounds);
61 private boolean isBetween(int val, int min, int max) {
62 return val >= min && val <= max;
66 public double getDistanceX(double x) {
67 return x - MathHelper.clamp(x, minCoords.getX(), maxCoords.getX());
71 public double getDistanceY(double y) {
72 return y - MathHelper.clamp(y, minCoords.getY(), maxCoords.getY());
76 public double getDistanceZ(double z) {
77 return z - MathHelper.clamp(z, minCoords.getZ(), maxCoords.getZ());
81 public AbstractRenderer<?> getRenderer() {
86 public boolean isVisibleCulling() {
87 return RenderCulling.isVisibleCulling(minCoords.getX(), minCoords.getY(), minCoords.getZ(), maxCoords.getX(), maxCoords.getY(), maxCoords.getZ());