1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package at.hannibal2.skyhanni.utils
import net.minecraft.client.Minecraft
object RaycastUtils {
data class Ray(
val origin: LorenzVec,
val direction: LorenzVec,
) {
init {
require(direction.isNormalized())
}
}
data class Plane(
val origin: LorenzVec,
val normal: LorenzVec,
) {
init {
require(normal.isNormalized())
}
}
fun createPlayerLookDirectionRay(): Ray {
return Ray(
LocationUtils.playerEyeLocation(),
Minecraft.getMinecraft().thePlayer.lookVec.toLorenzVec()
)
}
/**
* Create a plane that contains [point] and is orthogonal to [ray].
*/
fun createOrthogonalPlaneToRayAtPoint(
ray: Ray,
point: LorenzVec,
): Plane {
return Plane(point, ray.direction)
}
/**
* Intersect a plane (of any orientation) with a ray. The ray and plane may not be parallel to each other.
*/
fun intersectPlaneWithRay(plane: Plane, ray: Ray): LorenzVec {
// require(plane.normal.dotProduct(ray.direction).absoluteValue != 0.0)
val intersectionPointDistanceAlongRay =
(plane.normal.dotProduct(plane.origin) - plane.normal.dotProduct(ray.origin)) / plane.normal.dotProduct(ray.direction)
return ray.origin + ray.direction.scale(intersectionPointDistanceAlongRay)
}
/**
* Finds the distance between the given ray and the point. If the point is behind the ray origin (according to the ray's direction),
* returns [Double.MAX_VALUE] instead.
*/
fun findDistanceToRay(ray: Ray, point: LorenzVec): Double {
val plane = createOrthogonalPlaneToRayAtPoint(ray, point)
val intersectionPoint = intersectPlaneWithRay(plane, ray)
if ((intersectionPoint - ray.origin).dotProduct(ray.direction) < 0) return Double.MAX_VALUE
return intersectionPoint.distance(point)
}
inline fun <T> createDistanceToRayEstimator(ray: Ray, crossinline position: (T) -> LorenzVec): (T) -> Double {
return {
findDistanceToRay(ray, position(it))
}
}
fun <T : Any> List<T>.findClosestPointToRay(ray: Ray, positionExtractor: (T) -> LorenzVec): T? {
return minByOrNull(createDistanceToRayEstimator(ray, positionExtractor))
}
}
|