Better Rotation

This commit is contained in:
Brady 2018-09-25 09:39:59 -05:00
parent 9d3392c25c
commit c94ac6e26c
No known key found for this signature in database
GPG Key ID: 73A788379A197567
7 changed files with 155 additions and 19 deletions

View File

@ -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<Float, Float> {
/**
* 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)
);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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;

View File

@ -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));
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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) {