Skip to content

Commit

Permalink
Fix player offset when sitting on a living entitiy
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Jun 8, 2024
1 parent 0292e4c commit 68b9925
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/dev/tr7zw/firstperson/LogicHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public boolean shouldApplyThirdPerson(boolean thirdPerson) {
* @param defValue
*/
public void updatePositionOffset(Entity entity, Vec3 defValue) {
offset = defValue;
// handle sleeping
if (entity == client.getCameraEntity() && client.player.isSleeping()) {
offset = defValue;
return;
}
double x = 0;
Expand All @@ -102,7 +102,6 @@ public void updatePositionOffset(Entity entity, Vec3 defValue) {
double realYaw;
if ((entity != client.player) || (client.options.getCameraType() != CameraType.FIRST_PERSON)
|| !fpm.isRenderingPlayer()) {
offset = defValue;
return;
}
player = (AbstractClientPlayer) entity;
Expand All @@ -123,7 +122,7 @@ public void updatePositionOffset(Entity entity, Vec3 defValue) {
if (player.getVehicle() instanceof Boat || player.getVehicle() instanceof Minecart) {
realYaw = Mth.rotLerp(client.getFrameTime(), player.yBodyRotO, player.yBodyRot);
} else if (player.getVehicle() instanceof LivingEntity living) {
realYaw = Mth.rotLerp(client.getFrameTime(), living.yBodyRotO, living.yBodyRot);
realYaw = calculateBodyRot(Mth.rotLerp(client.getFrameTime(), living.yBodyRotO, living.yBodyRot), NMSHelper.getYRot(player));
} else {
// Non living entities don't use any custom rotation
// realYaw = Mth.rotLerp(client.getFrameTime(), player.getVehicle().yRotO,
Expand All @@ -146,6 +145,25 @@ public void updatePositionOffset(Entity entity, Vec3 defValue) {
}
offset = new Vec3(x, y, z);
}

private static float calculateBodyRot(float entityBodyRot, float riderHeadRot) {
// Wrap the head rotation to the range [-180, 180]
float wrappedHeadRot = Mth.wrapDegrees(riderHeadRot);

// Calculate the difference between the head and body rotation
float rotDiff = Mth.wrapDegrees(wrappedHeadRot - entityBodyRot);

// If the difference is more than 50 degrees, adjust the body rotation
if (Mth.abs(rotDiff) > 50.0F) {
// Pull the body along with the head
entityBodyRot = wrappedHeadRot - 50.0F * Math.signum(rotDiff);
}

// Ensure the body rotation is wrapped to [-180, 180]
entityBodyRot = Mth.wrapDegrees(entityBodyRot);

return entityBodyRot;
}

/**
* Util method to quicker find where swimming is referenced
Expand Down

0 comments on commit 68b9925

Please sign in to comment.