From 5b3eb5a375d044d663babc5f0d71fa479d5a6104 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 6 Aug 2018 19:16:33 -0500 Subject: [PATCH] Add rotation method to respect relative angles --- .../bot/pathing/movement/Movement.java | 2 +- .../movement/movements/MovementTraverse.java | 4 ++-- src/main/java/baritone/bot/utils/Helper.java | 8 ++++++-- .../java/baritone/bot/utils/Rotation.java | 1 + src/main/java/baritone/bot/utils/Utils.java | 20 ++++++++++++++++++- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/java/baritone/bot/pathing/movement/Movement.java b/src/main/java/baritone/bot/pathing/movement/Movement.java index 1f491d7b..80440574 100644 --- a/src/main/java/baritone/bot/pathing/movement/Movement.java +++ b/src/main/java/baritone/bot/pathing/movement/Movement.java @@ -178,6 +178,6 @@ public abstract class Movement implements Helper, MovementHelper { } protected void moveTowards(BlockPos pos) { - currentState.setTarget(new MovementState.MovementTarget(new Rotation(Utils.calcRotationFromVec3d(playerHead(), Utils.calcCenterFromCoords(pos, world())).getFirst(), player().rotationPitch))).setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); + currentState.setTarget(new MovementState.MovementTarget(new Rotation(Utils.calcRotationFromVec3d(playerHead(), Utils.calcCenterFromCoords(pos, world()), playerRotations()).getFirst(), player().rotationPitch))).setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); } } diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java index 2c1f7acd..bf314d7f 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java @@ -131,7 +131,7 @@ public class MovementTraverse extends Movement { double faceX = (dest.getX() + against1.getX() + 1.0D) * 0.5D; double faceY = (dest.getY() + against1.getY()) * 0.5D; double faceZ = (dest.getZ() + against1.getZ() + 1.0D) * 0.5D; - state.setTarget(new MovementState.MovementTarget(Utils.calcRotationFromVec3d(playerHead(), new Vec3d(faceX, faceY, faceZ)))); + state.setTarget(new MovementState.MovementTarget(Utils.calcRotationFromVec3d(playerHead(), new Vec3d(faceX, faceY, faceZ), playerRotations()))); EnumFacing side = Minecraft.getMinecraft().objectMouseOver.sideHit; if (Objects.equals(LookBehaviorUtils.getSelectedBlock().orElse(null), against1) && Minecraft.getMinecraft().player.isSneaking()) { @@ -158,7 +158,7 @@ public class MovementTraverse extends Movement { double faceZ = (dest.getZ() + src.getZ() + 1.0D) * 0.5D; //faceX,faceY,faceZ is the middle of the face between from and to BlockPos goalLook = src.down();//this is the block we were just standing on, and the one we want to place against - state.setTarget(new MovementState.MovementTarget(Utils.calcRotationFromVec3d(playerHead(), new Vec3d(faceX, faceY, faceZ)))); + state.setTarget(new MovementState.MovementTarget(Utils.calcRotationFromVec3d(playerHead(), new Vec3d(faceX, faceY, faceZ), playerRotations()))); state.setInput(InputOverrideHandler.Input.MOVE_BACK, true); state.setInput(InputOverrideHandler.Input.SNEAK, true); diff --git a/src/main/java/baritone/bot/utils/Helper.java b/src/main/java/baritone/bot/utils/Helper.java index e5f0a736..730b4642 100755 --- a/src/main/java/baritone/bot/utils/Helper.java +++ b/src/main/java/baritone/bot/utils/Helper.java @@ -24,11 +24,15 @@ public interface Helper { } default BlockPos playerFeet() { - return new BlockPos(mc.player.posX, mc.player.posY, mc.player.posZ); + return new BlockPos(player().posX, player().posY, player().posZ); } default Vec3d playerHead() { - return new Vec3d(mc.player.posX, mc.player.posY + mc.player.getEyeHeight(), mc.player.posZ); + return new Vec3d(player().posX, player().posY + player().getEyeHeight(), player().posZ); + } + + default Rotation playerRotations() { + return new Rotation(player().rotationYaw, player().rotationPitch); } default void displayChatMessageRaw(String message) { diff --git a/src/main/java/baritone/bot/utils/Rotation.java b/src/main/java/baritone/bot/utils/Rotation.java index cc5232ff..177c879a 100644 --- a/src/main/java/baritone/bot/utils/Rotation.java +++ b/src/main/java/baritone/bot/utils/Rotation.java @@ -3,6 +3,7 @@ package baritone.bot.utils; import net.minecraft.util.Tuple; public class Rotation extends Tuple { + public Rotation(Float yaw, Float pitch) { super(yaw, pitch); } diff --git a/src/main/java/baritone/bot/utils/Utils.java b/src/main/java/baritone/bot/utils/Utils.java index 3bec7086..20452532 100755 --- a/src/main/java/baritone/bot/utils/Utils.java +++ b/src/main/java/baritone/bot/utils/Utils.java @@ -1,9 +1,9 @@ package baritone.bot.utils; -import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; @@ -32,6 +32,17 @@ public final class Utils { (float) (pitch * 180 / Math.PI)); } + /** + * Calculates rotation to given Vecdest from Vecorig + * + * @param orig + * @param dest + * @return Rotation {@link Rotation} + */ + public static Rotation calcRotationFromVec3d(Vec3d orig, Vec3d dest, Rotation current) { + return wrapAnglesToRelative(current, calcRotationFromVec3d(orig, dest)); + } + public static Vec3d calcCenterFromCoords(BlockPos orig, World world) { IBlockState b = BlockStateInterface.get(orig); AxisAlignedBB bbox = b.getBoundingBox(world, orig); @@ -43,6 +54,13 @@ public final class Utils { orig.getZ() + zDiff); } + public static Rotation wrapAnglesToRelative(Rotation current, Rotation target) { + return new Rotation( + MathHelper.wrapDegrees(target.getFirst() - current.getFirst()) + current.getFirst(), + MathHelper.wrapDegrees(target.getSecond() - current.getSecond()) + current.getSecond() + ); + } + public static Vec3d vec3dFromBlockPos(BlockPos orig) { return new Vec3d(orig.getX() + 0.0D, orig.getY() + 0.0D, orig.getZ() + 0.0D); }