Skip to content

Commit df00a6d

Browse files
committed
Merge remote-tracking branch 'origin/feature/packetmine-rewrite' into feature/packetmine-rewrite
2 parents 0aeaece + 03ba095 commit df00a6d

File tree

3 files changed

+68
-89
lines changed

3 files changed

+68
-89
lines changed

common/src/main/kotlin/com/lambda/interaction/request/rotation/visibilty/PlaceDirection.kt

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import net.minecraft.util.math.MathHelper
2424
import net.minecraft.util.math.MathHelper.wrapDegrees
2525
import net.minecraft.util.math.Vec3i
2626
import kotlin.math.abs
27+
import kotlin.math.asin
2728
import kotlin.math.atan
2829
import kotlin.math.cos
2930
import kotlin.math.sin
@@ -66,31 +67,27 @@ enum class PlaceDirection(
6667

6768
// Calculate pitch boundaries based on the snapped yaw
6869
val snappedYawRad = Math.toRadians(clampedYaw)
69-
val pitchBoundaryEW = Math.toDegrees(atan(abs(sin(snappedYawRad))))
70-
val pitchBoundaryNS = Math.toDegrees(atan(abs(cos(snappedYawRad))))
70+
val sinYaw = abs(sin(snappedYawRad))
71+
val cosYaw = abs(cos(snappedYawRad))
72+
val pitchBoundaryEW = Math.toDegrees(atan(sinYaw))
73+
val pitchBoundaryNS = Math.toDegrees(atan(cosYaw))
7174

7275
// Determine the correct pitch boundary and snap pitch
7376
val snappedPitch = when {
7477
// Primary E/W Directions
7578
isEast() || isWest() -> {
7679
when {
77-
isUp() -> pitchBoundaryEW // Snap to lower edge of UP_E/UP_W area
78-
isDown() -> -pitchBoundaryEW // Snap to upper edge of DOWN_E/DOWN_W area
79-
else -> { // Horizontal E/W
80-
val isPitchWithinBounds = abs(rot.pitch - pitchBoundaryEW) < abs(rot.pitch - (-pitchBoundaryEW))
81-
if (isPitchWithinBounds) pitchBoundaryEW else -pitchBoundaryEW
82-
}
80+
isUp() -> calculateVerticalPitch(sinYaw, pitchBoundaryEW, true)
81+
isDown() -> calculateVerticalPitch(sinYaw, pitchBoundaryEW, false)
82+
else -> calculateHorizontalPitch(rot.pitch, pitchBoundaryEW)
8383
}
8484
}
8585
// Primary N/S Directions
8686
isNorth() || isSouth() -> {
8787
when {
88-
isUp() -> pitchBoundaryNS // Snap to lower edge of UP_N/UP_S area
89-
isDown() -> -pitchBoundaryNS // Snap to upper edge of DOWN_N/DOWN_S area
90-
else -> { // Horizontal N/S
91-
val isWithinNorthernBoundary = abs(rot.pitch - pitchBoundaryNS) < abs(rot.pitch - (-pitchBoundaryNS))
92-
if (isWithinNorthernBoundary) pitchBoundaryNS else -pitchBoundaryNS
93-
}
88+
isUp() -> calculateVerticalPitch(cosYaw, pitchBoundaryNS, true)
89+
isDown() -> calculateVerticalPitch(cosYaw, pitchBoundaryNS, false)
90+
else -> calculateHorizontalPitch(rot.pitch, pitchBoundaryNS)
9491
}
9592
}
9693
// Handle purely UP/DOWN directions
@@ -103,6 +100,42 @@ enum class PlaceDirection(
103100
return Rotation(clampedYaw, clampedPitch)
104101
}
105102

103+
/**
104+
* Calculates the pitch for vertical (Up/Down) directions
105+
*
106+
* @param trigValue The trigonometric value (sinYaw for E/W, cosYaw for N/S)
107+
* @param boundaryValue The boundary value (pitchBoundaryEW for E/W, pitchBoundaryNS for N/S)
108+
* @param isUp Whether this is for an Up direction (true) or Down direction (false)
109+
* @return The calculated pitch value
110+
*/
111+
private fun calculateVerticalPitch(trigValue: Double, boundaryValue: Double, isUp: Boolean): Double {
112+
val epsilon = 0.01
113+
val boundarySign = if (isUp) 1 else -1
114+
val asinSign = if (isUp) -1 else 1
115+
116+
val targetPitch = Math.toDegrees(
117+
asinSign * asin(trigValue * cos(Math.toRadians(boundarySign * boundaryValue)) + epsilon)
118+
)
119+
120+
return if (isUp) {
121+
targetPitch.coerceIn(-90.0, 0.0) // Ensure it's in the up range
122+
} else {
123+
targetPitch.coerceIn(0.0, 90.0) // Ensure it's in the down range
124+
}
125+
}
126+
127+
/**
128+
* Calculates the pitch for horizontal directions
129+
*
130+
* @param currentPitch The current pitch value
131+
* @param boundaryValue The boundary value (pitchBoundaryEW for E/W, pitchBoundaryNS for N/S)
132+
* @return The calculated pitch value
133+
*/
134+
private fun calculateHorizontalPitch(currentPitch: Double, boundaryValue: Double): Double {
135+
val isWithinPositiveBoundary = abs(currentPitch - boundaryValue) < abs(currentPitch - (-boundaryValue))
136+
return if (isWithinPositiveBoundary) boundaryValue else -boundaryValue
137+
}
138+
106139
// Helper functions to determine direction type
107140
private fun isEast(): Boolean = this == East || this == UpEast || this == DownEast
108141
private fun isWest(): Boolean = this == West || this == UpWest || this == DownWest

common/src/test/kotlin/PlaceDirectionTest.kt

Lines changed: 20 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,94 +26,44 @@ import kotlin.test.assertEquals
2626
import kotlin.test.assertTrue
2727

2828
class PlaceDirectionTest {
29-
29+
3030
@Test
3131
fun `test pitch snapping for East direction`() {
3232
val direction = PlaceDirection.East
33-
34-
// Calculate expected pitch boundary for East at yaw -90.0
35-
val yawRad = Math.toRadians(-90.0)
36-
val expectedBoundary = Math.toDegrees(atan(abs(sin(yawRad))))
37-
38-
// Test rotation outside the area (pitch too high)
39-
val rotationOutsideHigh = Rotation(-90.0, expectedBoundary + 10.0)
40-
val snappedHigh = direction.snapToArea(rotationOutsideHigh)
41-
42-
// Test rotation outside the area (pitch too low)
43-
val rotationOutsideLow = Rotation(-90.0, -expectedBoundary - 10.0)
44-
val snappedLow = direction.snapToArea(rotationOutsideLow)
45-
33+
val rot = Rotation(-90.0, 90.0)
34+
val snapped = direction.snapToArea(rot)
35+
4636
// Verify that the pitch is snapped to the boundary
47-
assertEquals(expectedBoundary, snappedHigh.pitch, 0.001, "Pitch should be snapped to the upper boundary")
48-
assertEquals(-expectedBoundary, snappedLow.pitch, 0.001, "Pitch should be snapped to the lower boundary")
37+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
4938
}
50-
39+
5140
@Test
5241
fun `test pitch snapping for North direction`() {
5342
val direction = PlaceDirection.North
54-
55-
// Calculate expected pitch boundary for North at yaw -180.0
56-
val yawRad = Math.toRadians(-180.0)
57-
val expectedBoundary = Math.toDegrees(atan(abs(cos(yawRad))))
58-
59-
// Test rotation outside the area (pitch too high)
60-
val rotationOutsideHigh = Rotation(-180.0, expectedBoundary + 10.0)
61-
val snappedHigh = direction.snapToArea(rotationOutsideHigh)
62-
63-
// Test rotation outside the area (pitch too low)
64-
val rotationOutsideLow = Rotation(-180.0, -expectedBoundary - 10.0)
65-
val snappedLow = direction.snapToArea(rotationOutsideLow)
66-
43+
val rot = Rotation(-180.0, 90.0)
44+
val snapped = direction.snapToArea(rot)
45+
6746
// Verify that the pitch is snapped to the boundary
68-
assertEquals(expectedBoundary, snappedHigh.pitch, 0.001, "Pitch should be snapped to the upper boundary")
69-
assertEquals(-expectedBoundary, snappedLow.pitch, 0.001, "Pitch should be snapped to the lower boundary")
47+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
7048
}
71-
49+
7250
@Test
7351
fun `test pitch snapping for UpEast direction`() {
7452
val direction = PlaceDirection.UpEast
75-
76-
// Calculate expected pitch boundary for UpEast at yaw -90.0
77-
val yawRad = Math.toRadians(-90.0)
78-
val expectedBoundary = Math.toDegrees(atan(abs(sin(yawRad))))
79-
80-
// Test rotation outside the area (pitch too low)
81-
val rotationOutside = Rotation(-90.0, expectedBoundary - 10.0)
82-
val snapped = direction.snapToArea(rotationOutside)
83-
53+
val rot = Rotation(-90.0, 0.0)
54+
val snapped = direction.snapToArea(rot)
55+
8456
// Verify that the pitch is snapped to the boundary
85-
assertEquals(expectedBoundary, snapped.pitch, 0.001, "Pitch should be snapped to the boundary")
57+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
8658
}
87-
59+
8860
@Test
8961
fun `test pitch snapping for DownNorth direction`() {
9062
val direction = PlaceDirection.DownNorth
91-
92-
// Calculate expected pitch boundary for DownNorth at yaw -180.0
93-
val yawRad = Math.toRadians(-180.0)
94-
val expectedBoundary = Math.toDegrees(atan(abs(cos(yawRad))))
95-
96-
// Test rotation outside the area (pitch too high)
97-
val rotationOutside = Rotation(-180.0, -expectedBoundary + 10.0)
98-
val snapped = direction.snapToArea(rotationOutside)
99-
63+
val rot = Rotation(-180.0, 0.0)
64+
val snapped = direction.snapToArea(rot)
65+
10066
// Verify that the pitch is snapped to the boundary
101-
assertEquals(-expectedBoundary, snapped.pitch, 0.001, "Pitch should be snapped to the boundary")
102-
}
103-
104-
@Test
105-
fun `test no snapping when rotation is already in area`() {
106-
val direction = PlaceDirection.East
107-
108-
// Create a rotation that should be in the East area
109-
val rotation = Rotation(-90.0, 0.0)
110-
111-
// Verify that the rotation is in the area
112-
assertTrue(direction.isInArea(rotation), "Rotation should be in the East area")
113-
114-
// Verify that snapToArea returns the same rotation
115-
val snapped = direction.snapToArea(rotation)
116-
assertEquals(rotation.yaw, snapped.yaw, 0.001, "Yaw should not change")
117-
assertEquals(rotation.pitch, snapped.pitch, 0.001, "Pitch should not change")
67+
assertEquals(direction, PlaceDirection.fromRotation(snapped))
11868
}
11969
}

common/src/test/kotlin/RotationTest.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
package com.lambda.interaction.request.rotation
19-
18+
import com.lambda.interaction.request.rotation.Rotation
2019
import com.lambda.interaction.request.rotation.Rotation.Companion.angleDifference
2120
import com.lambda.interaction.request.rotation.Rotation.Companion.dist
2221
import com.lambda.interaction.request.rotation.Rotation.Companion.lerp
@@ -25,10 +24,7 @@ import kotlin.test.Test
2524
import kotlin.test.assertEquals
2625
import kotlin.test.assertTrue
2726
import kotlin.test.assertFalse
28-
import kotlin.math.abs
2927
import kotlin.math.hypot
30-
import net.minecraft.util.math.Vec3d
31-
import net.minecraft.util.math.MathHelper
3228

3329
/**
3430
* Tests for the Rotation class

0 commit comments

Comments
 (0)