Skip to content

HeadRotation

Look direction for entities. Separate from body rotation in TransformComponent.

HeadRotation head = store.getComponent(ref, HeadRotation.getComponentType());
// get rotation vector (pitch, yaw, roll in radians)
Vector3f rotation = head.getRotation();
float pitch = rotation.getPitch(); // vertical angle (look up/down)
float yaw = rotation.getYaw(); // horizontal angle (look left/right)
float roll = rotation.getRoll(); // tilt angle
// set rotation
head.getRotation().assign(pitch, yaw, roll);
head.setRotation(rotationVector);
// teleport rotation (skips NaN values)
head.teleportRotation(new Vector3f(pitch, yaw, roll));
// get normalized look direction (3D vector)
Vector3d direction = head.getDirection();
// get axis-snapped direction (-1, 0, or 1 per axis)
Vector3i axisDir = head.getAxisDirection();
// get horizontal axis direction (ignores pitch)
Vector3i horizontalDir = head.getHorizontalAxisDirection();
// get dominant axis
Axis axis = head.getAxis(); // X, Y, or Z

The Axis enum represents X, Y, or Z:

Axis axis = head.getAxis();
Vector3i axisVector = axis.getDirection(); // (1,0,0), (0,1,0), or (0,0,1)
// rotate a vector around this axis
axis.rotate(vector, angle);
// flip a vector along this axis
axis.flip(vector);
HeadRotation head = store.getComponent(ref, HeadRotation.getComponentType());
TransformComponent transform = store.getComponent(ref, TransformComponent.getComponentType());
Vector3d entityPos = transform.getPosition();
Vector3d targetPos = new Vector3d(x, y, z);
Vector3d direction = targetPos.clone().subtract(entityPos).normalize();
// calculate pitch and yaw from direction
float pitch = (float) Math.asin(-direction.getY());
float yaw = (float) Math.atan2(direction.getX(), direction.getZ());
head.getRotation().assign(pitch, yaw, 0);

Use TargetUtil.getLook() to get the full look transform including eye height:

Transform lookTransform = TargetUtil.getLook(ref, store);
// includes position at eye level + head rotation
HeadRotation head = store.getComponent(ref, HeadRotation.getComponentType());
Vector3i facing = head.getHorizontalAxisDirection();
if (facing.getZ() > 0) {
// facing south (+Z)
} else if (facing.getZ() < 0) {
// facing north (-Z)
} else if (facing.getX() > 0) {
// facing east (+X)
} else {
// facing west (-X)
}

For players, head rotation is updated automatically from client input via PlayerInput.SetHead. The rotation syncs each tick based on where the player is looking.

  • HeadRotation - where the entity is looking (head/eyes)
  • TransformComponent.getRotation() - body orientation

For most gameplay purposes (raycasting, targeting), use HeadRotation. The body rotation is primarily for visual rendering.