Fixed encorrect angles calculated for differing y-positions, and properly skip out calculations when not looking at a block. Closes #123

This commit is contained in:
Sollace 2019-07-15 10:21:15 +02:00
parent 5ccb81bc11
commit 49f394e367

View file

@ -52,21 +52,36 @@ public class HorseCam {
* B- \ * B- \
* |y - \ * |y - \
* | - \ Tan(?) = horDist / toHeight * | - \ Tan(?) = horDist / toHeight
* |-------C ? = arcTan(horDist / toHeight); *==|-------C=== ? = arcTan(horDist / toHeight);
* horDist * horDist
*
* horDist
* |-------C
* | /.
* | /.
* | / .
* | / .
* | / .
* |?/ .
* A/ .
* | .
* | .
* | .
* | .
* B
* |
* |
*==o===========
*/ */
MinecraftClient client = MinecraftClient.getInstance(); MinecraftClient client = MinecraftClient.getInstance();
PlayerEntity player = client.player; PlayerEntity player = client.player;
client.gameRenderer.updateTargetedEntity(1);
HitResult hit = client.hitResult; HitResult hit = client.hitResult;
// noop // noop
if (hit == null || player == null) { // Ignore misses, helps with bows, arrows, and projectiles
return originalPitch; if (hit == null || hit.getType() != HitResult.Type.BLOCK || player == null) {
}
// Small angles aren't worth changing.
// Helps with bows, arrows, and projectiles.
if (Math.abs(originalPitch) < 2) {
return originalPitch; return originalPitch;
} }
@ -75,17 +90,22 @@ public class HorseCam {
double diffX = Math.abs(hitPos.x - pos.x); double diffX = Math.abs(hitPos.x - pos.x);
double diffZ = Math.abs(hitPos.z - pos.z); double diffZ = Math.abs(hitPos.z - pos.z);
double horDist = Math.sqrt(diffX * diffX + diffZ * diffZ); double horDist = Math.sqrt(diffX * diffX + diffZ * diffZ);
float theta = (float)Math.atan(horDist / toHeight); double toEyePos = pos.y + toHeight;
double verDist = Math.abs(hitPos.y - toEyePos);
double theta = Math.atan(horDist / verDist);
// convert to degress // convert to degress
theta /= Math.PI / 180; theta /= Math.PI / 180D;
// convert to vertical pitch (-90 to 90). // convert to vertical pitch (-90 to 90).
// Preserve up/down direction. // Preserve up/down direction.
float newPitch = (90 - theta) * Math.signum(originalPitch); double newPitch = Math.abs(90 - theta) * Math.signum(originalPitch);
return newPitch; return (float)newPitch;
} }
} }