Jump to content

[1.15.2]RayTracingResult is not working as it should be


ChAoS_UnItY

Recommended Posts

I am making a WAILA-like mod, I'm not familiar with RayTracingResult mechanics, so I decided to just copy some code from HWYLA source code.

 

Here's the problem, it works half fine, but once my crosshair is on specific side of block , it shows that what I'm pointing is an air block that is next to the block it should be. Any idea?

Link to comment
Share on other sites

Well, there's your first problem, don't do blank copy pastes, if you're going to, fork it on github, run it and try and understand its function before you just take from there. 

 

But, what would help is some of the code you have so we can see what the problem is. 

Link to comment
Share on other sites

You may at one point have the option to input a range for the raytrace, increasing that (may) help, also trying seeing what happens when you're right in front of a block, also, trying to filter out air blocks might be a shout too. I'm no expert at this so I'm just broadcasting ideas here. 

 

Link to comment
Share on other sites

public void onRender(RenderGameOverlayEvent event) {
		LambdaHelper lambdaHelper = (x) -> {
			return 1 + x * 9;
		};
		PlayerEntity player = mc.player;
		UtilMinecraftDate date = new UtilMinecraftDate(mc.world);
		if (event.getType() == ElementType.TEXT) {
			GL11.glPushMatrix();
			//Some codes not relative to my topic
			// ============================BENEATH PART============================//
			int h = event.getWindow().getHeight();
			lambdaHelper = (x) -> {
				return h - 1 - x * 9;
			};
			World world = mc.world;
			RayTracingHelper.INSTANCE.fire();   //<----From HWYLA
			RayTraceResult mop = RayTracingHelper.INSTANCE.getTarget();  //<----From HWYLA
			if (mop.getType() == Type.BLOCK) {
				// ============================TILEENTITY INFO============================//
				BlockPos pos2 = new BlockPos(mop.getHitVec());
				System.out.println(pos2);
				System.out.println(player.getForward().getX());
				BlockState state = world.getBlockState(pos2);
				Block block = state.getBlock();
				System.out.println(block);
				if (block.hasTileEntity(state)) {
					TileEntity te = world.getTileEntity(pos2);
					// mc.getItemRenderer().renderItemIntoGUI(stack, x, y);
					if (block == Blocks.BEE_NEST || block == Blocks.BEEHIVE) {
						simpleStringDraw("bee" + ((BeehiveTileEntity) te).getHoneyLevel(state), 2, 90, 16777215);
						mc.getItemRenderer().renderItemIntoGUI(Blocks.BEE_NEST.asItem().getDefaultInstance(), 1, 90);
					}
				}
			}
			GL11.glPopMatrix();
		}
	}

Main code where RayTracingResult get datas
 

public class RayTracingHelper {
	public static final RayTracingHelper INSTANCE = new RayTracingHelper();
    private RayTraceResult target = null;
    private Minecraft mc = Minecraft.getInstance();

    private RayTracingHelper() {
    }

    public void fire() {
        if (mc.objectMouseOver != null) {
            this.target = mc.objectMouseOver;
            return;
        }

        Entity viewpoint = mc.player;
        if (viewpoint == null)
            return;

        this.target = this.rayTrace(viewpoint, mc.playerController.getBlockReachDistance(), 1);
    }

    public RayTraceResult getTarget() {
        return this.target;
    }

    public RayTraceResult rayTrace(Entity entity, double playerReach, float partialTicks) {
        Vec3d eyePosition = entity.getEyePosition(partialTicks);
        Vec3d lookVector = entity.getLook(partialTicks);
        Vec3d traceEnd = eyePosition.add(lookVector.x * playerReach, lookVector.y * playerReach, lookVector.z * playerReach);
        RayTraceContext context = new RayTraceContext(traceEnd, eyePosition, BlockMode.OUTLINE, FluidMode.NONE, entity);
        return mc.world.rayTraceBlocks(context);
    }
}

Codes from HWYLA (just some important methods copied)

Any idea what is wrong in these codes?

EDIT: Also, all blocks' west, north, and up side is not doing well with ray tracing, they all show air block.

Edited by ChAoS_UnItY
more details
Link to comment
Share on other sites

23 minutes ago, ChAoS_UnItY said:

if (mop.getType() == Type.BLOCK) {
	BlockPos pos2 = new BlockPos(mop.getHitVec());

 

Why are you using #getHitVec? Check if mop instanceof BlockRayTraceResult, and mop.getType() == Type.Block. Then, cast mop to BlockRayTraceResult, and call #getPos.

if (mop instanceof BlockRayTraceResult && mop.getType() == Type.BLOCK) {
	BlockRayTraceResult bmop = (BlockRayTraceResult) bmop;
	BlockPos pos = bmop.getPos();

I'm fairly certain that if mop.getType() == Type.BLOCK, then mop is definitely BlockRayTraceResult, but better check instanceof to be sure that mop is not null.

 

I'm not sure, but I believe #getHitVec returns the vector in which the ray from the player's mouseover intersects the block's collision box. However, due to the fact that we have a [-0, -0] XYZ coordinate which does not have a corresponding block coordinate, ([-0, -0] in XYZ = [-1, -1] in blocks), converting the hit vector to BlockPos will result in a loss of 1 unit coordinate in the north and west directions.

Edited by sciwhiz12
inc. though
  • Thanks 1
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



×
×
  • Create New...

Important Information

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