diff --git a/src/main/java/com/simibubi/create/content/optics/Beam.java b/src/main/java/com/simibubi/create/content/optics/Beam.java index c5b62c805..1f85d4b68 100644 --- a/src/main/java/com/simibubi/create/content/optics/Beam.java +++ b/src/main/java/com/simibubi/create/content/optics/Beam.java @@ -8,10 +8,12 @@ import java.util.Set; import javax.annotation.Nullable; import com.mojang.blaze3d.matrix.MatrixStack; +import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.VecHelper; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.item.DyeColor; +import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.vector.Vector3d; import net.minecraft.world.World; @@ -122,4 +124,36 @@ public class Beam extends ArrayList { public Beam getParent() { return parent; } + + @Nullable + public Direction getBlockEnterFace(BlockPos blockPos) { + Vector3d beamDir = getDirection(); + if (beamDir == null) + return null; + Vector3d pos = VecHelper.getCenterOf(blockPos); + + Direction minDir = null; + double lambda = Double.MAX_VALUE; + + for (Direction testDir : Iterate.directions) { + Vector3d faceNormal = Vector3d.of(testDir.getDirectionVec()); + Vector3d posOnSide = faceNormal.scale(.5) + .add(pos); + double dotProd = faceNormal.dotProduct(beamDir); + if (dotProd == 0) + continue; + + double newLambda = (faceNormal.dotProduct(posOnSide) - faceNormal.dotProduct(get(0) + .getStart())) / dotProd; + Vector3d offset = get(0).getStart() + .add(beamDir.scale(newLambda)) + .subtract(posOnSide); + if (newLambda < lambda && (Math.abs(offset.x) < .5 && Math.abs(offset.y) < .5 && Math.abs(offset.z) < .5)) { + lambda = newLambda; + minDir = testDir; + } + + } + return minDir; + } } diff --git a/src/main/java/com/simibubi/create/content/optics/aligner/AlignerBehaviour.java b/src/main/java/com/simibubi/create/content/optics/aligner/AlignerBehaviour.java index 76868b389..572dbfccb 100644 --- a/src/main/java/com/simibubi/create/content/optics/aligner/AlignerBehaviour.java +++ b/src/main/java/com/simibubi/create/content/optics/aligner/AlignerBehaviour.java @@ -82,8 +82,12 @@ public class AlignerBehaviour extends AbstractLightHandlingBehaviour constructSubBeams(Beam beam) { + if (beam == collectedBeam) + return Stream.empty(); Vector3d beamDir = beam.getDirection(); - if (!beam.isRemoved() && beamDir != null && AngleHelper.deg(Math.cos(beamDir.dotProduct(getFacingVec()))) < 60) { + if (!beam.isRemoved() && beamDir != null && AngleHelper.deg(Math.cos(beamDir.dotProduct(getFacingVec()))) < 60 + && getFacing().getOpposite() + .equals(beam.getBlockEnterFace(getBlockPos()))) { beams.add(beam); requestBeamUpdate(); }