我有新高效的光线投射代码:
vectorX, vectorY = math.cos(r * math.pi/180), math.sin(r * math.pi/180)
wallDistanceX, wallDistanceY = v[3] - v[1], v[4] - v[2]
wallParallelX, wallParallelY = -wallDistanceY, wallDistanceX
rayX, rayY = v[1] - cam.x, v[2] - cam.y
Distance = (
(rayX * wallParallelX) + (rayY * wallParallelY))/(
(vectorX * wallParallelX) + (vectorY * wallParallelY)
)
if (
(math.min(v[1],v[3]) <= (cam.x + (Distance * vectorX)) and math.max(v[1],v[3]) >= (cam.x + (Distance * vectorX))) and
(math.min(v[2],v[4]) <= (cam.y + (Distance * vectorY)) and math.max(v[2],v[4]) >= (cam.y + (Distance * vectorY)))
) then
table.insert(possibles, Distance)
else
table.insert(possibles, 0)
end
这段代码从this帖子的答案转换为Lua,但是当绘制光线时,它们看起来像是这样:
我已经完成了我的调试,结果发现当测试光线的一条线的交点时,返回的距离为0(返回0的距离意味着光线据说不相交有墙)。你看到有一面墙,左上角的小地图就是这样。
有关射线在相交时未检测到的原因的任何想法?如果你不明白的话请问。
r = angle for ray
v = an index of the list {}; v[i] is either the x1,y1,x2,y2 of the line