Minecraft Data Packs / Tool

Minecraft Ray Collision Detector

  • check_circle Functions
  • 1,171 views, 1 today
  • 0 downloads, 0 today
  • 5
  • 1
  • 4
YouKnowWhen avatar YouKnowWhen
Level 32 : Artisan Crafter
21

Intro


Minecraft Ray Collision Detector is a super precise raycast system in vanilla minecraft! This datapack solved the raycast problem perfectly with minimal command cost. It defines the hitbox of most blocks and does some calculations to judge which surface will be touched.

Current datapack version: 2.5

Supported minecraft version: 1.16, 1.17, 1.18

How to use


Set scoreboard mrcd_x0, mrcd_y0, mrcd_z0 for any ray (entity used as the ray marker, usually area_effect_cloud or markers). These three scoreboards stand for how many milliblocks that the ray can fly in three dimensions respectively each time you call function mrcd:ray_tick as the ray. If it touches a block, it will have the tags mrcd_touch_edge and mrcd_touch_DIRECTION. You can recognize which surface it touched from those tags.

If you want a ray that can pass those blocks that a player can pass, you should tag the ray mrcd_bullet.

If you want a ray that can touch entities (stops after one found), you should tag the ray mrcd_entity and rotate the AEC as the speed direction. If it touched an entity, it will have a tag named mrcd_touch_entity, and the target entity will be tagged mrcd_target_entity. Note that some entities like player and projectiles are ignored by default. You can remove them in entity types tag (#mrcd:ignore) to change it.

If you want a ray that can touch a specific entity or group of entities (stops after one found), you should tag it/them with mrcd_target, the ray mrcd_entity_targeted and rotate the AEC as the speed direction. If it touched a tagged entity, it will have a tag named mrcd_touch_entity, and the target entity will be tagged mrcd_target_entity. Any other non tagged entity that hits, will be ignored and the ray will pass through. This method can target any entity, even those that are in the #mrcd:ignore tag list.

If you want a ray that can touch multiple entities (doesn't stops after one found), you should tag the ray mrcd_entity and mrcd_entity_bullet and rotate the AEC as the speed direction (this is also works with mrcd_entity_targeted). If it touched an entity, it will have a tag named mrcd_touch_entity, and the target entity will be tagged mrcd_target_entity.

Logically tags can be combined following this strucuture: <block_handling>,<entity_handeling>,<entity_extra>
  • block_handling: none or mrcd_bullet
  • entity_handeling: none or mrcd_entity or mrcd_entity_targeted
  • entity_extra: none or mrcd_entity_bullet

To see some basic working examples, check the folder example. You simply need to run each tick the function mrcd:example/tick and give yourself the needed items with the function mrcd:example/give.

How it works


Firstly, we get the direction vector of the marker, saved as #total_x,y,z. Besides, get the position of the marker in a block, saved as #block_x,y,z. These two vector can make a line, represant the moving route of the marker.

Secondly, we defines the hitboxes of all blocks that are not full (See mrcd:private/types/slab as an simple example). All the hitboxes in Minecraft are cuboid, so we can use 2 vectors(#box_x,y,z0 and #box_x,y,z1) to define them.

Thirdly, find which block the marker is in, and determine which plane of the block it will collide (See mrcd:private/cube/main). Calculate the intersection point of the plane and the moving line of the marker, and determine if it can collide by boundary condition (See mrcd:private/cube/x0,x1,y0,y1,z0,z1 and mrcd:private/calculate/x,y,z). Save the collision point as #target_x,y,z

Finally, move the marker to #targetx,y,z!
CreditK_bai, YouKnowWhen
CompatibilityMinecraft 1.14
toMinecraft 1.18
Tags

Create an account or sign in to comment.

1
04/27/2021 5:22 am
Level 11 : Journeyman Miner
Amadasa
Amadasa avatar
I'm not sure of my English but I will try.
Your datapack is exactly what I was looking for.
I'd like to incorporate some of the content of this datapack into the datapack I'm going to distribute, but would you mind it?
1
04/28/2021 1:26 am
Level 32 : Artisan Crafter
YouKnowWhen
YouKnowWhen avatar
I don't mind, I only ask that you give credit to K_bai and me for this datapack and add the link source.
1
05/01/2021 11:38 pm
Level 11 : Journeyman Miner
Amadasa
Amadasa avatar
Thank you!
1
02/23/2021 8:36 am
Level 40 : Master Engineer
JackieT
JackieT avatar
seems like a lot of manual work. maybe make an auto-scoreboard assignment system w/ configurable mrcd_bullet?

because i think that it would be a lot easier for a player to just throw a lingering potion and be done with it, instead of quickly whipping out /scoreboard to make it raycast.
Planet Minecraft

Website

© 2010 - 2023
www.planetminecraft.com

Welcome