-1
\$\begingroup\$

I am coding a custom client for Minecraft using MCP. (Please no hate, I'm not making a hacked client!) I have two variables currentYaw which is the current yaw rotation of the player, and targetYaw which is the yaw rotation that I want the player to be looking at. I have a function called lerpAngle and here is the code for it:

public static float lerpAngle(float fromRadians, float toRadians, float progress) {
    float delta = ((toRadians - fromRadians + PI2 + PI) % PI2) - PI;
    return (fromRadians + delta * progress + PI2) % PI2;
}

There is a float in minecraft.timer called elapsedPartialTicks. This variable is always between 0 and 1. Now I want to interpolate between currentYaw and targetYaw so that the transition is smooth. I passed in elapsedPartialTicks as progress but the screen moves way too fast. I am running all this code from the tick method.

What can I do to interpolate smoothly between currentYaw and targetYaw, but without the screen moving so fast.

Thank you for all the help!

Please note that I don't need any code, so it doesn't matter if you're answer is based on Forge or MCP. I just need a logic; I can write the code myself.

Again, thank you so much!

EDIT: In my current implementation, I have the following code:

float progress = (cameraInterpolationTimer.getCurrentTicks() + minecraft.timer.elapsedPartialTicks) / cameraInterpolationTimer.getTargetTicks();
minecraft.thePlayer.rotationYaw = (float) Math.toDegrees(InterpolationMath.angleLinearInterpolate((float) Math.toRadians(minecraft.thePlayer.rotationYaw), (float) Math.toRadians(yaw), progress));
minecraft.thePlayer.rotationPitch = (float) Math.toDegrees(InterpolationMath.angleLinearInterpolate((float) Math.toRadians(minecraft.thePlayer.rotationPitch), (float) Math.toRadians(pitch), progress));

The variables pitch and yaw are the rotations that I intend on facing. I also have variables prevPitch and prevYaw, which are set to pitch and yaw at the end of the function. cameraInterpolationTimer is a repeating timer (which is currently at a fixed interval) which gets reset when the difference between yaw and prevYaw is too large (because the target is moving; thus the pitch and yaw variables change each time the function is called). The function getCurrentTicks in the timer returns the amount of time that is passed since the timer was either created or reset. getTargetTicks is just another way of returning the fixed interval that the timer has been set for.

This should be interpolating smoothly to face the target yaw and pitch. It is working, but there is a mild stutter. It is pretty mild, but it is very annoying to look at and can be easily seen. How can I solve this?

\$\endgroup\$
1
\$\begingroup\$

Slow down your progress. Let it take more than 1 tick (if I'm reading your situation right).

For example, when starting the transition - choose the desired tick duration (e.g. tickDuration = 10 ticks) and store the startedOnTick. Now in the lerp you can lerp with the progress = (currentTick + elapsedPartialTicks - startedOnTick) / tickDuration. This way you will have your transition take more ticks and be longer.

\$\endgroup\$
3
  • \$\begingroup\$ It's kinda late for this now, but what if the final angle I want to look at changes every single tick? \$\endgroup\$
    – Day Trip
    Oct 19 at 21:53
  • \$\begingroup\$ @DayTrip if the angle changes, you can restart the lerping. You might also want to make tickDuration correlate with the difference between the current and target angles (so that smaller changes take less time to complete) \$\endgroup\$
    – Kromster
    Oct 20 at 5:35
  • \$\begingroup\$ I attached what I tried in my question. There is still an issue :( \$\endgroup\$
    – Day Trip
    Oct 21 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.