Skip to content

Commit e9db1aa

Browse files
committed
locks to yaw boundaries however pitch is still hard coded to 90, 0, -90
1 parent 8a0cb5b commit e9db1aa

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import com.lambda.util.world.raycast.RayCastUtils.rayCast
2929
import net.minecraft.entity.Entity
3030
import net.minecraft.util.math.Box
3131
import net.minecraft.util.math.Direction
32-
import net.minecraft.util.math.MathHelper
3332
import net.minecraft.util.math.MathHelper.wrapDegrees
3433
import net.minecraft.util.math.Vec3d
3534
import kotlin.math.abs
@@ -96,17 +95,17 @@ data class Rotation(val yaw: Double, val pitch: Double) {
9695
val UP = Rotation(0.0, -90.0)
9796
val Direction.rotation get() = Rotation(yaw.toDouble(), 0.0)
9897
var Entity.rotation
99-
get() = Rotation(wrapDegrees(yaw), wrapDegrees(pitch))
98+
get() = Rotation(yaw, pitch)
10099
set(value) {
101100
yaw = value.yawF
102101
pitch = value.pitchF
103102
}
104103

105-
fun wrap(deg: Double) = MathHelper.wrapDegrees(deg)
104+
fun wrap(deg: Double) = wrapDegrees(deg)
106105

107106
fun Rotation.lerp(other: Rotation, delta: Double): Rotation {
108-
val yaw = this.yaw + delta * (other.yaw - this.yaw)
109-
val pitch = this.pitch + delta * (other.pitch - this.pitch)
107+
val yaw = wrap(this.yaw + delta * (other.yaw - this.yaw))
108+
val pitch = wrap(this.pitch + delta * (other.pitch - this.pitch))
110109
return Rotation(yaw, pitch)
111110
}
112111

@@ -119,8 +118,8 @@ data class Rotation(val yaw: Double, val pitch: Double) {
119118
val yawSpeed = abs(yawDiff / diff) * speed
120119
val pitchSpeed = abs(pitchDiff / diff) * speed
121120

122-
val yaw = yaw + yawDiff.coerceIn(-yawSpeed, yawSpeed)
123-
val pitch = pitch + pitchDiff.coerceIn(-pitchSpeed, pitchSpeed)
121+
val yaw = wrap(yaw + yawDiff.coerceIn(-yawSpeed, yawSpeed))
122+
val pitch = wrap(pitch + pitchDiff.coerceIn(-pitchSpeed, pitchSpeed))
124123

125124
return Rotation(yaw, pitch)
126125
}

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
package com.lambda.interaction.request.rotation.visibilty
1919

2020
import com.lambda.interaction.request.rotation.Rotation
21-
import com.lambda.util.Communication.info
2221
import net.minecraft.entity.Entity
2322
import net.minecraft.util.math.Direction
2423
import net.minecraft.util.math.MathHelper
24+
import net.minecraft.util.math.MathHelper.wrapDegrees
2525
import net.minecraft.util.math.Vec3i
2626

2727
enum class PlaceDirection(
@@ -33,37 +33,35 @@ enum class PlaceDirection(
3333
Down ( 0.0, 90.0, 0, -1, 0, listOf(Double.MIN_VALUE..Double.MAX_VALUE)),
3434

3535
UpNorth ( -180.0, -90.0, 0, 1, -1, northYawRanges),
36-
UpSouth ( 0.0, -90.0, 0, 1, 1, listOf(southYawRange)),
37-
UpWest ( 90.0, -90.0, 1, 1, 0, listOf(westYawRange)),
38-
UpEast ( -90.0, -90.0, -1, 1, 0, listOf(eastYawRange)),
36+
UpSouth ( 0.0, -90.0, 0, 1, 1, listOf(southYawRange)),
37+
UpWest ( 90.0, -90.0, 1, 1, 0, listOf(westYawRange)),
38+
UpEast ( -90.0, -90.0, -1, 1, 0, listOf(eastYawRange)),
3939

4040
DownNorth( -180.0, 90.0, 0, -1, -1, northYawRanges),
41-
DownSouth( 0.0, 90.0, 0, -1, 1, listOf(southYawRange)),
42-
DownWest ( 90.0, 90.0, 1, -1, 0, listOf(westYawRange)),
43-
DownEast ( -90.0, 90.0, -1, -1, 0, listOf(eastYawRange)),
41+
DownSouth( 0.0, 90.0, 0, -1, 1, listOf(southYawRange)),
42+
DownWest ( 90.0, 90.0, 1, -1, 0, listOf(westYawRange)),
43+
DownEast ( -90.0, 90.0, -1, -1, 0, listOf(eastYawRange)),
4444

4545
North ( -180.0, 0.0, 0, 0, -1, northYawRanges),
46-
South ( 0.0, 0.0, 0, 0, 1, listOf(southYawRange)),
47-
West ( 90.0, 0.0, 1, 0, 0, listOf(westYawRange)),
48-
East ( -90.0, 0.0, -1, 0, 0, listOf(eastYawRange));
46+
South ( 0.0, 0.0, 0, 0, 1, listOf(southYawRange)),
47+
West ( 90.0, 0.0, 1, 0, 0, listOf(westYawRange)),
48+
East ( -90.0, 0.0, -1, 0, 0, listOf(eastYawRange));
4949

5050
constructor(yaw: Double, pitch: Double, x: Int, y: Int, z: Int, yawRanges: List<ClosedRange<Double>>)
5151
: this(Rotation(yaw, pitch), Vec3i(x, y, z), yawRanges)
5252

53+
//ToDo: Add dynamic pitch border calculations to avoid excess rotation distance
5354
fun snapToArea(rot: Rotation): Rotation {
54-
// if (isInArea(rot)) return rot
55+
if (isInArea(rot)) return rot
5556

56-
//ToDo: fix snapping to a given directions area to speed up rotations in the case they are not instant
57-
// val normalizedYaw = wrapDegrees(rot.yaw)
58-
// val clampedYaw = when {
59-
// this.rotation.yaw != 180.0 -> normalizedYaw.coerceIn(yawRanges[0])
60-
// normalizedYaw < 0 -> normalizedYaw.coerceIn(yawRanges[0])
61-
// else -> normalizedYaw.coerceIn(yawRanges[1])
62-
// }
63-
64-
info("$rotation")
57+
val normalizedYaw = wrapDegrees(rot.yaw)
58+
val clampedYaw = when {
59+
this.rotation.yaw != -180.0 -> normalizedYaw.coerceIn(yawRanges[0])
60+
normalizedYaw < 0 -> normalizedYaw.coerceIn(yawRanges[0])
61+
else -> normalizedYaw.coerceIn(yawRanges[1])
62+
}
6563

66-
return Rotation(rotation.yaw, rotation.pitch)
64+
return Rotation(clampedYaw, this.rotation.pitch)
6765
}
6866

6967
fun isInArea(rot: Rotation) = fromRotation(rot) == this
@@ -117,8 +115,9 @@ enum class PlaceDirection(
117115
}
118116
}
119117

120-
// North and south take priority at borders. Same rule applies with up and down over horizontal directions
121-
val northYawRanges = listOf(-180.0..-135.0, 135.0..180.0)
122-
val southYawRange = -45.0..45.0
123-
val eastYawRange = -134.99..-45.01
124-
val westYawRange = 45.01..134.99
118+
const val FUDGE_FACTOR = 0.01
119+
120+
val northYawRanges = listOf(-180.0..(-135.0 - FUDGE_FACTOR), (135.0 + FUDGE_FACTOR)..180.0)
121+
val southYawRange = ( -45.0 + FUDGE_FACTOR)..( 45.0 - FUDGE_FACTOR)
122+
val eastYawRange = (-135.0 + FUDGE_FACTOR)..(-45.0 - FUDGE_FACTOR)
123+
val westYawRange = ( 45.0 + FUDGE_FACTOR)..(135.0 - FUDGE_FACTOR)

0 commit comments

Comments
 (0)