Merge pull request #68 from PolyacovYury/player_strafing

Implemented player strafing calculation from change in model rotation angle
This commit is contained in:
Sollace 2018-06-10 18:56:22 +02:00 committed by GitHub
commit cf1a2bf058
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,6 +30,13 @@ public class RenderPonyPlayer extends RenderPonyBase {
GlStateManager.translate(0, player.isSneaking() ? 0.2F : -1, 0);
}
private float sensibleAngle(float angle) {
angle %= 360; // if you need to copy-paste a portion of code more than 2 times - make a function
if (angle > 180) angle -= 360;
if (angle < -180) angle += 360;
return angle;
}
@Override
protected void transformPegasusFlight(AbstractClientPlayer player, double motionX, double motionY, double motionZ, float yaw, float pitch, float ticks) {
double dist = Math.sqrt(motionX * motionX + motionZ * motionZ);
@ -52,16 +59,21 @@ public class RenderPonyPlayer extends RenderPonyBase {
double horMotion = Math.sqrt(motionX * motionX + motionZ * motionZ);
if (horMotion > 0) {
yaw = player.cameraYaw - player.rotationYawHead;
double roll = (Math.toDegrees(Math.atan2(motionX, motionZ)) - yaw) % 360;
if (roll < -180) roll += 360;
if (roll > 180) roll -= 360;
roll *= horMotion * 2;
roll = MathHelper.clamp(roll, -54, 54);
yaw = sensibleAngle(player.cameraYaw - player.rotationYawHead); // will need later
// since model roll should probably be calculated from model rotation rather than entity rotation...
double roll = sensibleAngle(player.prevRenderYawOffset - player.renderYawOffset);
float modelYaw = sensibleAngle(player.renderYawOffset);
// filtering ugly jitter that occurs in Vanilla code if motion changes from sideways to diagonal
if (Math.abs(roll) > 0.5f && Math.abs(sensibleAngle(modelYaw + yaw)) > 40) {
return;
}
// detecting that we're flying backwards and roll must be inverted
if (Math.abs(sensibleAngle((float) Math.toDegrees(Math.atan2(motionX, motionZ)) + modelYaw)) > 90) {
roll *= -1; // because inline ifs are not in favor
}
// ayyy magic numbers (after 5 - an approximation of nice looking coefficients calculated by hand)
roll *= horMotion * 5 * (3.6884f * Math.pow(Math.abs(roll), -0.191));
roll = MathHelper.clamp(roll, -54, 54); // safety measure, shouldn't be required anymore
GlStateManager.rotate((float)roll, 0, 0, 1);
}