How to reflect something from one part to another?

I’m wanting to have a ray basically reflect out from one part to another.

I have the basic of it, but I want to basically get if ray hits top part of part, then it comes out of top half of other part.
ezgif.com-gif-maker

Basically, all I have at the moment is

local Hit = result.Instance

local Origin = ToPortal.Position
local Direction = ToPortal.CFrame.LookVector

With ToPortal being the portal that the new ray comes out of. Result is the raycast result, so can do result.Position to get the position the ray hit. And Hit is the object hit. So I don’t know if there some way to convert the hit parts position, with the position the ray hit, and then transfer that onto the other part.

Also to note, ideally the angle you shoot at should also transfer onto the other part.

Like this:

Instead of it being like this, always going in the flat direction
image

I don’t know if this is possible though?

I’ve also looked at

however, wasn’t really working with my case

local Origin = ToPortal.Position
local Direction = Hit.CFrame.LookVector - (2 * Hit.CFrame.LookVector:Dot(result.Normal) * result.Normal)--ToPortal.CFrame.LookVector

Ray would just exit from the centre on the side of the part,

4 Likes

You’re not really talking about reflection. If you imagine rotating the rightmost part to face in the same direction as the top part in your “Like this:” figure, you can see that the ray exits in the exact opposite direction as where it enters, essentially following the same path but in reverse.

That’s totally a thing that makes sense tho, I’m just trying to make sure I understand what you want.

If that’s what you want, it needs a bit more info to be well defined. Specifically, what happens if the entry and exit portals aren’t the same size? If the ray enters 25% = 1 studs from the left edge of a 4 studs wide portal, which point should it exit from on the exit portal that’s 8 studs wide? 1 studs from the left edge? 25% = 2 studs from the left edge?

Anyway, here’s how you can do it if the entry and exit are the same size:

We want to get the origin and direction of the exit ray. The origin is where the entry ray hit the entry portal, relative to the entry portal (entry portal object-space), transformed to be relative to the exit portal (world space). In other words:

local exitRayOrigin = entryPortal.CFrame:PointToWorldSpace(
    entryPortal.CFrame:PointToObjectSpace(entryRayHitPosition)
)

The direction is the opposite of the entry ray, relative to the entry portal, transformed to be relative to the exit portal.

local exitRayDirection = entryPortal.CFrame:VectorToWorldSpace(
    entryPortal.CFrame:VectorToObjectSpace( -1 * entryRayDirection )
)

Hope this helps! Feel free to ask questions or post follow-ups if you have issues :slight_smile:

I’m unable to try this at the moment due to not being on my PC, but yeah, basically portals would be same size, but it should all be based on % anyway. So if the ray hit the top 10% of the part, and say the left 5% of the part, then it should come out of the top 10%, left 5% of the other part, irrelevant of the other portals sizes, so if portals were different sizes, it could still function correctly

1 Like

Ok I’m on, slightly confused, on possibly just the wording? There’s no reference of the exit portal?? And unsure what entryRayDirection is

You’ll have to create a variable that refers to the exit portal.

You can’t do what’s shown in the video with one ray, you have to have two:

  • one that “goes into” the entry portal which is what I’d refer to as the entry ray, and
  • one that “goes out of” the exit portal, which I’d call the exit ray.

Any ray is described by its origin point and direction. So the entryRayDirection is the direction of the entry ray.

The entry ray is what gets shot out of the gun in the video, while the exit ray is what we’re trying to determine the origin and direction of.