store entity rotation as float

This commit is contained in:
Bixilon 2022-12-20 09:08:53 +01:00
parent 2aa261acae
commit cbfa644a31
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
19 changed files with 37 additions and 40 deletions

View File

@ -23,7 +23,7 @@ class PitchRotation(
override val range: FloatRange,
) : RotationProperty {
override fun getValue(rotation: EntityRotation): Double {
override fun getValue(rotation: EntityRotation): Float {
return rotation.pitch
}

View File

@ -22,12 +22,12 @@ import de.bixilon.minosoft.data.entities.entities.Entity
interface RotationProperty : EntityTargetProperty {
val range: FloatRange
fun getValue(rotation: EntityRotation): Double
fun getValue(rotation: EntityRotation): Float
override fun passes(properties: EntitySelectorProperties, entity: Entity): Boolean {
val rotation = getValue(entity.rotation)
return rotation.toFloat() in range
return rotation in range
}
}

View File

@ -23,7 +23,7 @@ class YawRotation(
override val range: FloatRange,
) : RotationProperty {
override fun getValue(rotation: EntityRotation): Double {
override fun getValue(rotation: EntityRotation): Float {
return rotation.yaw
}

View File

@ -18,8 +18,8 @@ import de.bixilon.kotlinglm.func.sin
import de.bixilon.kotlinglm.vec3.Vec3
data class EntityRotation(
val yaw: Double,
val pitch: Double,
val yaw: Float,
val pitch: Float,
) {
val front: Vec3
get() = Vec3(
@ -28,13 +28,12 @@ data class EntityRotation(
(yaw + 90).rad.sin * (-pitch).rad.cos
).normalize()
constructor(bodyYaw: Float, pitch: Float) : this(bodyYaw.toDouble(), pitch.toDouble())
override fun toString(): String {
return "(yaw=$yaw, pitch=$pitch)"
}
companion object {
val EMPTY = EntityRotation(0.0, 0.0)
val EMPTY = EntityRotation(0.0f, 0.0f)
}
}

View File

@ -186,8 +186,8 @@ abstract class Entity(
_attachedEntity = vehicleId
}
fun setRotation(yaw: Int, pitch: Int) {
rotation = EntityRotation(yaw.toDouble(), pitch.toDouble())
fun forceSetRotation(rotation: EntityRotation) {
this.rotation = rotation
}
fun setHeadRotation(headYaw: Int) {

View File

@ -127,7 +127,7 @@ abstract class LivingEntity(connection: PlayConnection, entityType: EntityType,
tickStatusEffects()
if (isSleeping) {
rotation = rotation.copy(pitch = 0.0)
rotation = rotation.copy(pitch = 0.0f)
}
}

View File

@ -48,7 +48,7 @@ abstract class PlayerEntity(
entityType: EntityType,
data: EntityData,
position: Vec3d = Vec3d.EMPTY,
rotation: EntityRotation = EntityRotation(0.0, 0.0),
rotation: EntityRotation = EntityRotation.EMPTY,
name: String = "",
properties: PlayerProperties? = null,
val additional: PlayerAdditional = PlayerAdditional(name = name, properties = properties),

View File

@ -30,7 +30,7 @@ class RemotePlayerEntity(
entityType: EntityType,
data: EntityData,
position: Vec3d = Vec3d.EMPTY,
rotation: EntityRotation = EntityRotation(0.0, 0.0),
rotation: EntityRotation = EntityRotation.EMPTY,
name: String = "TBA",
properties: PlayerProperties? = null,
tabListItem: PlayerAdditional = PlayerAdditional(name = name, gamemode = Gamemodes.SURVIVAL, properties = properties),

View File

@ -85,7 +85,7 @@ class LocalPlayerEntity(
account: Account,
connection: PlayConnection,
val privateKey: PlayerPrivateKey?,
) : PlayerEntity(connection, connection.registries.entityTypeRegistry[RemotePlayerEntity.RESOURCE_LOCATION]!!, EntityData(connection), Vec3d.EMPTY, EntityRotation(0.0, 0.0), account.username, account.properties) {
) : PlayerEntity(connection, connection.registries.entityTypeRegistry[RemotePlayerEntity.RESOURCE_LOCATION]!!, EntityData(connection), Vec3d.EMPTY, EntityRotation.EMPTY, account.username, account.properties) {
var healthCondition by observed(HealthCondition())
var experienceCondition by observed(ExperienceCondition())
var compass by observed(CompassPosition())
@ -117,7 +117,7 @@ class LocalPlayerEntity(
// last state (for updating movement on server)
private var lastPositionPacketSent = -1L
private var lastSentPosition = Vec3d.EMPTY
private var lastRotation = EntityRotation(0.0, 0.0)
private var lastRotation = EntityRotation.EMPTY
private var lastSprinting = false
private var lastSneaking = false
private var lastOnGround = false
@ -269,7 +269,7 @@ class LocalPlayerEntity(
val rotation = rotation.copy()
val yawDiff = rotation.yaw - lastRotation.yaw
val pitchDiff = rotation.pitch - lastRotation.pitch
val rotationChanged = yawDiff != 0.0 && pitchDiff != 0.0
val rotationChanged = yawDiff != 0.0f && pitchDiff != 0.0f
val onGround = onGround
@ -309,7 +309,7 @@ class LocalPlayerEntity(
return flyingSpeed
}
private fun calculateVelocity(sidewaysSpeed: Float, forwardSpeed: Float, speed: Double, yaw: Double): Vec3d {
private fun calculateVelocity(sidewaysSpeed: Float, forwardSpeed: Float, speed: Double, yaw: Float): Vec3d {
if (sidewaysSpeed == 0.0f && forwardSpeed == 0.0f) {
return Vec3d.EMPTY
}

View File

@ -17,6 +17,7 @@ import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.kotlinglm.vec4.Vec4i
import de.bixilon.kutil.concurrent.Reference
import de.bixilon.kutil.math.simple.DoubleMath.rounded10
import de.bixilon.kutil.math.simple.FloatMath.rounded10
import de.bixilon.kutil.observer.DataObserver.Companion.observe
import de.bixilon.kutil.string.StringUtil.truncate
import de.bixilon.kutil.unit.UnitFormatter.formatBytes

View File

@ -107,7 +107,7 @@ class CameraInput(
}
yaw %= 180
val pitch = GLM.clamp(delta.y + rotation.pitch, -89.9, 89.9)
return EntityRotation(yaw, pitch)
return EntityRotation(yaw.toFloat(), pitch.toFloat())
}
private companion object {

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.protocol.packets.s2c.play.entity
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.minosoft.data.entities.EntityRotation
import de.bixilon.minosoft.data.entities.data.EntityData
import de.bixilon.minosoft.data.entities.entities.player.PlayerEntity
import de.bixilon.minosoft.data.entities.entities.player.RemotePlayerEntity
@ -51,8 +50,7 @@ class EntityPlayerS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val position: Vec3d = buffer.readVec3d()
val yaw = buffer.readAngle()
val pitch = buffer.readAngle()
val rotation = buffer.readEntityRotation()
if (buffer.versionId < ProtocolVersions.V_15W31A) {
buffer.connection.registries.itemRegistry[buffer.readUnsignedShort()] // current item
}
@ -66,7 +64,7 @@ class EntityPlayerS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
entityType = buffer.connection.registries.entityTypeRegistry[RemotePlayerEntity.RESOURCE_LOCATION]!!,
data = EntityData(buffer.connection, data),
position = position,
rotation = EntityRotation(yaw.toDouble(), pitch.toDouble()),
rotation = rotation,
name = name,
properties = properties,
)

View File

@ -26,8 +26,7 @@ import de.bixilon.minosoft.util.logging.LogMessageType
class MovementRotationS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val entityId: Int = buffer.readEntityId()
var delta: Vec3d = buffer.readPositionDelta()
val yaw: Int = buffer.readAngle()
val pitch: Int = buffer.readAngle()
val rotation = buffer.readEntityRotation()
val onGround = if (buffer.versionId >= ProtocolVersions.V_14W25B) {
buffer.readBoolean()
} else {
@ -37,13 +36,13 @@ class MovementRotationS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
override fun handle(connection: PlayConnection) {
val entity = connection.world.entities[entityId] ?: return
entity.forceMove(delta)
entity.setRotation(yaw, pitch)
entity.forceSetRotation(rotation)
}
override fun log(reducedLog: Boolean) {
if (reducedLog) {
return
}
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Entity movement + rotate (entityId=$entityId, delta=$delta, yaw=$yaw, pitch=$pitch, onGround=$onGround)" }
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Entity movement + rotate (entityId=$entityId, delta=$delta, rotation=$rotation, onGround=$onGround)" }
}
}

View File

@ -29,7 +29,7 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket(threadSafe = false)
class PositionRotationS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val position: Vec3d
val position: Vec3d = buffer.readVec3d()
val rotation: EntityRotation
var isOnGround = false
private var flags: Int = 0
@ -37,7 +37,6 @@ class PositionRotationS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
private var dismountVehicle = true
init {
position = buffer.readVec3d()
rotation = EntityRotation(buffer.readFloat(), buffer.readFloat())
if (buffer.versionId < ProtocolVersions.V_14W03B) {
isOnGround = buffer.readBoolean()

View File

@ -24,8 +24,7 @@ import de.bixilon.minosoft.util.logging.LogMessageType
@LoadPacket
class RotationS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val entityId: Int = buffer.readEntityId()
val yaw: Int = buffer.readAngle()
val pitch: Int = buffer.readAngle()
val rotation = buffer.readEntityRotation()
val onGround = if (buffer.versionId >= ProtocolVersions.V_14W25B) {
buffer.readBoolean()
} else {
@ -34,13 +33,13 @@ class RotationS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
override fun handle(connection: PlayConnection) {
val entity = connection.world.entities[entityId] ?: return
entity.setRotation(yaw, pitch)
entity.forceSetRotation(rotation)
}
override fun log(reducedLog: Boolean) {
if (reducedLog) {
return
}
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Entity rotation (entityId=$entityId, yaw=$yaw, pitch=$pitch, onGround=$onGround)" }
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Entity rotation (entityId=$entityId, rotation=$rotation, onGround=$onGround)" }
}
}

View File

@ -30,8 +30,7 @@ class TeleportS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
} else {
buffer.readVec3d()
}
val yaw: Int = buffer.readAngle()
val pitch: Int = buffer.readAngle()
val rotation = buffer.readEntityRotation()
val onGround = if (buffer.versionId >= ProtocolVersions.V_14W25B) {
buffer.readBoolean()
} else {
@ -41,13 +40,13 @@ class TeleportS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
override fun handle(connection: PlayConnection) {
val entity = connection.world.entities[entityId] ?: return
entity.position = position
entity.setRotation(yaw, pitch)
entity.forceSetRotation(rotation)
}
override fun log(reducedLog: Boolean) {
if (reducedLog) {
return
}
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Entity teleport (entityId=$entityId, position=$position, yaw=$yaw, pitch=$pitch, onGround=$onGround)" }
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Entity teleport (entityId=$entityId, position=$position, rotation=$rotation, onGround=$onGround)" }
}
}

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.protocol.packets.s2c.play.entity.spawn
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.minosoft.data.entities.EntityRotation
import de.bixilon.minosoft.data.entities.data.EntityData
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.modding.event.events.EntitySpawnEvent
@ -45,7 +44,7 @@ class EntityMobSpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
buffer.readVarInt()
}
val position: Vec3d = buffer.readVec3d()
val rotation = EntityRotation(buffer.readAngle().toDouble(), buffer.readAngle().toDouble())
val rotation = buffer.readEntityRotation()
val headYaw = buffer.readAngle()
val velocity = buffer.readVelocity()

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.protocol.packets.s2c.play.entity.spawn
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.minosoft.data.entities.EntityRotation
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.data.registries.DefaultRegistries.ENTITY_OBJECT_REGISTRY
import de.bixilon.minosoft.modding.event.events.EntitySpawnEvent
@ -46,7 +45,7 @@ class EntityObjectSpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
buffer.readVarInt()
}
val position: Vec3d = buffer.readVec3d()
val rotation = EntityRotation(buffer.readAngle().toDouble(), buffer.readAngle().toDouble()) // ToDo: Is yaw/pitch swapped?
val rotation = buffer.readEntityRotation() // ToDo: Is yaw/pitch swapped?
if (buffer.versionId >= ProtocolVersions.V_22W14A) {
val headYaw = buffer.readAngle()
}

View File

@ -21,6 +21,7 @@ import de.bixilon.kotlinglm.vec3.Vec3i
import de.bixilon.kutil.compression.zlib.GzipUtil.decompress
import de.bixilon.kutil.uuid.UUIDUtil.toUUID
import de.bixilon.minosoft.data.direction.Directions
import de.bixilon.minosoft.data.entities.EntityRotation
import de.bixilon.minosoft.data.entities.Poses
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.tags.Tag
@ -283,6 +284,10 @@ open class InByteBuffer {
return (readByte() * ProtocolDefinition.ROTATION_ANGLE_DIVIDER).toInt()
}
fun readEntityRotation(): EntityRotation {
return EntityRotation(readAngle().toFloat(), readAngle().toFloat())
}
fun readVec2f(): Vec2 {
return Vec2(readFloat(), readFloat())
}