Skip to content

Commit

Permalink
teleporting???
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Nov 8, 2023
1 parent 3924f0c commit 7458b99
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
import net.minecraft.world.phys.Vec3;

public class TransformUtils {
public static Vec3 apply(UnaryOperator<Vector3d> 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<Vector3d> func1, UnaryOperator<Vector3d> 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<Vector3d>... functions) {
Vector3d result = new Vector3d(input.x, input.y, input.z);
for (UnaryOperator<Vector3d> function : functions) {
result = function.apply(result);
}
return new Vec3(result.x, result.y, result.z);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
}
Expand Down

0 comments on commit 7458b99

Please sign in to comment.