Skip to content

Instantly share code, notes, and snippets.

@TheGreyGhost
Last active April 28, 2021 13:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheGreyGhost/b5ea2acd1c651a2d6350 to your computer and use it in GitHub Desktop.
Save TheGreyGhost/b5ea2acd1c651a2d6350 to your computer and use it in GitHub Desktop.
EntityBodyHelper (1.8) deobfuscated
/**
*
* @author Nico Bergemann <barracuda415 at yahoo.de>
*/
public class DragonBodyHelper extends EntityBodyHelper
{
private EntityTameableDragon dragon;
private int turnTicks;
private int turnTicksLimit = 20;
private float lastRotationYawHead;
public DragonBodyHelper(EntityTameableDragon dragon) {
super(dragon);
this.dragon = dragon;
}
/**
* The body helper is used to rotate the Dragon based on whether it is moving or not.
* 1) If moving - rotate the body and the head to the movement direction
* 2) If not moving - rotate the body to face where the head is looking
*/
@Override
public void updateRenderAngles()
{
double deltaX = dragon.posX - dragon.prevPosX;
double deltaZ = dragon.posZ - dragon.prevPosZ;
double distSQ = deltaX * deltaX + deltaZ * deltaZ;
float maximumHeadBodyAngleDifference = 90;
final float MOVEMENT_THRESHOLD_SQ = 0.0001F;
// if moving:
// 1) snap the body yaw (renderYawOffset) to the movement direction (rotationYaw)
// 2) constrain the head yaw (rotationYawHead) to be within +/- 90 of the body yaw (renderYawOffset)
if (distSQ > MOVEMENT_THRESHOLD_SQ) {
dragon.renderYawOffset = dragon.rotationYaw;
float newRotationYawHead = MathX.constrainAngle(dragon.getRotationYawHead(), dragon.renderYawOffset,
maximumHeadBodyAngleDifference);
dragon.rotationYawHead = newRotationYawHead;
lastRotationYawHead = newRotationYawHead;
turnTicks = 0;
return;
}
double changeInHeadYaw = Math.abs(dragon.getRotationYawHead() - lastRotationYawHead);
if (dragon.isSitting() || changeInHeadYaw > 15) { // dragon has moved his look position
turnTicks = 0;
lastRotationYawHead = dragon.getRotationYawHead();
} else {
turnTicks++;
// constrain the body yaw to a tighter and tighter zone around the head yaw as time increases
if (turnTicks > turnTicksLimit) {
maximumHeadBodyAngleDifference = Math.max(1 - (float) (turnTicks - turnTicksLimit) / turnTicksLimit, 0) * 75;
}
}
float rotationYawHead = dragon.getRotationYawHead();
dragon.renderYawOffset = MathX.constrainAngle(dragon.renderYawOffset, rotationYawHead, maximumHeadBodyAngleDifference);
dragon.rotationYaw = dragon.renderYawOffset; //bug fix to eliminate client<-->server mismatch
}
}
class MathX {
/** clamp the target angle to within a given range of the centre angle
* @param targetAngle the desired angle (degrees)
* @param centreAngle the centre angle to clamp to (degrees)
* @param maximumDifference the maximum allowable difference between the target and the centre (degrees)
* @return the target angle, clamped to within +/-maximuDifference of the centreAngle.
*/
public static float constrainAngle(float targetAngle, float centreAngle, float maximumDifference) {
return centreAngle + clamp(normDeg(targetAngle - centreAngle), -maximumDifference, maximumDifference);
}
// normalizes a double degrees angle to between +180 and -180
public static double normDeg(double a) {
a %= 360;
if (a >= 180) {
a -= 360;
}
if (a < -180) {
a += 360;
}
return a;
}
// numeric double clamp
public static double clamp(double value, double min, double max) {
return (value < min ? min : (value > max ? max : value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment