henkelmax / simple-voice-chat

A working voice chat in Minecraft!
https://modrepo.de/minecraft/voicechat/wiki
441 stars 112 forks source link

Improve freecam support #555

Closed MattSturgeon closed 1 year ago

MattSturgeon commented 1 year ago

Improved the check for being in freecam. Instead of checking the distance from the main camera to mc.player, we can check if the render entity is mc.player or not.

public static boolean isFreecamEnabled() {
    return !(mc.player.isSpectator() || mc.player.equals(mc.getCameraEntity()));
}

Support distance-based volume while in freecam. The distance is calculated player-to-player. I.e. the camera's position is ignored.

public static float getDistanceVolume(float maxDistance, Vec3 pos) {
    return PositionalAudioUtils.getDistanceVolume(maxDistance, getReferencePoint(), pos);
}

public static Vec3 getReferencePoint() {
    return isFreecamEnabled() ?
            mc.player.getEyePosition() : mc.gameRenderer.getMainCamera().getPosition();
}

Stereo effects are currently not applied while in freecam. During testing, I couldn't decide if it felt "right"" to hear players talking near mc.player in stereo when looking at them from an outside perspective.

It would be relatively simple to support stereo, by having pretty much all code use FreecamUtil.getReferencePoint() instead of the camera position, and also introducing a FreecamUtil.getRotation() to replace Camera::getYRot:

public static float getYRotation() {
    return isFreecamEnabled() ?
            mc.player.getYRot() : mc.gameRenderer.getMainCamera().getYRot();
}

Using stereo would make freecam less of a special-case in AudioChannel::writeToSpeaker, but would mean more code would have to make use of FreecamUtil methods (e.g. PositionalAudioUtils, the ALSpeaker classes, etc)


FreecamUtil.getDistanceTo(Vec3) is used to check how/if packets should be sent/handled, rather than using the distance from the main camera:

public static double getDistanceTo(Vec3 pos) {
    return getReferencePoint().distanceTo(pos);
}

I also included a commit which removes the freecamSupport config option, since IMO it is only useful for debugging edge-cases. Perhaps you'd rather merge that commit later on after a period of beta-testing? If so, I've set the config option's default value to true.


Testing has been done using hashalite's freecam (I couldn't find any others supporting 1.19.4, I found two for 1.19.3 that wouldn't run). I've tested both with a dedicated server process and with the integrated (open-to-LAN) server.

Thank you for you work, help, discussion, etc on this issue. I look forward to your thoughts and review. :smile:

henkelmax commented 1 year ago

Thank you for your PR. Everything worked perfectly fine. I just made some minor tweaks and cherry picked your first 2 commits.