Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

how can i reflect rays for ray tracing?

Asked by 4 years ago

i am making a ray tracing game and i dont know how i can create a ray to reflect.

Here is my code

local Cache = script.Parent:WaitForChild("Cache")

local Player = game.Players.LocalPlayer
local Character = Player.Character

local PixelSize = 20

local ViewPortSize = Cache.AbsoluteSize
local VSX = ViewPortSize.X
local VSY = ViewPortSize.Y

function CastRay(Origin, Direction, Ray_Length)
    local R = Ray.new(Origin, Direction * Ray_Length)
    local Hit, Position, Normal = workspace:FindPartOnRay()

    return R, Hit, Position, Normal
end

function Render()
    for X = 1, VSX do
        for Y = 1, VSY do
            local R = Vector2.new(workspace.CurrentCamera:ScreenPointToRay(UDim2.new(0, X, 0, Y)))

            local R, Hit, Position, Normal = CastRay(R.Origin, R.Direction, 500)

            print(Hit)

            if Hit then
                local RR, HitR, PositionR, NormalR = CastRay(R.Origin, R.Direction, 500)
            end

            wait()
        end

        wait()
    end
end

wait(20)

Render()

Thanks

0
There's a whole community tutorial written by Crazyman on how to make ray reflection https://devforum.roblox.com/t/how-to-reflect-rays-on-hit/18143 User#834 0 — 4y
0
no i want the calculation not the code i cant find out it from code Ariya1234gamer 15 — 4y
0
@Ariyal1234gamer wait so is this your code or are you trying to extract some sort of equation from it. Could you please be more spesific? Benbebop 1049 — 4y
0
ok i am making a code for drawing the camera view on UI but i am working on Relfecting now and i dont know how to calculate it if u see i created a Ray Called RR(RayReflection) Ariya1234gamer 15 — 4y
View all comments (2 more)
0
@Ariyal1234gamer It depends on what types of reflection you want to achieve. If you are working with normal and or specular maps thats gonna be much different then without. You could maybe even try screen-space reflections to be less hardware intensive, if that's a goal. Benbebop 1049 — 4y
0
no a simple reflection math i want not any specular and normal Ariya1234gamer 15 — 4y

2 answers

Log in to vote
0
Answered by
Benbebop 1049 Moderation Voter
4 years ago
Edited 4 years ago

Reflecting a ray is fairly simple.

To get the reflection angle (Denoted as R) we'll need a surface normal (Denoted as N), which you already have set up it seems, and the incident direction (Denoted as I). Incident direction is basically the angle at which the ray hits the surface, once we have it we can basically just do R = I. We can use the direction of the ray (Denoted as D) along with cosign to get it, like so:

I = cos(D) * N

This is of coarse only 2D, so we need to do it twice for each axis.

Ix = cos(Dx) * N

Iy = cos(Dy) * N

Since D = I then you basically have your ray's direction.

Now, time to convert to lua.

local R, Hit, Position, Normal = CastRay(nil, nil, nil)

local RR.Direction = cos(R.Direction) * Normal

Or something along the lines of that, modify it for your own use

Source

P.S Very exited to see whatever your working on if it means cool shaders like this, good luck. Also sorry about the response time.

0
thx for ur help and yes it is a path tracing like blender cycles if u ever seen cycles on blender Ariya1234gamer 15 — 4y
0
but why u putted A vector3 on a Cos when i used it. output get me a error called number expected, got vector3 Ariya1234gamer 15 — 4y
0
i need to put Direction.X / Y / Z on Cos function?? Ariya1234gamer 15 — 4y
0
@Ariyal1234gamer then you will have to calculate each axis individualy. So do 3 calculations and rebuild a vector with the numbers. Ex. Vector3.new(cos(R.Direction.X) * Normal, cos(R.Direction.Y) * Normal, cos(R.Direction.Z) * Normal) Benbebop 1049 — 4y
View all comments (10 more)
0
ok tysm Ariya1234gamer 15 — 4y
0
but why the reflections or incorrect ?? when it reflect it go to sky Ariya1234gamer 15 — 4y
0
Now sure why it would do that ether, could you link a picture of what it looks like? Benbebop 1049 — 4y
0
ok sure Ariya1234gamer 15 — 4y
0
https://ibb.co/VVJQRwb this is the image the magenta lines is a line created between first psotion and reflection position Ariya1234gamer 15 — 4y
0
@Ariyal1234gamer Im not sure what is going on, is there some way you could isolate one ray, also maybe visualise the incoming rays in a different color? Benbebop 1049 — 4y
0
wdum? Ariya1234gamer 15 — 4y
0
@Benbebop Wdum? Ariya1234gamer 15 — 4y
0
@Aryial1234gamer do you maybe want to take this to the dm’s? There is just too much response time in the comments. Benbebop 1049 — 4y
0
can u send me a fr request on roblox so u can help me better? Ariya1234gamer 15 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

https://ibb.co/VVJQRwb this is the image the magenta lines is a line created between first psotion and reflection position

and this is the new Code

local Cache = script.Parent:WaitForChild("Cache")

local Player = game.Players.LocalPlayer
local Character = Player.Character

local PixelSize = 20

local ViewPortSize = Cache.AbsoluteSize
local VSX = ViewPortSize.X
local VSY = ViewPortSize.Y

local Camera = workspace.CurrentCamera

function CastRay(Origin, Direction, Ray_Length)
    local R = Ray.new(Origin, Direction * Ray_Length)
    local Hit, Position, Normal = workspace:FindPartOnRay(R)

    return R, Hit, Position, Normal
end

function Cos(NumberX)
    return math.cos(NumberX)
end

function Render()
    for X = 0, VSX / PixelSize do
        for Y = 0, VSY / PixelSize do
            local F = Instance.new("Frame", Cache)
            F.Size = UDim2.new(0, PixelSize, 0, PixelSize)
            F.Position = UDim2.new(0, X * PixelSize, 0, Y * PixelSize)
            F.BorderSizePixel = 0

            local R = Camera:ScreenPointToRay(X * PixelSize, Y * PixelSize)

            local R, Hit, Position, Normal = CastRay(R.Origin, R.Direction, 500)

            print(Hit)

            if Hit then
                local RR, HitR, PositionR, NormalR = CastRay(Position, Vector3.new(Cos(R.Direction.X), Cos(R.Direction.Y), Cos(R.Direction.Z)) * Normal, 500)

                local Chance = 10

                if Chance == 10 then
                    local Laser = game.ReplicatedStorage.Part:Clone()

                    local M = (Position - PositionR).Magnitude

                    Laser.Parent = workspace

                    Laser.Size = Vector3.new(.1,.1,M)
                    Laser.CFrame = CFrame.new(Position, PositionR) * CFrame.new(0,0,-M / 2)
                end 

                print(HitR, " ReflectNORMAL")
            end

            wait()
        end

        wait()
    end
end

wait(20)

Render()

Answer this question