tomato3017 Posted June 23, 2018 Share Posted June 23, 2018 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! Quote Link to comment Share on other sites More sharing options...
jabelar Posted June 23, 2018 Share Posted June 23, 2018 (edited) 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. 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. find the difference vector (basically subtract the two BlockPos) which will get a vector of (x2-x1, y2-y1, z2-z1). find the incremental vector you want to step along, which is the difference vector divided by the length of the difference vector. 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. 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 June 23, 2018 by jabelar 1 Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/ Link to comment Share on other sites More sharing options...
tomato3017 Posted June 24, 2018 Author Share Posted June 24, 2018 Awesome, thank you so much. Makes much more sense now! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.