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

Specular Reflection function using rays?

Asked by 9 years ago

Hello,

I have been somewhat attempting the implementation of the following mathematical function without any luck.

function reflect(n,v)
    local n1 = n.unit
    local v1 = v.unit
    return 2*(n1:Dot(-1*v1))*n1 + v1 -- From the 'Developer’s Journal' blog tutorial post.
end

Snippet of my code:

-- Tool is defined as well as the remote function.
local ray = Ray.new(tool.Effector.CFrame.p, (mouse.Hit.p - tool.Effector.CFrame.p).unit * 120)
local hit, position = game.Workspace:FindPartOnRay(ray, character)
func:InvokeServer(hit, tool.Effector.CFrame.p,position, charge.Value) -- Basically draws the ray through a remote function. I have FilteringEnabled set to true. 

How would I use this when raycasting and what do the arguments 'n' and 'v' need?

0
"Specular reflection" is an optics thing, probably not what you mean in this case. What exactly are you trying to do here? BlueTaslem 18071 — 9y
0
Here is the wiki page on specular reflection. Hope you can help! http://en.wikipedia.org/wiki/Specular_reflection RobloxUserBrandon 20 — 9y
1
You didn't answer: what are you *trying* to do? Why is specular reflection *relevant*? BlueTaslem 18071 — 9y
0
Ah, sorry dude.. To put it simply, I am just trying to make a ray bounce of an object. Like if you shined a laser pen into a mirror, that sort of thing! The function is there, just don't know how to implement it into my code! :) RobloxUserBrandon 20 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

Well this is the simplest part of the code you need to get this working.

First of all the arguments are:

n - Normal vector(direction of the hit face) v - Direction of the ray(if I'm not mistaking)

Hardest thing to get, is the normal vector. There are scripts made by community, that does this very well. Here is first one I could find: http://pastebin.com/sXZ9FTNz

Note that this is not the fastest algorithm, but it will work good for your case.

To use it with ray casting, you would need to cast at least 2 rays(unless you want to have it reflect against several surfaces). Basically you find the part on ray, use the normal function, then the reflect function and finally cast a new ray , in direction that got returned by reflect function.

Ad

Answer this question