From 7458b99bde4ac2c8883bdde0c02d0ec0c0eb8b23 Mon Sep 17 00:00:00 2001 From: TropheusJ Date: Wed, 8 Nov 2023 02:14:13 -0500 Subject: [PATCH] teleporting??? --- .../portalcubed/content/portal/Portal.java | 9 ++++++++ .../content/portal/manager/PortalManager.java | 22 ++++++++++++++----- .../framework/util/TransformUtils.java | 15 +++++-------- .../portalcubed/mixin/EntityMixin.java | 7 +++--- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/github/fusionflux/portalcubed/content/portal/Portal.java b/src/main/java/io/github/fusionflux/portalcubed/content/portal/Portal.java index bf2a3740..465e1ab8 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/content/portal/Portal.java +++ b/src/main/java/io/github/fusionflux/portalcubed/content/portal/Portal.java @@ -14,6 +14,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import org.joml.Quaternionf; +import org.joml.Vector3d; import java.util.Optional; import java.util.UUID; @@ -105,6 +106,14 @@ public boolean isActive() { return this.linked != null; } + public Vector3d relativize(Vector3d pos) { + return pos.sub(origin.x, origin.y, origin.z); + } + + public Vector3d derelativize(Vector3d pos) { + return pos.add(origin.x, origin.y, origin.z); + } + /** * Do not use, use the portal manager */ diff --git a/src/main/java/io/github/fusionflux/portalcubed/content/portal/manager/PortalManager.java b/src/main/java/io/github/fusionflux/portalcubed/content/portal/manager/PortalManager.java index ce2a1aaf..b767d2af 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/content/portal/manager/PortalManager.java +++ b/src/main/java/io/github/fusionflux/portalcubed/content/portal/manager/PortalManager.java @@ -15,6 +15,7 @@ import io.github.fusionflux.portalcubed.framework.util.TransformUtils; import org.jetbrains.annotations.Nullable; +import org.joml.Vector3d; import java.util.List; import java.util.Objects; @@ -65,13 +66,24 @@ private PortalHitResult clipPortal(Portal portal, Vec3 start, Vec3 end) { if (linked == null) // this shouldn't happen return null; // portals cannot be interacted with from behind -// Vec3 delta = end.subtract(start); -// if (delta.dot(portal.normal) > 0) -// return null; + Vec3 delta = end.subtract(start); + if (delta.dot(portal.normal) > 0) + return null; return portal.plane.clip(start, end).map(hit -> { - Vec3 teleportedHit = TransformUtils.applyDual(portal.rotation::transformInverse, linked.rotation::transform, hit); + Vec3 teleportedHit = TransformUtils.apply(hit, + portal::relativize, + portal.rotation::transformInverse, + linked.rotation::transform, + linked::derelativize + ); Vec3 remainder = end.subtract(hit); - Vec3 teleportedEnd = TransformUtils.applyDual(portal.rotation::transformInverse, linked.rotation::transform, remainder); + Vec3 teleportedEnd = TransformUtils.apply(remainder, + portal::relativize, + portal.rotation::transformInverse, + linked.rotation::transform, + linked::derelativize, + portal::derelativize + ); return new PortalHitResult(start, teleportedEnd, hit, teleportedHit, portal, linked); }).orElse(null); diff --git a/src/main/java/io/github/fusionflux/portalcubed/framework/util/TransformUtils.java b/src/main/java/io/github/fusionflux/portalcubed/framework/util/TransformUtils.java index b34def6a..eccff7e7 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/framework/util/TransformUtils.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/util/TransformUtils.java @@ -7,15 +7,12 @@ import net.minecraft.world.phys.Vec3; public class TransformUtils { - public static Vec3 apply(UnaryOperator function, Vec3 input) { - Vector3d asJomlVec = new Vector3d(input.x, input.y, input.z); - Vector3d result = function.apply(asJomlVec); - return new Vec3(result.x, result.y, result.z); - } - - public static Vec3 applyDual(UnaryOperator func1, UnaryOperator func2, Vec3 input) { - Vector3d asJomlVec = new Vector3d(input.x, input.y, input.z); - Vector3d result = func2.apply(func1.apply(asJomlVec)); + @SafeVarargs + public static Vec3 apply(Vec3 input, UnaryOperator... functions) { + Vector3d result = new Vector3d(input.x, input.y, input.z); + for (UnaryOperator function : functions) { + result = function.apply(result); + } return new Vec3(result.x, result.y, result.z); } } diff --git a/src/main/java/io/github/fusionflux/portalcubed/mixin/EntityMixin.java b/src/main/java/io/github/fusionflux/portalcubed/mixin/EntityMixin.java index 4bfd5864..3dce938e 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/mixin/EntityMixin.java +++ b/src/main/java/io/github/fusionflux/portalcubed/mixin/EntityMixin.java @@ -41,19 +41,20 @@ public abstract class EntityMixin { ) ) private void moveThroughPortals(Args args) { - if (this.getType().is(PortalCubedEntityTags.PORTAL_BLACKLIST)) + Level level = level(); + if (level.isClientSide || this.getType().is(PortalCubedEntityTags.PORTAL_BLACKLIST)) return; Vec3 oldPos = position(); Vec3 newPos = new Vec3(args.get(0), args.get(1), args.get(2)); - PortalManager manager = PortalManager.of(level()); + PortalManager manager = PortalManager.of(level); PortalHitResult result = manager.clipPortal(oldPos, newPos); if (result != null) { Vec3 teleported = result.teleportedEnd(); args.set(0, teleported.x); args.set(1, teleported.y); args.set(2, teleported.z); - System.out.println("entity teleported"); + System.out.println("entity teleported from " + newPos + " to " + teleported); // TODO: should we teleport the old position fields to behind the out portal? } }