Jump to content

Retrieving distance from viewport to player head


ricepuffz

Recommended Posts

Hi, this is my first time posting on here so sorry for everything that I'm gonna do wrong in advance

 

I am currently trying to render custom models using OpenGL. Now I have a mesh which follows the player, but as soon as I go into third person the position of the mesh isn't correct anymore because OpenGL translates from the viewport and not the actual player "eyes" position where the viewport is located in first person (as far as I know).

Now my question is, how do I compensate for the offset? Is there a way to get the actual viewport position in relation to the player "eyes" or is there a more elegant solution which I just don't know about. Or maybe 2am is too late for my brain to work properly.

 

Sorry if I ask stupid questions but I just got back into Forge yesterday which is why I'm not quite familiar with all the new stuff.

All help is appreciated, thanks in advance :"

 

@SubscribeEvent
public void RenderWorldLastEvent(RenderWorldLastEvent event)
{
    float partialTicks = event.getPartialTicks();

    ClientPlayerEntity playerEntity = Minecraft.getInstance().player;
    double x = playerEntity.lastTickPosX + (playerEntity.posX - playerEntity.lastTickPosX) * partialTicks;
    double y = playerEntity.lastTickPosY + (playerEntity.posY - playerEntity.lastTickPosY) * partialTicks;
    double z = playerEntity.lastTickPosZ + (playerEntity.posZ - playerEntity.lastTickPosZ) * partialTicks;

    GlStateManager.pushMatrix();

    GlStateManager.translated(-0.5 d, -playerEntity.getEyeHeight(), -0.5 d);

    GlStateManager.disableCull();
    GlStateManager.disableDepthTest();
    GlStateManager.disableTexture();

    GlStateManager.color4f(1, 1, 1, 1);

    Tessellator tessellator = Tessellator.getInstance();
    BufferBuilder wr = tessellator.getBuffer();

    wr.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION);

    wr.pos(0, 0, 0).endVertex();
    wr.pos(1, 0, 0).endVertex();
    wr.pos(1, 0, 1).endVertex();
    wr.pos(0, 0, 1).endVertex();

    wr.pos(0, 1, 0).endVertex();
    wr.pos(1, 1, 0).endVertex();
    wr.pos(1, 1, 1).endVertex();
    wr.pos(0, 1, 1).endVertex();

    tessellator.draw();

    GlStateManager.enableCull();
    GlStateManager.enableDepthTest();
    GlStateManager.enableTexture();

    GlStateManager.popMatrix();
}

 

Link to comment
Share on other sites

I just managed to fix the issue. For anyone who find this post in the future, I used the following code to do it:

Vec3d projectedView = Minecraft.getInstance().gameRenderer.getActiveRenderInfo().getProjectedView();

ClientPlayerEntity playerEntity = Minecraft.getInstance().player;
double x = playerEntity.lastTickPosX + (playerEntity.posX - playerEntity.lastTickPosX) * partialTicks;
double y = playerEntity.lastTickPosY + (playerEntity.posY - playerEntity.lastTickPosY) * partialTicks;
double z = playerEntity.lastTickPosZ + (playerEntity.posZ - playerEntity.lastTickPosZ) * partialTicks;

GlStateManager.translated(x - projectedView.x - 0.5d, y - projectedView.y, z - projectedView.z - 0.5d);

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Ensure your system is running the latest version of Java. Sodium requires Java 17 or later for newer Minecraft versions (like 1.17+). 
    • Hi I wanted to my custom mob to hold any sword item, but didn’t rendered properly.   In entity class, make entity hold items as below: @Override public InteractionResult mobInteract(Player pPlayer, InteractionHand pHand) { //… ItemStack itemstack = pPlayer.getItemInHand(pHand); if (this.isTame()) { if ( (itemstack.is(Items.MELON_SLICE) || itemstack.is(Items.HONEY_BOTTLE)) && this.getHealth() < this.getMaxHealth() ) { //… } /* Handle holding sword */ else if (itemstack.getItem() instanceof SwordItem) { pPlayer.displayClientMessage(Component.literal("Clicked with item: " + itemstack.getDisplayName().getString()).withStyle(ChatFormatting.GOLD), true); //Return sword //pPlayer.getInventory().add(this.getItemInHand(InteractionHand.MAIN_HAND)); pPlayer.getInventory().add(this.getItemBySlot(EquipmentSlot.MAINHAND)); //The entity holds item //this.setItemInHand(InteractionHand.MAIN_HAND, itemstack); this.setItemSlot(EquipmentSlot.MAINHAND, itemstack.copy()); //Give copy of itemstack //If player is not in creative mode. From mobInteract() in wolf. if (!pPlayer.getAbilities().instabuild) { //Decrement sword count in hand pPlayer.getItemInHand(pHand).shrink(1); } return InteractionResult.SUCCESS; } else { //If player is sneaking pPlayer.displayClientMessage(getItemInHand(InteractionHand.MAIN_HAND).getDisplayName(), false); if (pPlayer.isShiftKeyDown()) { //Return sword //pPlayer.getInventory().add(this.getItemInHand(InteractionHand.MAIN_HAND)); pPlayer.getInventory().add(this.getItemBySlot(EquipmentSlot.MAINHAND)); //The entity holds nothing this.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY); return InteractionResult.SUCCESS; } else { //… } } else { return interactionresult; } }   And in render class, render the item as below: @Override public void render(RanaEntity pEntity, float pEntityYaw, float pPartialTicks, PoseStack pMatrixStack, MultiBufferSource pBuffer, int pPackedLight) { if(pEntity.isBaby()) { pMatrixStack.scale(0.5f, 0.5f, 0.5f); } model.setupAnim(pEntity, 0, 0, 0, pEntityYaw, 0); // //Get location and rotation of arm bone ModelPart rightArm = model.rightArm(); //Get right arm //Get location and rotation of item according to arm bone pMatrixStack.pushPose(); pMatrixStack.translate(rightArm.x, rightArm.y, rightArm.z); //Move to bone location pMatrixStack.mulPose(Axis.XP.rotationDegrees(rightArm.xRot)); //Rotate X pMatrixStack.mulPose(Axis.YP.rotationDegrees(rightArm.yRot)); //Rotate Y pMatrixStack.mulPose(Axis.ZP.rotationDegrees(rightArm.zRot)); //Rotate Z //Draw item //ItemStack itemStack = pEntity.getItemInHand(InteractionHand.MAIN_HAND); ItemStack itemStack = pEntity.getItemBySlot(EquipmentSlot.MAINHAND); if (!itemStack.isEmpty()) { //Offset pMatrixStack.translate(0.0, 0.0, 0.1); // Render the item //Minecraft.getInstance().getItemRenderer().renderStatic(itemStack, ItemDisplayContext.THIRD_PERSON_RIGHT_HAND, pPackedLight, OverlayTexture.NO_OVERLAY, pMatrixStack, pBuffer, pEntity.level(), pEntity.getId()); //itemRenderer.renderStatic(itemStack, ItemDisplayContext.THIRD_PERSON_RIGHT_HAND, pPackedLight, OverlayTexture.NO_OVERLAY, pMatrixStack, pBuffer, pEntity.level(), pEntity.getId()); itemInHandRenderer.renderItem(pEntity, itemStack, ItemDisplayContext.THIRD_PERSON_RIGHT_HAND, false, pMatrixStack, pBuffer, pEntity.getId()); } pMatrixStack.popPose(); super.render(pEntity, pEntityYaw, pPartialTicks, pMatrixStack, pBuffer, pPackedLight); }   I confirmed the entity can properly hold item(logically) but the item which the entity holds is not rendered at all.   Full code: https://github.com/sakiiiiika/ranamod   Thanks.
    • It is Immersive Melodies
    • MINECRAFT JAVA wht is ic_im it shows up yellow heres the modpack https://rocktheslayer.wixsite.com/my-site-7 also It wouldnt Let me submit a bug report only a suggestion for this post
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.