@@ -21,131 +21,33 @@ import com.lambda.interaction.request.rotation.Rotation
2121import net.minecraft.entity.Entity
2222import net.minecraft.util.math.Direction
2323import net.minecraft.util.math.MathHelper
24- import net.minecraft.util.math.MathHelper.wrapDegrees
2524import net.minecraft.util.math.Vec3i
26- import kotlin.math.abs
27- import kotlin.math.asin
28- import kotlin.math.atan
29- import kotlin.math.cos
30- import kotlin.math.sin
3125
3226// ToDo: This is broken in many ways. Still a WIP
3327enum class PlaceDirection (
3428 val rotation : Rotation ,
3529 val vector : Vec3i ,
36- private val yawRanges : List <ClosedRange <Double >>
3730) {
38- UpNorth ( - 180.0 , - 90.0 , 0 , 1 , - 1 , northYawRanges ),
39- UpSouth ( 0.0 , - 90.0 , 0 , 1 , 1 , listOf (southYawRange) ),
40- UpWest ( 90.0 , - 90.0 , 1 , 1 , 0 , listOf (westYawRange) ),
41- UpEast ( - 90.0 , - 90.0 , - 1 , 1 , 0 , listOf (eastYawRange) ),
31+ UpNorth (- 180.0 , - 90.0 , 0 , 1 , - 1 ),
32+ UpSouth ( 0.0 , - 90.0 , 0 , 1 , 1 ),
33+ UpWest ( 90.0 , - 90.0 , 1 , 1 , 0 ),
34+ UpEast ( - 90.0 , - 90.0 , - 1 , 1 , 0 ),
4235
43- DownNorth ( - 180.0 , 90.0 , 0 , - 1 , - 1 , northYawRanges ),
44- DownSouth ( 0.0 , 90.0 , 0 , - 1 , 1 , listOf (southYawRange) ),
45- DownWest ( 90.0 , 90.0 , 1 , - 1 , 0 , listOf (westYawRange) ),
46- DownEast ( - 90.0 , 90.0 , - 1 , - 1 , 0 , listOf (eastYawRange) ),
36+ DownNorth (- 180.0 , 90.0 , 0 , - 1 , - 1 ),
37+ DownSouth ( 0.0 , 90.0 , 0 , - 1 , 1 ),
38+ DownWest ( 90.0 , 90.0 , 1 , - 1 , 0 ),
39+ DownEast ( - 90.0 , 90.0 , - 1 , - 1 , 0 ),
4740
48- North ( - 180.0 , 0.0 , 0 , 0 , - 1 , northYawRanges ),
49- South ( 0.0 , 0.0 , 0 , 0 , 1 , listOf (southYawRange) ),
50- West ( 90.0 , 0.0 , 1 , 0 , 0 , listOf (westYawRange) ),
51- East ( - 90.0 , 0.0 , - 1 , 0 , 0 , listOf (eastYawRange) );
41+ North (- 180.0 , 0.0 , 0 , 0 , - 1 ),
42+ South ( 0.0 , 0.0 , 0 , 0 , 1 ),
43+ West ( 90.0 , 0.0 , 1 , 0 , 0 ),
44+ East ( - 90.0 , 0.0 , - 1 , 0 , 0 );
5245
53- constructor (yaw: Double , pitch: Double , x: Int , y: Int , z: Int , yawRanges : List < ClosedRange < Double >> )
54- : this (Rotation (yaw, pitch), Vec3i (x, y, z), yawRanges )
46+ constructor (yaw: Double , pitch: Double , x: Int , y: Int , z: Int )
47+ : this (Rotation (yaw, pitch), Vec3i (x, y, z))
5548
56- fun snapToArea (rot : Rotation ): Rotation {
57- if (isInArea(rot)) return rot
58-
59- val normalizedYaw = wrapDegrees(rot.yaw)
60- val clampedYaw = when {
61- rotation.yaw != - 180.0 -> normalizedYaw.coerceIn(yawRanges[0 ])
62- normalizedYaw < 0 -> normalizedYaw.coerceIn(yawRanges[0 ])
63- else -> normalizedYaw.coerceIn(yawRanges[1 ])
64- }
65-
66- // Calculate pitch boundaries based on the snapped yaw
67- val snappedYawRad = Math .toRadians(clampedYaw)
68- val sinYaw = abs(sin(snappedYawRad))
69- val cosYaw = abs(cos(snappedYawRad))
70- val pitchBoundaryEW = Math .toDegrees(atan(sinYaw))
71- val pitchBoundaryNS = Math .toDegrees(atan(cosYaw))
72-
73- // Determine the correct pitch boundary and snap pitch
74- val snappedPitch = when {
75- // Primary E/W Directions
76- isEast() || isWest() -> {
77- when {
78- isUp() -> calculateVerticalPitch(sinYaw, pitchBoundaryEW, true )
79- isDown() -> calculateVerticalPitch(sinYaw, pitchBoundaryEW, false )
80- else -> calculateHorizontalPitch(rot.pitch, pitchBoundaryEW)
81- }
82- }
83- // Primary N/S Directions
84- isNorth() || isSouth() -> {
85- when {
86- isUp() -> calculateVerticalPitch(cosYaw, pitchBoundaryNS, true )
87- isDown() -> calculateVerticalPitch(cosYaw, pitchBoundaryNS, false )
88- else -> calculateHorizontalPitch(rot.pitch, pitchBoundaryNS)
89- }
90- }
91- // impossible to look just up or just down as you are always facing a horizontal direction
92- else -> rotation.pitch
93- }
94-
95- // Clamp pitch to valid range
96- val clampedPitch = snappedPitch.coerceIn(- 90.0 , 90.0 )
97-
98- return Rotation (clampedYaw, clampedPitch)
99- }
100-
101- /* *
102- * Calculates the pitch for vertical (Up/Down) directions
103- *
104- * @param trigValue The trigonometric value (sinYaw for E/W, cosYaw for N/S)
105- * @param boundaryValue The boundary value (pitchBoundaryEW for E/W, pitchBoundaryNS for N/S)
106- * @param isUp Whether this is for an Up direction (true) or Down direction (false)
107- * @return The calculated pitch value
108- */
109- private fun calculateVerticalPitch (trigValue : Double , boundaryValue : Double , isUp : Boolean ): Double {
110- val epsilon = 0.01
111- val boundarySign = if (isUp) 1 else - 1
112- val asinSign = if (isUp) - 1 else 1
113-
114- val targetPitch = Math .toDegrees(
115- asinSign * asin(trigValue * cos(Math .toRadians(boundarySign * boundaryValue)) + epsilon)
116- )
117-
118- return if (isUp) {
119- targetPitch.coerceIn(- 90.0 , 0.0 ) // Ensure it's in the up range
120- } else {
121- targetPitch.coerceIn(0.0 , 90.0 ) // Ensure it's in the down range
122- }
123- }
124-
125- /* *
126- * Calculates the pitch for horizontal directions
127- *
128- * @param currentPitch The current pitch value
129- * @param boundaryValue The boundary value (pitchBoundaryEW for E/W, pitchBoundaryNS for N/S)
130- * @return The calculated pitch value
131- */
132- private fun calculateHorizontalPitch (currentPitch : Double , boundaryValue : Double ): Double {
133- // Handle extreme pitch values (-90 or 90) by returning 0
134- if (abs(currentPitch) >= 90.0 - 0.1 ) {
135- return 0.0
136- }
137-
138- val isWithinPositiveBoundary = abs(currentPitch - boundaryValue) < abs(currentPitch - (- boundaryValue))
139- return if (isWithinPositiveBoundary) boundaryValue else - boundaryValue
140- }
141-
142- // Helper functions to determine direction type
143- private fun isEast (): Boolean = this == East || this == UpEast || this == DownEast
144- private fun isWest (): Boolean = this == West || this == UpWest || this == DownWest
145- private fun isNorth (): Boolean = this == North || this == UpNorth || this == DownNorth
146- private fun isSouth (): Boolean = this == South || this == UpSouth || this == DownSouth
147- private fun isUp (): Boolean = this == UpEast || this == UpWest || this == UpNorth || this == UpSouth
148- private fun isDown (): Boolean = this == DownEast || this == DownWest || this == DownNorth || this == DownSouth
49+ // ToDo: snap to the area not the cardinal to avoid excess rotation distance
50+ fun snapToArea (rot : Rotation ): Rotation = rotation
14951
15052 fun isInArea (rot : Rotation ) = fromRotation(rot) == this
15153
@@ -197,10 +99,3 @@ enum class PlaceDirection(
19799 }
198100 }
199101}
200-
201- const val FUDGE_FACTOR = 0.01
202-
203- val northYawRanges = listOf (- 180.0 .. (- 135.0 - FUDGE_FACTOR ), (135.0 + FUDGE_FACTOR ).. 180.0 )
204- val southYawRange = ( - 45.0 + FUDGE_FACTOR ).. ( 45.0 - FUDGE_FACTOR )
205- val eastYawRange = (- 135.0 + FUDGE_FACTOR ).. (- 45.0 - FUDGE_FACTOR )
206- val westYawRange = ( 45.0 + FUDGE_FACTOR ).. (135.0 - FUDGE_FACTOR )
0 commit comments