Ray Collision Detector

Ray Collision Detector

1 Downloads

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.2

Supported minecraft version: 1.17 (21w08b)

Github repository: K-bai/Minecraft-Ray-Collision-Detector: Super precise raycast system datapack in vanilla minecraft (github.com)

How to use

Set scoreboard mrcd_x0mrcd_y0mrcd_z0 for any area effect cloud. These three scoreboards stand for how many milliblocks that the area effect cloud can fly in three dimensions respectively each time you call function mrcd:ray_tick as the area effect cloud. 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 an area effect cloud that can pass those blocks that a player can pass, you should tag the area effect cloud mrcd_bullet.

If you want an area effect cloud that can touch entities, you should tag the area effect cloud 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 an area effect cloud that can touch a specific entity or group of entities, you should tag it/them with mrcd_target, the area effect cloud 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.

To see a working example, 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!