diff --git a/src/api/java/baritone/api/utils/Rotation.java b/src/api/java/baritone/api/utils/Rotation.java index ca8cb66f..dc697169 100644 --- a/src/api/java/baritone/api/utils/Rotation.java +++ b/src/api/java/baritone/api/utils/Rotation.java @@ -17,11 +17,96 @@ package baritone.api.utils; -import net.minecraft.util.Tuple; +/** + * @author Brady + * @since 9/25/2018 + */ +public class Rotation { -public class Rotation extends Tuple { + /** + * The yaw angle of this Rotation + */ + private float yaw; - public Rotation(Float yaw, Float pitch) { - super(yaw, pitch); + /** + * The pitch angle of this Rotation + */ + private float pitch; + + public Rotation(float yaw, float pitch) { + this.yaw = yaw; + this.pitch = pitch; + } + + /** + * @return The yaw of this rotation + */ + public float getYaw() { + return this.yaw; + } + + /** + * @return The pitch of this rotation + */ + public float getPitch() { + return this.pitch; + } + + /** + * Adds the yaw/pitch of the specified rotations to this + * rotation's yaw/pitch, and returns the result. + * + * @param other Another rotation + * @return The result from adding the other rotation to this rotation + */ + public Rotation add(Rotation other) { + return new Rotation( + this.yaw + other.yaw, + this.pitch + other.pitch + ); + } + + /** + * Subtracts the yaw/pitch of the specified rotations from this + * rotation's yaw/pitch, and returns the result. + * + * @param other Another rotation + * @return The result from subtracting the other rotation from this rotation + */ + public Rotation subtract(Rotation other) { + return new Rotation( + this.yaw - other.yaw, + this.pitch - other.pitch + ); + } + + /** + * @return A copy of this rotation with the pitch clamped + */ + public Rotation clamp() { + return new Rotation( + this.yaw, + RotationUtils.clampPitch(this.pitch) + ); + } + + /** + * @return A copy of this rotation with the yaw normalized + */ + public Rotation normalize() { + return new Rotation( + RotationUtils.normalizeYaw(this.yaw), + this.pitch + ); + } + + /** + * @return A copy of this rotation with the pitch clamped and the yaw normalized + */ + public Rotation normalizeAndClamp() { + return new Rotation( + RotationUtils.normalizeYaw(this.yaw), + RotationUtils.clampPitch(this.pitch) + ); } } diff --git a/src/api/java/baritone/api/utils/RotationUtils.java b/src/api/java/baritone/api/utils/RotationUtils.java new file mode 100644 index 00000000..0df4b38d --- /dev/null +++ b/src/api/java/baritone/api/utils/RotationUtils.java @@ -0,0 +1,54 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.api.utils; + +/** + * @author Brady + * @since 9/25/2018 + */ +public final class RotationUtils { + + private RotationUtils() {} + + /** + * Clamps the specified pitch value between -90 and 90. + * + * @param pitch The input pitch + * @return The clamped pitch + */ + public static float clampPitch(float pitch) { + return Math.max(-90, Math.min(90, pitch)); + } + + /** + * Normalizes the specified yaw value between -180 and 180. + * + * @param yaw The input yaw + * @return The normalized yaw + */ + public static float normalizeYaw(float yaw) { + float newYaw = yaw % 360F; + if (newYaw < -180F) { + newYaw += 360F; + } + if (newYaw >= 180F) { + newYaw -= 360F; + } + return newYaw; + } +} diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index a02ffa51..a42a1e50 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -69,9 +69,9 @@ public final class LookBehavior extends Behavior implements ILookBehavior, Helpe switch (event.getState()) { case PRE: { if (this.force) { - player().rotationYaw = this.target.getFirst(); + player().rotationYaw = this.target.getYaw(); float oldPitch = player().rotationPitch; - float desiredPitch = this.target.getSecond(); + float desiredPitch = this.target.getPitch(); player().rotationPitch = desiredPitch; if (desiredPitch == oldPitch) { nudgeToLevel(); @@ -79,7 +79,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior, Helpe this.target = null; } else if (silent) { this.lastYaw = player().rotationYaw; - player().rotationYaw = this.target.getFirst(); + player().rotationYaw = this.target.getYaw(); } break; } @@ -101,7 +101,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior, Helpe switch (event.getState()) { case PRE: this.lastYaw = player().rotationYaw; - player().rotationYaw = this.target.getFirst(); + player().rotationYaw = this.target.getYaw(); break; case POST: player().rotationYaw = this.lastYaw; diff --git a/src/main/java/baritone/behavior/LookBehaviorUtils.java b/src/main/java/baritone/behavior/LookBehaviorUtils.java index c94f2a1f..8cbe01ab 100644 --- a/src/main/java/baritone/behavior/LookBehaviorUtils.java +++ b/src/main/java/baritone/behavior/LookBehaviorUtils.java @@ -51,10 +51,10 @@ public final class LookBehaviorUtils implements Helper { * @return vector of the rotation */ public static Vec3d calcVec3dFromRotation(Rotation rotation) { - float f = MathHelper.cos(-rotation.getFirst() * (float) DEG_TO_RAD - (float) Math.PI); - float f1 = MathHelper.sin(-rotation.getFirst() * (float) DEG_TO_RAD - (float) Math.PI); - float f2 = -MathHelper.cos(-rotation.getSecond() * (float) DEG_TO_RAD); - float f3 = MathHelper.sin(-rotation.getSecond() * (float) DEG_TO_RAD); + float f = MathHelper.cos(-rotation.getYaw() * (float) DEG_TO_RAD - (float) Math.PI); + float f1 = MathHelper.sin(-rotation.getYaw() * (float) DEG_TO_RAD - (float) Math.PI); + float f2 = -MathHelper.cos(-rotation.getPitch() * (float) DEG_TO_RAD); + float f3 = MathHelper.sin(-rotation.getPitch() * (float) DEG_TO_RAD); return new Vec3d((double) (f1 * f2), (double) f3, (double) (f * f2)); } diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 1b53011a..d614e544 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -463,7 +463,7 @@ public interface MovementHelper extends ActionCosts, Helper { state.setTarget(new MovementTarget( new Rotation(Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F), Utils.getBlockPosCenter(pos), - new Rotation(mc.player.rotationYaw, mc.player.rotationPitch)).getFirst(), mc.player.rotationPitch), + new Rotation(mc.player.rotationYaw, mc.player.rotationPitch)).getYaw(), mc.player.rotationPitch), false )).setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java index 34bcfef3..a16a50a1 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java @@ -165,8 +165,8 @@ public class MovementTraverse extends Movement { // combine the yaw to the center of the destination, and the pitch to the specific block we're trying to break // it's safe to do this since the two blocks we break (in a traverse) are right on top of each other and so will have the same yaw - float yawToDest = Utils.calcRotationFromVec3d(playerHead(), Utils.calcCenterFromCoords(dest, world())).getFirst(); - float pitchToBreak = state.getTarget().getRotation().get().getSecond(); + float yawToDest = Utils.calcRotationFromVec3d(playerHead(), Utils.calcCenterFromCoords(dest, world())).getYaw(); + float pitchToBreak = state.getTarget().getRotation().get().getPitch(); state.setTarget(new MovementState.MovementTarget(new Rotation(yawToDest, pitchToBreak), true)); return state.setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); diff --git a/src/main/java/baritone/utils/Utils.java b/src/main/java/baritone/utils/Utils.java index 12be404b..0de61461 100755 --- a/src/main/java/baritone/utils/Utils.java +++ b/src/main/java/baritone/utils/Utils.java @@ -99,10 +99,7 @@ public final class Utils { } 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() - ); + return target.subtract(current).normalize().add(current); } public static Vec3d vec3dFromBlockPos(BlockPos orig) {