Indexing Lua向上移动表格导致零值错误
我有一个表格表,它表示一个65*65的正方形网格,其中每个元素都是整数值。它代表演员周围的区域,演员可以在四个基本方向上移动,当他们这样做时,我需要桌子与他们一起移动,以便他们保持在中心广场33,33 我没有移动表的元素,而是有两个指针tableIndex.left和tableIndex.down,分别存储最下面一行最左边列的索引,它们都从65开始。我有三个助手函数来执行表上的操作,它们都取参数x1,x2,y1,y2,其中x1和y1是开始函数的坐标,x2和y2是结束函数的坐标 这些函数是tableInit,它在x1和x2之间的所有值中创建表。零表,用0填充指定范围内的所有条目;清除表,将指定范围内的所有条目设置为零Indexing Lua向上移动表格导致零值错误,indexing,lua,null,lua-table,Indexing,Lua,Null,Lua Table,我有一个表格表,它表示一个65*65的正方形网格,其中每个元素都是整数值。它代表演员周围的区域,演员可以在四个基本方向上移动,当他们这样做时,我需要桌子与他们一起移动,以便他们保持在中心广场33,33 我没有移动表的元素,而是有两个指针tableIndex.left和tableIndex.down,分别存储最下面一行最左边列的索引,它们都从65开始。我有三个助手函数来执行表上的操作,它们都取参数x1,x2,y1,y2,其中x1和y1是开始函数的坐标,x2和y2是结束函数的坐标 这些函数是tabl
function movetable(direction)
--function to move our probability histogram
-- 0 is up, 1 is right, 2 is down, 3 is left
--as a note robotSpeed = 1
if direction == 0 then
tableIndex.down += robotSpeed
zerotable(tableIndex.left,tableIndex.left+64,tableIndex.down+64,tableIndex.down+64)
cleartable(tableIndex.left,tableIndex.left+64,tableIndex.down-1,tableIndex.down-1)
elseif direction == 1 then
tableIndex.left += robotSpeed
tableInit(tableIndex.left+64,tableIndex.left+64)
zerotable(tableIndex.left+64,tableIndex.left+64,tableIndex.down,tableIndex.down+64)
cleartable(tableIndex.left-1,tableIndex.left-1,tableIndex.down,tableIndex.down+64)
elseif direction == 2 then
tableIndex.down -= robotSpeed
zerotable(tableIndex.left,tableIndex.left+64,tableIndex.down,tableIndex.down)
cleartable(tableIndex.left,tableIndex.left+64,tableIndex.down+65,tableIndex.down+65)
else
tableIndex.left -= robotSpeed
tableInit(tableIndex.left,tableIndex.left)
zerotable(tableIndex.left,tableIndex.left,tableIndex.down,tableIndex.down+64)
cleartable(tableIndex.left+65,tableIndex.left+65,tableIndex.down,tableIndex.down+64)
end
end
该表将在程序后面的代码段中调用,这是引发错误的地方“试图对字段'?'执行算术运算的值为零。”。当i和j为0时会发生这种情况。这仅在表最后一次上移后发生,因此dir==0,并且如果在dir==0的情况下注释掉movetable函数中的cleartable调用,则错误不再存在
for i=0,64 do
for j=0,64 do
--Robot is at 33 33 so we know there isnt an obstacle here
if i ~= 32 and j ~= 32 then
--This is the equation from the paper which tells us which direction the result is in
local tan = atan2(i-33,j-33)+0.25
if (tan > 1) tan = tan-1
local dInD = flr(360*tan)
--Our polar histogram has sectors ALPHA degrees wide, this rounds the direction down to a multiple of
--the sector size so we can use it as an index
local dir = dInD - dInD%ALPHA
polarHist[dir] += certaintyTable[tableIndex.left+i][tableIndex.down+j]
end
end
请注意,我没有访问标准lua库的权限
这是一张清晰的表格,以防我做错了
function cleartable(x1,x2,y1,y2)
if x1 == x2 then
for j=y1,y2 do
certaintyTable[x1][j] = nil
end
elseif y1==y2 then
for i=x1,x2 do
certaintyTable[i][y1] = nil
end
else
for i=x1,x2 do
for j=y1,y2 do
certaintyTable[i][j] = nil
end
end
end
end
感谢您查看我的问题将
atan2(i-33,j-33)
更改为atan2(i-32,j-32)
将如果i~=32和j~=32
更改为如果i~=32或j~=32
请给出atan2(-1,-2)
的结果,以了解此函数返回的内容。并演示如何初始化polarHist数组。例如,atan2(1,1)是0.875(7/4π弧度,或315度),atan2(-1,-1)是0.375(3/4π弧度,或135度)。类似地,atan2(0,1)是0.75“极性历史被初始化为如此,函数polarHistInit()local sectorNum=360/ALPHA polarHist.size=sectorNum For i=0,sectorNum do polarHist[i*ALPHA]=0 end end end
引发错误的行是polarHist[dir]+=certaintyTable[tableIndex.left+i][tableIndex.down+j]
什么是atan2(0,0)
?在我的前两条评论中描述的更正之后,问题是否仍然存在?将atan2(i-33,j-33)
更改为atan2(i-32,j-32)
将如果i~=32和j~=32更改为如果i~=32或j~=32
请给出atan2(-1,-2)
的结果,以了解此函数返回的内容。并演示如何初始化polarHist
数组。例如,atan2(1,1)是0.875(7/4π弧度,或315度),atan2(-1,-1)是0.375(3/4π弧度,或135度)。类似地,atan2(0,1)是0.75“极性历史被初始化为如此,函数polarHistInit()local sectorNum=360/ALPHA polarHist.size=sectorNum For i=0,sectorNum do polarHist[i*ALPHA]=0 end end end
引发错误的行是polarHist[dir]+=certaintyTable[tableIndex.left+i][tableIndex.down+j]
什么是atan2(0,0)
?在我的前两条评论中描述的更正之后,问题是否仍然存在?