001/*
002 * This file is part of Baritone.
003 *
004 * Baritone is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Lesser General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * Baritone is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License
015 * along with Baritone.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package baritone.api.utils;
019
020/**
021 * @author Brady
022 * @since 9/25/2018
023 */
024public class Rotation {
025
026    /**
027     * The yaw angle of this Rotation
028     */
029    private float yaw;
030
031    /**
032     * The pitch angle of this Rotation
033     */
034    private float pitch;
035
036    public Rotation(float yaw, float pitch) {
037        this.yaw = yaw;
038        this.pitch = pitch;
039    }
040
041    /**
042     * @return The yaw of this rotation
043     */
044    public float getYaw() {
045        return this.yaw;
046    }
047
048    /**
049     * @return The pitch of this rotation
050     */
051    public float getPitch() {
052        return this.pitch;
053    }
054
055    /**
056     * Adds the yaw/pitch of the specified rotations to this
057     * rotation's yaw/pitch, and returns the result.
058     *
059     * @param other Another rotation
060     * @return The result from adding the other rotation to this rotation
061     */
062    public Rotation add(Rotation other) {
063        return new Rotation(
064                this.yaw + other.yaw,
065                this.pitch + other.pitch
066        );
067    }
068
069    /**
070     * Subtracts the yaw/pitch of the specified rotations from this
071     * rotation's yaw/pitch, and returns the result.
072     *
073     * @param other Another rotation
074     * @return The result from subtracting the other rotation from this rotation
075     */
076    public Rotation subtract(Rotation other) {
077        return new Rotation(
078                this.yaw - other.yaw,
079                this.pitch - other.pitch
080        );
081    }
082
083    /**
084     * @return A copy of this rotation with the pitch clamped
085     */
086    public Rotation clamp() {
087        return new Rotation(
088                this.yaw,
089                clampPitch(this.pitch)
090        );
091    }
092
093    /**
094     * @return A copy of this rotation with the yaw normalized
095     */
096    public Rotation normalize() {
097        return new Rotation(
098                normalizeYaw(this.yaw),
099                this.pitch
100        );
101    }
102
103    /**
104     * @return A copy of this rotation with the pitch clamped and the yaw normalized
105     */
106    public Rotation normalizeAndClamp() {
107        return new Rotation(
108                normalizeYaw(this.yaw),
109                clampPitch(this.pitch)
110        );
111    }
112
113    /**
114     * Is really close to
115     *
116     * @param other another rotation
117     * @return are they really close
118     */
119    public boolean isReallyCloseTo(Rotation other) {
120        return yawIsReallyClose(other) && Math.abs(this.pitch - other.pitch) < 0.01;
121    }
122
123    public boolean yawIsReallyClose(Rotation other) {
124        float yawDiff = Math.abs(normalizeYaw(yaw) - normalizeYaw(other.yaw)); // you cant fool me
125        return (yawDiff < 0.01 || yawDiff > 359.99);
126    }
127
128    /**
129     * Clamps the specified pitch value between -90 and 90.
130     *
131     * @param pitch The input pitch
132     * @return The clamped pitch
133     */
134    public static float clampPitch(float pitch) {
135        return Math.max(-90, Math.min(90, pitch));
136    }
137
138    /**
139     * Normalizes the specified yaw value between -180 and 180.
140     *
141     * @param yaw The input yaw
142     * @return The normalized yaw
143     */
144    public static float normalizeYaw(float yaw) {
145        float newYaw = yaw % 360F;
146        if (newYaw < -180F) {
147            newYaw += 360F;
148        }
149        if (newYaw > 180F) {
150            newYaw -= 360F;
151        }
152        return newYaw;
153    }
154
155    @Override
156    public String toString() {
157        return "Yaw: " + yaw + ", Pitch: " + pitch;
158    }
159}