RayCasting but for Guis

Hello once again!

I was scripting a 2D RayCasting module and would like to show what I have made so far! It is quite accurate in detecting which point the ray hit, just a few edge cases that I’ll be fixing soon.

Heres what I have made!

The red line is the ray. Which hits the obstacles that come in its path and create a grey square to indicate where it hit. It is quite easy to do so. I found this wikipedia page that details the math behind two intersecting lines and finding the point it intersects at. And using that, I find out the shortest magnitude between the point of intersection and the direction point of the ray and therefore determine which point is the closest to the origin!

9 Likes

I actually wonder how fast this is compared to placing parts of same size in a WorldModel and treating them as 2D.

It depends. All it does is, takes two points - origin and direction. And just map out each edge and check if it intersects, then finds out the closest intersection to the origin. I might find out about an algorithm that can make this faster. If theres 1 gui frame. It checks 4 times for intersections (4 edges). If there were ‘x’ amount of guis. It’ll check 4x times for intersections. I might use some form of algorithm to cut short the memory that can possibly be wasted.

Recently figured out how to rotate lines on a pivot, now works for any origin → direction. According to both points, I find the theta using

math.deg(math.acos(adjacent/hypotenuse))

And using some quick conditions to find out the rotation, I find the quadrant in which the ray lies and adjust the theta accordingly.

Then I rotate the gui and set its Rotation to theta and find the mid point between the origin and direction, then simply position the gui to that mid point (reason being, rotations are only available on the center of the gui).

I released this as a module!

That’s really cool to be honest. I like it a lot, would be cool for people who design games entirely with UI and not Workspace much.

1 Like

Im having a problem,whenever i raycast it the results are strange, its supposed to ray into all of the corners and corners of the box but its all moved to left