aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt
diff options
context:
space:
mode:
authorThunderblade73 <85900443+Thunderblade73@users.noreply.github.com>2024-04-03 20:50:31 +0200
committerGitHub <noreply@github.com>2024-04-03 20:50:31 +0200
commit2f85351bacddb9ab3704a53c778d558a755bcc06 (patch)
treef479045f271f04a66a1ba69a61cb1b9893261eb5 /src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt
parent76be6ad6de39c7078550394e8ec24a494ddb3bcc (diff)
downloadskyhanni-2f85351bacddb9ab3704a53c778d558a755bcc06.tar.gz
skyhanni-2f85351bacddb9ab3704a53c778d558a755bcc06.tar.bz2
skyhanni-2f85351bacddb9ab3704a53c778d558a755bcc06.zip
Backend: Mob Detection (#712)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Co-authored-by: Cal <cwolfson58@gmail.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt
index 29cc1d569..ca651ea08 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/LocationUtils.kt
@@ -3,6 +3,9 @@ package at.hannibal2.skyhanni.utils
import net.minecraft.client.Minecraft
import net.minecraft.entity.Entity
import net.minecraft.util.AxisAlignedBB
+import net.minecraft.util.BlockPos
+import kotlin.math.max
+import kotlin.math.min
object LocationUtils {
@@ -22,6 +25,9 @@ object LocationUtils {
fun Entity.distanceToPlayer() = getLorenzVec().distanceToPlayer()
fun Entity.distanceTo(location: LorenzVec) = getLorenzVec().distance(location)
+ fun Entity.distanceTo(other: Entity) = getLorenzVec().distance(other.getLorenzVec())
+
+ fun Entity.distanceToIgnoreY(location: LorenzVec) = getLorenzVec().distanceIgnoreY(location)
fun playerEyeLocation(): LorenzVec {
val player = Minecraft.getMinecraft().thePlayer
@@ -41,4 +47,53 @@ object LocationUtils {
val inFov = true // TODO add Frustum "Frustum().isBoundingBoxInFrustum(entity.entityBoundingBox)"
return noBlocks && notTooFar && inFov
}
+
+ fun AxisAlignedBB.minBox() = LorenzVec(minX, minY, minZ)
+
+ fun AxisAlignedBB.maxBox() = LorenzVec(maxX, maxY, maxZ)
+
+ fun AxisAlignedBB.rayIntersects(origin: LorenzVec, direction: LorenzVec): Boolean {
+ // Reference for Algorithm https://tavianator.com/2011/ray_box.html
+ val rayDirectionInverse = direction.inverse()
+ val t1 = (this.minBox().subtract(origin)).multiply(rayDirectionInverse)
+ val t2 = (this.maxBox().subtract(origin)).multiply(rayDirectionInverse)
+
+ val tmin = max(t1.minOfEachElement(t2).max(), Double.NEGATIVE_INFINITY)
+ val tmax = min(t1.maxOfEachElement(t2).min(), Double.POSITIVE_INFINITY)
+ return tmax >= tmin && tmax >= 0.0
+ }
+
+ fun AxisAlignedBB.union(aabbs: List<AxisAlignedBB>?): AxisAlignedBB? {
+ if (aabbs.isNullOrEmpty()) {
+ return null
+ }
+
+ var minX = this.minX
+ var minY = this.minY
+ var minZ = this.minZ
+ var maxX = this.maxX
+ var maxY = this.maxY
+ var maxZ = this.maxZ
+
+ aabbs.forEach { aabb ->
+ if (aabb.minX < minX) minX = aabb.minX
+ if (aabb.minY < minY) minY = aabb.minY
+ if (aabb.minZ < minZ) minZ = aabb.minZ
+ if (aabb.maxX > maxX) maxX = aabb.maxX
+ if (aabb.maxY > maxY) maxY = aabb.maxY
+ if (aabb.maxZ > maxZ) maxZ = aabb.maxZ
+ }
+
+ val combinedMin = BlockPos(minX, minY, minZ)
+ val combinedMax = BlockPos(maxX, maxY, maxZ)
+
+ return AxisAlignedBB(minX, minY, minZ, maxX, maxY, maxZ)
+ }
+
+ fun AxisAlignedBB.getEdgeLengths() = this.maxBox().subtract(this.minBox())
+
+ fun AxisAlignedBB.getCenter() = this.getEdgeLengths().multiply(0.5).add(this.minBox())
+
+ fun AxisAlignedBB.getTopCenter() = this.getCenter().add(y = (maxY - minY) / 2)
}
+