Skip to content

Commit

Permalink
Entity dragging tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePlasticPotato committed Dec 30, 2024
1 parent 590ed87 commit a798855
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import net.minecraft.world.level.Level
import net.minecraft.world.phys.Vec3
import org.valkyrienskies.core.api.ships.LoadedShip
import org.valkyrienskies.mod.common.util.EntityDragger.serversideEyeRotationOrDefault
import org.valkyrienskies.mod.common.util.EntityLerper.yawToWorld
import org.valkyrienskies.mod.common.util.IEntityDraggingInformationProvider
import org.valkyrienskies.mod.common.util.toJOML
import org.valkyrienskies.mod.common.util.toMinecraft
import org.valkyrienskies.mod.mixin.accessors.entity.EntityAccessor
Expand Down Expand Up @@ -39,10 +41,10 @@ object PlayerUtil {
val yaw = -atan2(direction.x, direction.z)
val pitch = -atan2(direction.y, sqrt((direction.x * direction.x) + (direction.z * direction.z)))

player.yRot = player.serversideEyeRotationOrDefault(yaw * (180 / Math.PI)).toFloat()
player.yRot = (yaw * (180.0 / Math.PI)).toFloat()
player.yHeadRot = player.yRot

player.xRot = (pitch * (180 / Math.PI)).toFloat()
player.xRot = (pitch * (180.0 / Math.PI)).toFloat()
(player as EntityAccessor).setPosNoUpdates(position.toMinecraft())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.minecraft.core.Registry
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerLevel
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.entity.player.Player
import net.minecraft.world.phys.Vec3
import org.joml.Vector3d
import org.valkyrienskies.core.api.attachment.getAttachment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.joml.Vector3d
import org.joml.Vector3dc
import org.valkyrienskies.core.api.ships.ClientShip
import org.valkyrienskies.core.api.ships.Ship
import org.valkyrienskies.mod.common.getShipObjectManagingPos
import org.valkyrienskies.mod.common.toWorldCoordinates
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin
Expand All @@ -17,24 +19,48 @@ object EntityLerper {
* Called from preAiStep. This function lerps the entity's movement while keeping it locked relative to the ship.
*/
fun lerpStep(dragInfo: EntityDraggingInformation, refship: Ship, entity: Entity) {
if (refship !is ClientShip) {
if (refship !is ClientShip || !dragInfo.isEntityBeingDraggedByAShip()) {
return
}
val ship = refship as ClientShip
if (dragInfo.lerpSteps > 0) {
if (dragInfo.changedShipLastTick) {
dragInfo.lerpSteps = 0
//dragInfo.lerpSteps = 0
dragInfo.changedShipLastTick = false
val transformed = if (dragInfo.lerpPositionOnShip != null) {
ship.transform.shipToWorld.transformPosition(dragInfo.lerpPositionOnShip, Vector3d())
} else entity.position().toJOML()
val transformedYaw = if (dragInfo.lerpYawOnShip != null) {
yawToWorld(ship, dragInfo.lerpYawOnShip!!)
} else entity.yRot.toDouble()
entity.setPos(transformed.x, transformed.y, transformed.z)
entity.yRot = transformedYaw.toFloat()
return


// val transformed = if (dragInfo.lerpPositionOnShip != null) {
// entity.level.toWorldCoordinates(Vector3d(dragInfo.lerpPositionOnShip))
// } else entity.position().toJOML()
// val transformedYaw = if (dragInfo.lerpYawOnShip != null) {
// yawToWorld(ship, dragInfo.lerpYawOnShip!!)
// } else entity.yRot.toDouble()
//
// if (dragInfo.relativePositionOnShip != null) {
// val newX: Double = dragInfo.relativePositionOnShip!!.x() + (dragInfo.lerpPositionOnShip - currentX) / dragInfo.lerpSteps
// val newY: Double = currentY + (lerpY - currentY) / dragInfo.lerpSteps
// val newZ: Double = currentZ + (lerpZ - currentZ) / dragInfo.lerpSteps
//
// entity.setPos(transformed.x, transformed.y, transformed.z)
// entity.yRot = transformedYaw.toFloat()
// }
//dragInfo.lerpSteps--
//return
}

val lerpship = entity.level.getShipObjectManagingPos(dragInfo.lerpPositionOnShip!!)
val posship = entity.level.getShipObjectManagingPos(dragInfo.relativePositionOnShip!!)
if (dragInfo.lerpPositionOnShip != null && lerpship != null && lerpship.id != ship.id) {
//transform it to the new ship
val worldPos = lerpship.transform.shipToWorld.transformPosition(Vector3d(dragInfo.lerpPositionOnShip), Vector3d())
dragInfo.lerpPositionOnShip = ship.transform.worldToShip.transformPosition(worldPos, Vector3d())

}
if (dragInfo.relativePositionOnShip != null && posship != null && posship.id != ship.id) {
val worldRelativePos = posship.shipToWorld.transformPosition(Vector3d(dragInfo.relativePositionOnShip), Vector3d())
dragInfo.relativePositionOnShip = ship.transform.worldToShip.transformPosition(worldRelativePos, Vector3d())
}

val currentX: Double = dragInfo.relativePositionOnShip?.x() ?: return
val currentY: Double = dragInfo.relativePositionOnShip!!.y()
val currentZ: Double = dragInfo.relativePositionOnShip!!.z()
Expand All @@ -50,7 +76,7 @@ object EntityLerper {
val newY: Double = currentY + (lerpY - currentY) / dragInfo.lerpSteps
val newZ: Double = currentZ + (lerpZ - currentZ) / dragInfo.lerpSteps

val newPos = ship.shipToWorld.transformPosition(newX, newY, newZ, Vector3d())
val newPos = entity.level.toWorldCoordinates(newX, newY, newZ, Vector3d())

val currentYawWorld = yawToWorld(ship, currentYaw)
val lerpYawWorld = yawToWorld(ship, lerpYaw)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.valkyrienskies.core.api.ships.Ship
import org.valkyrienskies.core.apigame.collision.ConvexPolygonc
import org.valkyrienskies.core.util.extend
import org.valkyrienskies.mod.common.dimensionId
import org.valkyrienskies.mod.common.getShipObjectManagingPos
import org.valkyrienskies.mod.common.getShipsIntersecting
import org.valkyrienskies.mod.common.shipObjectWorld
import org.valkyrienskies.mod.common.vsCore
Expand Down Expand Up @@ -93,7 +94,8 @@ object EntityShipCollisionUtils {
movement.toJOML(), entityBoundingBox.toJOML(), stepHeight, collidingShipPolygons
)
if (entity != null) {
if (shipCollidingWith != null) {
val standingOnShip = entity.level.getShipObjectManagingPos(entity.onPos)
if (shipCollidingWith != null && standingOnShip != null && standingOnShip.id == shipCollidingWith) {
// Update the [IEntity.lastShipStoodOn]
(entity as IEntityDraggingInformationProvider).draggingInformation.lastShipStoodOn = shipCollidingWith
for (entityRiding in entity.indirectPassengers) {
Expand Down

0 comments on commit a798855

Please sign in to comment.