Jump to content

Draw a line/vector and get all blocks inbetween


tomato3017

Recommended Posts

Hey all,

 

Wondering if I can get some assistance on an issue im trying to figure out. I have a point in the sky or a player position and I want to draw a line to or from a TE and get all the blocks in between(using them to calculate a number/resistance). Any ideas? I know about ray tracing but it stops at the first block it hits iirc. 

 

 

Thanks!

Link to comment
Share on other sites

You just use the same idea as ray tracing but don't stop at the first block it hits.

 

Usually the logic is pretty simple, probably something like this.

  1. Create a field or collection to hold the results. In your case sounds like it could just be a float that gets adjusted as you find blocks.
  2. find the difference vector (basically subtract the two BlockPos) which will get a vector of (x2-x1, y2-y1, z2-z1). 
  3. find the incremental vector you want to step along, which is the difference vector divided by the length of the difference vector.
  4. make a for loop that will loop a number of times of the length of the difference vector, and the loop starts either at your TE or player position and adds the incremental vector each loop and checks the resulting block at that position.
  5. If the block found should adjust your field or collection from Step 1, adjust it.

That's pretty much it. However, there is an important detail to think about. in Step 3 setting the increments like that may miss some little corners of blocks that are technically in the path. So you probably want to make the steps smaller. Like if you want to catch even 1/10th of a block you would divide the incremental vector by 10 and also in Step 4 loop 10 times more. But that cause the opposite problem where you might count the same block multiple times. So there is probably a bit of code you need in Step 4 to remember the last block you found and don't count it twice.

 

The main difference between what you want and the vanilla ray tracing is the fact that you need to prevent double-counting a block. Since the vanilla stops at first block found it can have a fine step without worrying about that.

 

So mostly simple, needs a bit of thought and detail.

 

Note. If you're not familiar with it already, there is a Vec3D class which is useful as it has ability to do things like subtract and divide and add exactly as you need and it works well with BlockPos class.

Edited by jabelar
  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.