RayIntersect | Calculate a ray's intersection length inside a part, union or mesh

About

Ray Intersect is an open-source module I created to calculate the intersection length of a ray travelling through a BasePart. I could not find any resources that provided similar functionality, so here it is.

Unlike any solutions I could find, this module is capable of calculating the intersection length even when the part in question has hollow or concave elements. Empty space is not included in the final intersection length.

Any modifications and redistributions of the module are welcome, however proper credit would be appreciated when using the module commercially or redistributing the module or modifications thereof.

How it works

The module has two different ways of calculating intersection length: simple and complex.

The simple mode, as the name would suggest, does not take into account more complex part shapes, so for example a ray travelling through a hollow sphere ends up with the same intersection length as a ray travelling through a solid one. Below is an animation showing how the simple intersection calculation works:

The complex intersection mode is significantly more performance-intensive (and the computing time grows with each ray entry/exit), but it can more accurately calculate the intersection length when the part geometry also includes empty space. Below is an animation demonstrating the complex intersection calculation:

Personally I do not recommend using the complex intersection function unless you absolutely have to due to its slower speed. However, this is ultimately dependent on your particular use case.

Using the module

To use the module in your game, simply insert it to the location of your choice and require it from any scripts that need to perform these calculations.

The module comes with a User Manual parented to the ModuleScript. Please refer to it for documentation on the available functions.

Where to get it

The module is available on Roblox.


Any suggestions for improvements are more than welcome.
5 Likes