BodyRotationControl .java

net.minecraft.world.entity.ai.control.BodyRotationControl (class, 65 lines): members: DELAY_UNTIL_STARTING_TO_FACE_FORWARD, lastStableYHeadRot, clientTick.

net.minecraft.world.entity.ai.controlclass65 linescommon2.1 KB

Implements

API Surface (1)

void clientTick()

Fields & Constants (6)

HEAD_STABLE_ANGLE
DELAY_UNTIL_STARTING_TO_FACE_FORWARD
HOW_LONG_IT_TAKES_TO_FACE_FORWARD
mob
headStableTime
lastStableYHeadRot

Code Graph: Dependencies (3)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

BodyRotationControl.java65 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.control;
2
3
import net.minecraft.util.Mth;
4
import net.minecraft.world.entity.Mob;
5
6
public class BodyRotationControl implements Control {
7
	private final Mob mob;
8
	private static final int HEAD_STABLE_ANGLE = 15;
9
	private static final int DELAY_UNTIL_STARTING_TO_FACE_FORWARD = 10;
10
	private static final int HOW_LONG_IT_TAKES_TO_FACE_FORWARD = 10;
11
	private int headStableTime;
12
	private float lastStableYHeadRot;
13
14
	public BodyRotationControl(final Mob mob) {
15
		this.mob = mob;
16
	}
17
18
	public void clientTick() {
19
		if (this.isMoving()) {
20
			this.mob.yBodyRot = this.mob.getYRot();
21
			this.rotateHeadIfNecessary();
22
			this.lastStableYHeadRot = this.mob.yHeadRot;
23
			this.headStableTime = 0;
24
		} else {
25
			if (this.notCarryingMobPassengers()) {
26
				if (Math.abs(this.mob.yHeadRot - this.lastStableYHeadRot) > 15.0F) {
27
					this.headStableTime = 0;
28
					this.lastStableYHeadRot = this.mob.yHeadRot;
29
					this.rotateBodyIfNecessary();
30
				} else {
31
					this.headStableTime++;
32
					if (this.headStableTime > 10) {
33
						this.rotateHeadTowardsFront();
34
					}
35
				}
36
			}
37
		}
38
	}
39
40
	private void rotateBodyIfNecessary() {
41
		this.mob.yBodyRot = Mth.rotateIfNecessary(this.mob.yBodyRot, this.mob.yHeadRot, this.mob.getMaxHeadYRot());
42
	}
43
44
	private void rotateHeadIfNecessary() {
45
		this.mob.yHeadRot = Mth.rotateIfNecessary(this.mob.yHeadRot, this.mob.yBodyRot, this.mob.getMaxHeadYRot());
46
	}
47
48
	private void rotateHeadTowardsFront() {
49
		int timeSinceStartingToFaceForward = this.headStableTime - 10;
50
		float faceForwardFraction = Mth.clamp(timeSinceStartingToFaceForward / 10.0F, 0.0F, 1.0F);
51
		float angleRemainingUntilFacingForward = this.mob.getMaxHeadYRot() * (1.0F - faceForwardFraction);
52
		this.mob.yBodyRot = Mth.rotateIfNecessary(this.mob.yBodyRot, this.mob.yHeadRot, angleRemainingUntilFacingForward);
53
	}
54
55
	private boolean notCarryingMobPassengers() {
56
		return !(this.mob.getFirstPassenger() instanceof Mob);
57
	}
58
59
	private boolean isMoving() {
60
		double xd = this.mob.getX() - this.mob.xo;
61
		double zd = this.mob.getZ() - this.mob.zo;
62
		return xd * xd + zd * zd > 2.5000003E-7F;
63
	}
64
}
65

Decompiled Minecraft 26.3 source of BodyRotationControl (net/minecraft/world/entity/ai/control/BodyRotationControl.java) with Mojang official mappings, stored verbatim on the site.