2020-04-24 03:25:58 +02:00
|
|
|
package net.minestom.server.collision;
|
2019-08-30 01:17:46 +02:00
|
|
|
|
2020-04-24 03:25:58 +02:00
|
|
|
import net.minestom.server.entity.Entity;
|
|
|
|
import net.minestom.server.utils.BlockPosition;
|
|
|
|
import net.minestom.server.utils.Position;
|
2019-08-30 01:17:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See https://wiki.vg/Entity_metadata#Mobs_2
|
|
|
|
*/
|
|
|
|
public class BoundingBox {
|
|
|
|
|
|
|
|
private Entity entity;
|
|
|
|
private float x, y, z;
|
|
|
|
|
|
|
|
public BoundingBox(Entity entity, float x, float y, float z) {
|
|
|
|
this.entity = entity;
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.z = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean intersect(BoundingBox boundingBox) {
|
|
|
|
return (getMinX() <= boundingBox.getMaxX() && getMaxX() >= boundingBox.getMinX()) &&
|
|
|
|
(getMinY() <= boundingBox.getMaxY() && getMaxY() >= boundingBox.getMinY()) &&
|
|
|
|
(getMinZ() <= boundingBox.getMaxZ() && getMaxZ() >= boundingBox.getMinZ());
|
|
|
|
}
|
|
|
|
|
2019-08-31 07:54:53 +02:00
|
|
|
public boolean intersect(BlockPosition blockPosition) {
|
2020-04-15 13:20:28 +02:00
|
|
|
|
2020-04-20 18:46:39 +02:00
|
|
|
final float x = blockPosition.getX();
|
|
|
|
final float y = blockPosition.getY();
|
|
|
|
final float z = blockPosition.getZ();
|
2020-04-15 13:20:28 +02:00
|
|
|
|
2020-04-20 18:46:39 +02:00
|
|
|
final float offsetX = 1;
|
|
|
|
final float offsetZ = 1;
|
|
|
|
|
|
|
|
float minX = x;
|
|
|
|
float maxX = x + offsetX;
|
2020-04-15 13:20:28 +02:00
|
|
|
|
2020-04-20 18:46:39 +02:00
|
|
|
float minY = y;
|
|
|
|
float maxY = y + 0.99999f;
|
2020-04-15 13:20:28 +02:00
|
|
|
|
2020-04-20 18:46:39 +02:00
|
|
|
float minZ = z;
|
|
|
|
float maxZ = z + offsetZ;
|
|
|
|
|
|
|
|
boolean checkX = getMinX() < maxX && getMaxX() > minX;
|
|
|
|
boolean checkY = getMinY() < maxY && getMaxY() > minY;
|
|
|
|
boolean checkZ = getMinZ() < maxZ && getMaxZ() > minZ;
|
|
|
|
|
|
|
|
return checkX && checkY && checkZ;
|
2020-04-15 13:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 14:25:42 +02:00
|
|
|
public boolean intersect(float x, float y, float z) {
|
|
|
|
return (x >= getMinX() && x <= getMaxX()) &&
|
|
|
|
(y >= getMinY() && y <= getMaxY()) &&
|
|
|
|
(z >= getMinZ() && z <= getMaxZ());
|
|
|
|
}
|
|
|
|
|
2019-08-31 07:54:53 +02:00
|
|
|
public boolean intersect(Position position) {
|
2020-04-09 14:25:42 +02:00
|
|
|
return intersect(position.getX(), position.getY(), position.getZ());
|
2019-08-31 07:54:53 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 01:17:46 +02:00
|
|
|
public BoundingBox expand(float x, float y, float z) {
|
|
|
|
return new BoundingBox(entity, this.x + x, this.y + y, this.z + z);
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:46:39 +02:00
|
|
|
public BoundingBox contract(float x, float y, float z) {
|
|
|
|
return new BoundingBox(entity, this.x - x, this.y - y, this.z - z);
|
|
|
|
}
|
|
|
|
|
2019-08-30 01:17:46 +02:00
|
|
|
public float getX() {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getY() {
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getZ() {
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getMinX() {
|
|
|
|
return entity.getPosition().getX() - (x / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getMaxX() {
|
|
|
|
return entity.getPosition().getX() + (x / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getMinY() {
|
2020-04-20 18:46:39 +02:00
|
|
|
return entity.getPosition().getY();
|
2019-08-30 01:17:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public float getMaxY() {
|
2020-04-20 18:46:39 +02:00
|
|
|
return entity.getPosition().getY() + y;
|
2019-08-30 01:17:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public float getMinZ() {
|
|
|
|
return entity.getPosition().getZ() - (z / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getMaxZ() {
|
|
|
|
return entity.getPosition().getZ() + (z / 2);
|
|
|
|
}
|
|
|
|
|
2019-08-31 07:54:53 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2020-04-20 18:46:39 +02:00
|
|
|
String result = "BoudingBox";
|
|
|
|
result += "\n";
|
|
|
|
result += "[" + getMinX() + " : " + getMaxX() + "]";
|
|
|
|
result += "\n";
|
|
|
|
result += "[" + getMinY() + " : " + getMaxY() + "]";
|
|
|
|
result += "\n";
|
|
|
|
result += "[" + getMinZ() + " : " + getMaxZ() + "]";
|
|
|
|
return result;
|
2019-08-31 07:54:53 +02:00
|
|
|
}
|
2019-08-30 01:17:46 +02:00
|
|
|
}
|