2021-06-16 20:19:33 +02:00
|
|
|
package com.jozufozu.flywheel.util;
|
2019-12-22 01:31:40 +01:00
|
|
|
|
|
|
|
import net.minecraft.util.Direction;
|
|
|
|
import net.minecraft.util.Direction.Axis;
|
|
|
|
|
|
|
|
public class AngleHelper {
|
|
|
|
|
2021-05-06 23:10:12 +02:00
|
|
|
/**
|
|
|
|
* Legacy method. See {@link #horizontalAngleNew(Direction)} for new method.
|
|
|
|
*/
|
2019-12-22 01:31:40 +01:00
|
|
|
public static float horizontalAngle(Direction facing) {
|
2021-07-15 20:36:24 +02:00
|
|
|
float angle = facing.toYRot();
|
2021-06-30 21:43:54 +02:00
|
|
|
if (facing.getAxis() == Axis.X) angle = -angle;
|
2019-12-22 01:31:40 +01:00
|
|
|
return angle;
|
|
|
|
}
|
2020-02-04 14:48:21 +01:00
|
|
|
|
2021-05-06 23:10:12 +02:00
|
|
|
/**
|
|
|
|
* Same as {@link #horizontalAngle(Direction)}, but returns 0 instead of -90 for vertical directions.
|
|
|
|
*/
|
|
|
|
public static float horizontalAngleNew(Direction facing) {
|
2021-06-30 21:43:54 +02:00
|
|
|
if (facing.getAxis()
|
|
|
|
.isVertical()) {
|
2021-05-06 23:10:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2021-07-15 20:36:24 +02:00
|
|
|
float angle = facing.toYRot();
|
2021-06-30 21:43:54 +02:00
|
|
|
if (facing.getAxis() == Axis.X) angle = -angle;
|
2021-05-06 23:10:12 +02:00
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
2020-02-04 14:48:21 +01:00
|
|
|
public static float verticalAngle(Direction facing) {
|
|
|
|
return facing == Direction.UP ? -90 : facing == Direction.DOWN ? 90 : 0;
|
|
|
|
}
|
|
|
|
|
2020-05-25 19:58:13 +02:00
|
|
|
public static float rad(double angle) {
|
2021-06-30 21:43:54 +02:00
|
|
|
if (angle == 0) return 0;
|
2020-02-04 14:48:21 +01:00
|
|
|
return (float) (angle / 180 * Math.PI);
|
|
|
|
}
|
2020-04-29 23:32:17 +02:00
|
|
|
|
2020-05-25 19:58:13 +02:00
|
|
|
public static float deg(double angle) {
|
2021-06-30 21:43:54 +02:00
|
|
|
if (angle == 0) return 0;
|
2020-02-15 18:54:23 +01:00
|
|
|
return (float) (angle * 180 / Math.PI);
|
|
|
|
}
|
2020-04-29 23:32:17 +02:00
|
|
|
|
2020-08-24 21:02:03 +02:00
|
|
|
public static float angleLerp(double pct, double current, double target) {
|
|
|
|
return (float) (current + getShortestAngleDiff(current, target) * pct);
|
2020-02-15 18:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 23:32:17 +02:00
|
|
|
public static float getShortestAngleDiff(double current, double target) {
|
2020-02-15 18:54:23 +01:00
|
|
|
current = current % 360;
|
|
|
|
target = target % 360;
|
|
|
|
return (float) (((((target - current) % 360) + 540) % 360) - 180);
|
|
|
|
}
|
2020-02-04 14:48:21 +01:00
|
|
|
|
2019-12-22 01:31:40 +01:00
|
|
|
}
|