2019-08-30 01:17:46 +02:00
|
|
|
package fr.themode.minestom.collision;
|
|
|
|
|
|
|
|
import fr.themode.minestom.entity.Entity;
|
2019-08-31 07:54:53 +02:00
|
|
|
import fr.themode.minestom.utils.BlockPosition;
|
|
|
|
import fr.themode.minestom.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());
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:20:28 +02:00
|
|
|
// FIXME: seems to do not work properly
|
2019-08-31 07:54:53 +02:00
|
|
|
public boolean intersect(BlockPosition blockPosition) {
|
2019-09-03 07:36:04 +02:00
|
|
|
final float x = 1.6f;
|
2019-08-31 07:54:53 +02:00
|
|
|
final float y = 1;
|
2019-09-03 07:36:04 +02:00
|
|
|
final float z = 1.6f;
|
2020-04-15 13:20:28 +02:00
|
|
|
|
|
|
|
float minX = blockPosition.getX() - (x / 2) + 0.5f;
|
|
|
|
float maxX = blockPosition.getX() + (x / 2) + 0.5f;
|
|
|
|
|
|
|
|
float minY = blockPosition.getY();
|
|
|
|
float maxY = blockPosition.getY() + y;
|
|
|
|
|
|
|
|
float minZ = blockPosition.getZ() - (z / 2) + 0.5f;
|
|
|
|
float maxZ = blockPosition.getZ() + (z / 2) + 0.5f;
|
|
|
|
|
|
|
|
if (getMinX() > maxX) return false;
|
|
|
|
if (getMaxX() < minX) return false;
|
|
|
|
if (getMinY() > maxY) return false;
|
|
|
|
if (getMaxY() < minY) return false;
|
|
|
|
if (getMinZ() > maxZ) return false;
|
|
|
|
return !(getMaxZ() < minZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*public boolean intersect(BlockPosition blockPosition) {
|
|
|
|
final float x = 1.6f;
|
|
|
|
final float y = 1;
|
|
|
|
final float z = 1.6f;
|
2019-08-31 07:54:53 +02:00
|
|
|
float minX = blockPosition.getX() - (x / 2) + 0.5f;
|
|
|
|
float maxX = blockPosition.getX() + (x / 2) + 0.5f;
|
|
|
|
|
|
|
|
float minY = blockPosition.getY();
|
|
|
|
float maxY = blockPosition.getY() + y;
|
|
|
|
|
|
|
|
float minZ = blockPosition.getZ() - (z / 2) + 0.5f;
|
|
|
|
float maxZ = blockPosition.getZ() + (z / 2) + 0.5f;
|
|
|
|
|
|
|
|
boolean checkX = getMinX() + x / 2 < maxX && getMaxX() - x / 2 > minX;
|
|
|
|
boolean checkY = getMinY() + y < maxY && getMaxY() + y > minY;
|
|
|
|
boolean checkZ = getMinZ() + z / 2 < maxZ && getMaxZ() - z / 2 > minZ;
|
|
|
|
return checkX && checkY && checkZ;
|
2020-04-15 13:20:28 +02:00
|
|
|
}*/
|
2019-08-31 07:54:53 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
return entity.getPosition().getY() - (y / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getMaxY() {
|
|
|
|
return entity.getPosition().getY() + (y / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
return "BoundingBox[" + x + ":" + y + ":" + z + "]";
|
|
|
|
}
|
2019-08-30 01:17:46 +02:00
|
|
|
}
|