Hello,
im trying to make a method that determines a yaw/pitch to look at a certain block. A method with Pythagoras doesn't work because it fails when theres a block in the angle. So my idea was to create a dummy entity at the player position which will try out all yaws/pitches and then ray trace to see what blocks get hit and what yaw/pitch was used to hit that block. So i first created a new EntityLivingBase:
EntityLivingBase playerSim = new EntityLivingBase(mc.theWorld) {
@Override
public ItemStack getHeldItem() {
return null;
}
@Override
public ItemStack getEquipmentInSlot(int slotIn) {
return null;
}
@Override
public ItemStack getCurrentArmor(int slotIn) {
return null;
}
@Override
public void setCurrentItemOrArmor(int slotIn, ItemStack stack) {
}
@Override
public ItemStack[] getInventory() {
return new ItemStack[0];
}
};
Then i wrote the method that trys out all yaws and pitches:
public ArrayList<Pair<BlockPos, Pair<Float, Float>>> getLookForAllVisibleBlocks(EntityLivingBase entityLivingBase) {
entityLivingBase.setPosition(mc.thePlayer.getPosition().getX() + 0.5d, mc.thePlayer.getPosition().getY(), mc.thePlayer.getPosition().getZ() + 0.5d );
ArrayList<Pair<BlockPos, Pair<Float, Float>>> visibleBlocksWithLook = new ArrayList<>();
for (int yaw = 0; yaw < 360; yaw++) {
for (int pitch = 0; pitch < 180; pitch++) {
entityLivingBase.rotationPitch = pitch - 90;
entityLivingBase.rotationYawHead = yaw - 180;
Vec3 vec3 = entityLivingBase.getPositionEyes(1);
Vec3 vec31 = entityLivingBase.getLook(1);
Vec3 vec32 = vec3.addVector(vec31.xCoord * 5, vec31.yCoord * 5, vec31.zCoord * 5);
MovingObjectPosition playerSimRay = mc.theWorld.rayTraceBlocks(vec3, vec32, false, false, true);
boolean add = true;
for (Pair<BlockPos, Pair<Float, Float>> visibleBlock: visibleBlocksWithLook) {
if (visibleBlock.getLeft().equals(playerSimRay.getBlockPos())) {
add = false;
}
}
if (add) {
visibleBlocksWithLook.add(Pair.of(playerSimRay.getBlockPos(), Pair.of((float) (yaw - 180), (float) (pitch - 90))));
}
}
}
return visibleBlocksWithLook;
}
This indeed returns a pitch and a yaw but its failing 50% of the time and i dont have any idea why it does. My guess is that the rotationPitch/rotationYawHead are different from whats used in the f3 menu. I tried out to log the yaw and pitch and the pitch seemed to be exactly the same but the yaw was different but it seemed like it was a multiple of the rotations in deg and since this method only counts up to 360 (for the yaw) it shouldnt be a problem. There also could be something else wrong with this. Thanks for the help in advance.
EDIT:
The Ray Trace can't be the problem since i tested it out. I also tested the yaw and pitch and they are also correct. I have no clue at all what the problem could be.