# My brain exploded; trying to think of proper logic for laser deflection

Alright, I’ll try to be as specific as possible in this post, as I don’t have any code to give, as there isn’t a “problem” with anything, it’s more regarding how I should go about implementing the following logic to my system.

Outline
I made a functioning laser cannon that uses raycasting to shoot a solid laser beam. If an object is in front of it, the laser goes only to that object. (the object blocks it)

However, there’s more complexity. I did that first part just fine, but there’s other objects, mirrors, that will deflect the laser in another direction, and this process can keep repeating until the laser isn’t hitting any more mirrors.

I wanted to make mirrors as simple as possible. So what I did was, I placed two parts on both sides of the mirror, where it can deflect the lasers, and named them “Face1” or “Face2”. If a laser hits one of the faces, it’ll then find the opposite face in the model and draw a laser with the origin being that face’s CFrame, and the direction being it’s looking direction, and so the process repeats depending on how many more mirrors there are as new lasers are drawn.

I hope that makes sense.

Anyways, I’ve been struggling with trying to figure out the proper logic for storing the “dependent” laser objects, because the “deflecting” laser parts will be dependent on the older lasers. For example, if an object moves to where there’s only 1 deflecting laser, but before the object moved there were 2 deflecting lasers…this is kind of tricky.

I think there is a tutorial on this make an invisible part and make sure to do a copy script that is the opposite laser director. It is easier that way.

1 Like

That is just in addition to what you said. Instead of focusing on the reflect make opposite position of the laser then it will basically be reflecting. Make sure to clone part and opposite direction of the laser to Make the part snap when laser is shot to automatically do the opposite of it.

I hope this helped! Good luck on your project!

1 Like

Yeah, my brain is a bit confused by your mirror system too.

This image is what I’d use as a base for my reflection algorithm. Assuming this is a 2D area (If it gets 3D, I’m sorry pal, but you’re in for a fun time), you must find the normal of the surface, usually by CFrame.LookVector. Then, get the vector that is the laser hitting the surface. Next, make sure these are both UNIT vectors with the same origin. By using the Dot Product, you can find the length of the base. Now that you know the base and hypotenuse, you can do some basic trigonometry to find the angle the laser is hitting the mirror at. Finally, use that information and do some more trigonometry to create a new laser with the opposite angle!

Sorry for the word soup, but I like math!

1 Like

I appreciate the info! Although, I probably could’ve made my OP more clear, but I’m not actually struggling to generate the angle for the deflected laser, the issue is more how I could efficiently and set up the logic for checking which lasers are no longer deflecting (if the mirror moves) and then if so, delete those lasers.

No angle needs to be calculated for drawing a reflected angle, I use two parts on the mirror to be the direction of the laser if the ray hits one of those parts.

I hope that makes a bit more sense

Yes, that’s why you should use a clone script. We’re as soon as you click the button (that lasers) it will read when hiting a part it will copy the same laser from that position. So it be reflect like a real laser.

I have came up an idea to use a detection ray to calculate the reflection angle.
I hope this helps you to solve it.

I’m using picture to show how it works.

I’m so sorry that I forgot to make a white background.

I spent a lot of time using it XD.

1 Like

Hope this helps.

2 Likes