baritone/src/main/java/baritone/bot/utils/Utils.java

57 lines
2.1 KiB
Java
Raw Normal View History

2018-08-01 17:10:48 +00:00
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;
2018-08-02 14:59:51 +00:00
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
2018-08-01 17:10:48 +00:00
/**
* @author Brady
* @since 8/1/2018 12:56 AM
*/
public final class Utils {
2018-08-06 00:54:30 +00:00
public static Rotation calcRotationFromCoords(BlockPos orig, BlockPos dest) {
2018-08-02 14:59:51 +00:00
return calcRotationFromVec3d(vec3dFromBlockPos(orig), vec3dFromBlockPos(dest));
}
/**
* Calculates rotation to given Vec<sub>dest</sub> from Vec<sub>orig</sub>
*
* @param orig
* @param dest
2018-08-06 00:54:30 +00:00
* @return Rotation {@link Rotation}
*/
public static Rotation calcRotationFromVec3d(Vec3d orig, Vec3d dest) {
2018-08-02 14:59:51 +00:00
double yaw = Math.atan2(orig.x - dest.x, -orig.z + dest.z);
2018-08-06 00:08:41 +00:00
double dist = Math.sqrt((orig.x - dest.x) * (orig.x - dest.x) + (-orig.z + dest.z) * (-orig.z + dest.z));
2018-08-02 14:59:51 +00:00
double pitch = Math.atan2(orig.y - dest.y, dist);
return new Rotation((float) (yaw * 180 / Math.PI),
(float) (pitch * 180 / Math.PI));
}
2018-08-02 14:59:51 +00:00
public static Vec3d calcCenterFromCoords(BlockPos orig, World world) {
IBlockState b = BlockStateInterface.get(orig);
AxisAlignedBB bbox = b.getBoundingBox(world, orig);
double xDiff = (bbox.minX + bbox.maxX) / 2;
double yDiff = (bbox.minY + bbox.maxY) / 2;
double zDiff = (bbox.minZ + bbox.maxZ) / 2;
2018-08-02 14:59:51 +00:00
return new Vec3d(orig.getX() + xDiff,
orig.getY() + yDiff,
orig.getZ() + zDiff);
}
2018-08-02 14:01:34 +00:00
2018-08-02 14:59:51 +00:00
public static Vec3d vec3dFromBlockPos(BlockPos orig) {
return new Vec3d(orig.getX() + 0.0D, orig.getY() + 0.0D, orig.getZ() + 0.0D);
}
2018-08-02 14:01:34 +00:00
public static double distanceToCenter(BlockPos pos, double x, double y, double z) {
double xdiff = x - (pos.getX() + 0.5D);
double ydiff = y - (pos.getY() + 0.5D);
double zdiff = z - (pos.getZ() + 0.5D);
return Math.sqrt(xdiff * xdiff + ydiff * ydiff + zdiff * zdiff);
}
2018-08-01 17:10:48 +00:00
}