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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
  • Topics

×
×
  • Create New...

Important Information

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