math - "mirror"上的二维线反射

标签 math lua love2d

所以我已经断断续续地工作了一个星期,谷歌搜索等等,我还没有找到如何做到这一点。

我有一张“光线”表和一张“线”表,我希望这些线充当镜子,并在光线碰到一条线时反射光线。想象一下激光从镜子上反弹,那种反射。我已经让相交检测工作了,但我不知道如何正确计算反射角并将光线延伸到那个方向。

代码:

--the table rays is a table of tables, and each table inside is formatted as such:
--rays[x] = {100,200,150,600,200,400}, where (100,200) are ordered pairs, etc.
--The table lines simply contains values for x1,y1,x2,y2

for i,ray in ipairs(rays) do
        for j,line in ipairs(lines) do
            if line.x2 ~= nil and #ray>3 then 
                print(line.x2..' '..line.y2)
                iX, iY = intersect.test(ray[#ray-3],ray[#ray-2],
                         ray[#ray-1],ray[#ray],line.x1,line.y1,line.x2,line.y2)

--The above code takes each ray and 
--sees if it intersects with a line, with the intersect.test function
--Then if it does, where iX and iY aren't nil, it continues

                if iX ~= nil and iY ~= nil then
                    local rayA = (180/math.pi)*math.atan(getSlope(ray[#ray-3],ray[#ray-2],ray[#ray-1],ray[#ray]))
                    local lineA = (180/math.pi)*math.atan(getSlope(line.x1,line.y1,line.x2,line.y2))
                    local normalA = (180/math.pi)*math.atan(-1/getSlope(line.x1,line.y1,line.x2,line.y2))

--Here I'm calculating the angle in degrees. For the final code all those atans will
--be out of there for optimization, but its easiest now to see the actual angle 

                    print(rayA..' '..lineA..' '..normalA)

                    ray[#ray-1]=iX
                    ray[#ray]=iY

--This little part just create a point on the ray right at the intersection
--The code after this is my attempt, which doesn't work

                    local reflectA = normalA-rayA
                    local reflectR = 2*reflectA+rayA

                    print(reflectR)

                    reflectR = reflectR/(180/math.pi)
                    local rSlope = math.tan(reflectR)

                    local offset = 0


                    ray[#ray+1]=iX+offset
                    ray[#ray+1]=iY+(offset*rSlope)
                end
            end
        end
    end

我被困在最后一部分。它有点创建一个从线反弹的线段,但有时它会穿过线,而且它永远不是正确的反射角度。任何关于我应该如何做到这一点的指示将不胜感激。

最佳答案

如果可以避免,最好避免使用斜率和角度,因为您将不得不处理烦人的特殊情况,例如斜率是 +ve 或 -ve 无穷大等等。

如果可以计算线的法线(蓝色箭头),则可以使用点积进行反射:

enter image description here

计算线的法线是这样完成的:

local normalY = line.x2 - line.x1
local normalX = line.y1 - line.y2
local normalLength = math.sqrt(normalX * normalX + normalY * normalY)
normalX = normalX / normalLength
normalY = normalY / normalLength

然后你需要计算从线和射线的交点到射线尖端(已经“穿过”你想要反射的线的点)的向量:
local rayX = rayTipX - iX
local rayY = rayTipY - iY

然后计算点积:
local dotProduct = (rayX * normalX) + (rayY * normalY)

这告诉我们光线在法线方向上经过交点的距离(绿线的长度)。要找到绿线的向量,将线法线乘以点积:
 local dotNormalX = dotProduct * normalX
 local dotNormalY = dotProduct * normalY

如果我们否定这个向量,然后将它加倍(得到绿线和粉线),并将其添加到光线的尖端,我们将得到光线的反射尖端:
 local reflectedRayTipX = rayTipX - (dotNormalX * 2)
 local reflectedRayTipY = rayTipY - (dotNormalY * 2)

关于math - "mirror"上的二维线反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30970103/

相关文章:

由于未 merge 文件而导致 Github merge 失败

java - 如何正确地将具有给定小数位数的浮点值的字符串解析为整数?

math - 如何测试随机性(例如 - 洗牌)

c - 如何在LuaJIT中打印固定的字符数组?

lua - 在Lua 3.2(.1)中访问全局表?

function - Lua 根据函数输入命名表

menu - 更改 love.load 函数中的内容

jquery - 在 div 中以隐藏的溢出居中特定的 li

java - 了解 strictMath java 库

lua - 纯Lua哈希方法