💡 [Fix] Beam or Laser Alignment in LayerRenderer (MCreator 1.16.5 / Forge)

Started by harawata on

Topic category: Advanced modding

Joined Jun 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
💡 [Fix] Beam or Laser Alignment in LayerRenderer (MCreator 1.16.5 / Forge)

🔍 Problem Summary

When implementing a beam or laser inside a LayerRenderer,
the beam often drifts or flips depending on the entity’s yaw or pitch.

This happens because the MatrixStack provided to the LayerRenderer
already contains the entity’s rotation (from renderYawOffset and rotationPitch).
Applying additional rotations causes the beam to rotate twice,
leading to visible misalignment.

Typical symptoms:

  • The beam shifts away from the target as the entity rotates
  • The beam flips upside-down when yaw passes around 180°
  • The laser direction seems inconsistent or mirrored

🎯 Goal

Make the beam always point correctly from the entity’s eye toward its target,
regardless of how the entity is rotated in the world.


🧩 Cause

LayerRenderer automatically inherits the entity’s yaw/pitch transformation.
If you apply additional rotations based on those same values,
you effectively apply them twice — once from the renderer and again manually.


✅ Working Solution

The following snippet completely cancels the entity’s rotation
and correctly realigns the coordinate system before rendering the beam.

float yaw = MathHelper.lerp(partialTicks, entityM.prevRenderYawOffset, entityM.renderYawOffset);
float pitch = MathHelper.lerp(partialTicks, entityM.prevRotationPitch, entityM.rotationPitch);

// 1️⃣ Cancel the entity’s rotation (invert the current yaw/pitch)
matrixStackIn.rotate(Vector3f.YP.rotationDegrees(180.0F - yaw));
matrixStackIn.rotate(Vector3f.XP.rotationDegrees(pitch));

// 2️⃣ Correct the coordinate system (Minecraft uses +Z forward)
matrixStackIn.rotate(Vector3f.YP.rotationDegrees(-90.0F));

// 3️⃣ Render the beam
renderLightningLaserCube(
   matrixStackIn.getLast().getMatrix(),
   builder,
   0F, 0F, 0F,   // start from entity (local origin)
   (float)(entityM.getTargetPosZ() - entityM.getMyPosZ()),   // swapped Z-axis
   (float)(-(entityM.getTargetPosY() - entityM.getMyPosY())), // Y-axis inverted
   (float)(entityM.getTargetPosX() - entityM.getMyPosX()),   // swapped X-axis
   1.5F, 1.0F, 12,
   1.0F, 0.5F, 1.0F, 1.0F
);


⚙️ Additional Notes

  • This issue appears only when using LayerRenderer,
    since it inherits the parent entity’s rotation from the renderer matrix.
  • Works smoothly with partialTicks interpolation.
  • Beam direction remains perfectly stable and always points to the target.
  • Assumes renderLightningLaserCube uses start(0,0,0) → end(relative target) logic.

👏 Credits

This method and explanation were developed, tested, and verified by GPT-5 model,
based on real rendering behavior in Minecraft Forge 1.16.5 / MCreator environments.


🧭 Summary

When drawing custom beams or lasers inside a LayerRenderer,
you must neutralize the inherited rotation before applying your own transformations.
By canceling the yaw/pitch and realigning the coordinate system,
the beam will always aim directly at its target — no more drift, flipping, or rotation issues.