Jump to content

[1.16.5] Drawing lines from the center of screen with View Bobbing enabled


Recommended Posts

Posted

It is simple task when View Bobbing is disabled. In RenderWorldLastEvent I do this:

Vector3d view = Minecraft.getInstance().gameRenderer.getMainCamera().getPosition();

MatrixStack stack = event.getMatrixStack();
stack.translate(-view.x, -view.y, -view.z);

GL11.glPushMatrix();
RenderSystem.multMatrix(stack.last().pose());

double centerX = view.x;
double centerY = view.y;
double centerZ = view.z;

double drawBeforeCameraDist = 1;
double pitch = (mc.player.xRot) * Math.PI / 180;
double yaw = (mc.player.yRot) * Math.PI / 180;

centerX -= Math.sin(yaw) * Math.cos(pitch) * drawBeforeCameraDist;
centerZ += Math.cos(yaw) * Math.cos(pitch) * drawBeforeCameraDist;
centerY -= Math.sin(pitch) * drawBeforeCameraDist;

But when I enabled View Bobbing, the center point starts moving in half-circle trajectory. I looked into GameRenderer class, bobView method. It calculates few float values and applies them to MatrixStack. I don't know if I can use this matrix (it comes in RenderWorldLastEvent), so I copied code from bobView and started experimentation. This is the best I can get:

// omitted same lines from my first example

double centerX = view.x;
double centerY = view.y;
double centerZ = view.z;

double deltaXRot = 0;
double deltaZRot = 0;
double translateX = 0;
double translateY = 0;

if (mc.options.bobView && mc.getCameraEntity() instanceof PlayerEntity) {
    PlayerEntity playerentity = (PlayerEntity)mc.getCameraEntity();
    float f = playerentity.walkDist - playerentity.walkDistO;
    float f1 = -(playerentity.walkDist + f * event.getPartialTicks());
    float f2 = MathHelper.lerp(event.getPartialTicks(), playerentity.oBob, playerentity.bob);
    translateX = (double)(MathHelper.sin(f1 * (float)Math.PI) * f2 * 0.5F);
    translateY = (double)(-Math.abs(MathHelper.cos(f1 * (float)Math.PI) * f2));
    deltaZRot = MathHelper.sin(f1 * (float)Math.PI) * f2 * 3.0F;
    deltaXRot = Math.abs(MathHelper.cos(f1 * (float)Math.PI - 0.2F) * f2) * 5.0F;
}

double drawBeforeCameraDist = 1;
double yaw = (mc.player.yRot) * Math.PI / 180;
double pitch = (mc.player.xRot + deltaXRot) * Math.PI / 180;

centerY -= translateY;
centerX += translateX * Math.cos(yaw);
centerZ += translateX * Math.sin(yaw);

centerX -= Math.sin(yaw) * Math.cos(pitch) * drawBeforeCameraDist;
centerZ += Math.cos(yaw) * Math.cos(pitch) * drawBeforeCameraDist;
centerY -= Math.sin(pitch) * drawBeforeCameraDist;

Now center point doesn't jump to the right and left on the screen. And it doesn't jump up and down, but only if I look straight forward. As pitch angle goes far from 0 (either to -90 or 90), center point jumps harder up and down. Is my approach right? Or should I learn about MatrixStack and try to use it?

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



×
×
  • Create New...

Important Information

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