我在为Ti Nspire CX(使用Lua)构建的光线投射引擎上日复一日地工作,并且遇到了光线碰撞的问题。
我已经摆弄了我认为有问题的区域,因为在屏幕上绘制光线没有问题:
我也在这里进行了大量的调试,例如显示从玩家向外射击时光线所在的坐标。我说我相信碰撞部分存在问题,因为当我打印每条光线的半径时,它们都达到了最大距离,我将其设置为40.这就是碰撞和单射线管理代码:
function ray()
radius = 1
for n = 1, 40, 1 do -- increase of testdot
x_ray = x + (radius * math.cos(rayf * 3.141592653/180))
y_ray = y - (radius * math.sin(rayf * 3.141592653/180))
--print(math.floor(x_ray,3), math.floor(y_ray,3), rayf, radius)
for i = 1, 4, 1 do --for k,v in pairs(map) do -- testing for collision of testdot and a wall
--print("X ",v[1],"<-->",math.floor(x_ray),"<-->",v[3])
--print("Y ",v[2],"<-->",math.floor(y_ray),"<-->",v[4])'
------------------------------------
if (
math.min(map[i][1],map[i][3]) <= x_ray and x_ray <= math.max(map[i][1],map[i][3])
) and (
math.min(map[i][2],map[i][4]) <= y_ray and y_ray <= math.max(map[i][2],map[i][4])
) then
print("Collision")
--return true
end
------------------------------------
end
radius = n
end
end
我知道第二个for循环可以压缩,但我在调试过程中这样做,以找出为什么它不能正常运行。
------------------------------------周围的区域是光线没有的区域碰撞/超出/错过......我不知道为什么这不起作用,有人有什么建议吗?
仅供参考,这次碰撞是基于一个python程序,我遇到了here的问题,当然还有碰撞部分。
变量值:
x,y是玩家的位置(而光线投射将保持静态)
radius是单个光线的当前半径,只要没有检测到碰撞就会继续增加
rayf是光线的当前程度(与玩家无关)。在程序开始时计算玩家的程度(这里没有显示,但被称为'面对'),加30,然后顺时针旋转直到满足60度的FOV。
X射线,y_ray是单个射线的当前点,并且将继续朝着指定的射线散射值增加,并且将以1的值递增以使半径等于最后一个for循环中的n。 (必须注意,在典型的单位圆中,度数是相同的,并且不会镜像以匹配此镜像的y轴;即,向上90度,向下180度。)
答案 0 :(得分:1)
这不是代码审查网站,但我将尝试以更易理解的方式编写代码,然后猜测代码注释中的错误。
function ray(x,y,rayf,map)
%Are you sure that your global variables x,y,rayf are not overwritten?
%I believe these are correct if 0 degrees is "right", "90" - top and "180" - left
%is it so?
local x_proj = math.cos(rayf* 3.141592653/180);
local y_proj = -math.sin(rayf* 3.141592653/180);
for radius=1,40 do
local x_ray = x + radius * x_proj
local y_ray = y + radius * y_proj
for i=1,4 do
%I take it the map[i] is a particular rectangle located at a particular side of the room (so map[1] is located at the left edge of the screen, for example)
%is it so?
local wall_left_edge = math.min ( map[i][1],map[i][3] )
local wall_right_edge = math.max ( map[i][1],map[i][3] )
%if I understood correctly, the smaller y is above bigger y
local wall_top_edge = math.min ( map[i][2], map[i][4] )
local wall_bottom_edge = math.max ( map[i][2], map[i][4] )
%it is beyond me why couldnt you just sort the wall coordinates beforehand
%(say, top - 1 , bottom - 2 left - 3, right - 4)
if (wall_left_edge < x) and (x < wall_right_edge)
and (wall_top_edge < y) and (y < wall_bottom_edge) then
%this does not detect collision,
%it detects whether beam steps into the rectangle "map[i]"
print("Collision")
end
end
end
end
因此,考虑到最后一条评论,您定义的墙必须宽且足够厚,以保证光束步入一个:(wall_right_edge - wall_left_edge ) > 1
(1是半径环的一步)和{ {1}}。在角落处,墙壁必须重叠,或者它们应该共享长度至少为1的边界。
答案 1 :(得分:0)
光线始终超过40的原因是因为for循环没有被取消,这是包含 return 以打破函数并继续代码的一个很好的理由(I我认为包含了一个回报,但是它没有正常运行,如下所示:
--return true
过去这一点,光线投射效果很好,但不是数学。因为一些未知的原因,新射线坐标也使得射线射程超过40。