0

我正在使用 Lua 重新创建 Pacman 夫人,最近才了解到一系列瓷砖用于碰撞和移动;我正在尝试重新创建它。这是瓷砖地图:

nodemap = {
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1},
    {1,1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1,1},
    {1,1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1,1},
    {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
    {1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1},
    {1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1},
    {1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1},
    {1,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,1},
    {1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1},
    {1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1},
    {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
    {1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,0,1,1,1,0,1,1,1,1,1,0,1,1,1,1},
    {1,1,1,1,0,1,1,1,1,1,0,1,0,0,0,0,0,0,1,0,1,1,1,1,1,0,1,1,1,1},
    {1,1,1,1,0,1,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,1,1,1},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,但我遇到的问题是 Pacman 在瓷砖之间平滑移动,而不会从瓷砖跳到瓷砖,再到瓷砖......所以我有这个代码来“顺利”移动 Pacman:

-- Make Pacmans position consist of a single decimal (because over time a number of 1.899999994 will occur and that's ugly)
mrspacman.x = math.floor(mrspacman.x*10)/10
mrspacman.y = math.floor(mrspacman.y*10)/10

--If an arrowkey was pressed (Think of this as "A new direction was queued")
if (mrspacman.nextDirection) then

    -- If Pacman is in the center of a tile, then
    if (mrspacman.x == math.floor(mrspacman.x)) and (mrspacman.y == math.floor(mrspacman.y)) then

        -- If the tile in front of Pacman is empty, set direction to that
        if (nodemap[mrspacman.y-math2.sin(mrspacman.dir)][mrspacman.x+math2.cos(mrspacman.dir)]~=1) then
            mrspacman.dz = mrspacman.dir

            -- Disable this queue
            mrspacman.nextDirection = false
        end
    end
end

-- If Pacman is NOT in the center of a tile
if (mrspacman.x ~= math.floor(mrspacman.x)) or (mrspacman.y ~= math.floor(mrspacman.y)) then

    -- Constantly move forwards
    mrspacman.x = mrspacman.x + (math2.cos(mrspacman.dz)*mrspacman.speed)
    mrspacman.y = mrspacman.y - (math2.sin(mrspacman.dz)*mrspacman.speed)
else

    -- If the tile in front of Pacman is empty, move to that tile
    if (nodemap[mrspacman.y-math2.sin(mrspacman.dz)][mrspacman.x+math2.cos(mrspacman.dz)] ~= 1) then
        mrspacman.x = mrspacman.x + (math2.cos(mrspacman.dz)*mrspacman.speed)
        mrspacman.y = mrspacman.y - (math2.sin(mrspacman.dz)*mrspacman.speed)
    end
end
  • mrspacman.dz = 所面对的角度。

  • mrspacman.dir = 记录的按键。

  • mrspacman.speed = .1(允许平滑移动的小数)。

执行此代码会导致 Pacman 在按下后退键时冻结,并且在拐角处旋转时其位置变得混乱,穿过墙壁......可以在这里看到。

如何解决这个问题?

更新

我添加了一个名为 math2 的表,由 cos 和 sin fx 组成(只会返回 4 个基本方向的值):

math2 = {
    cos = function(angle)
        vectors = {1,0,-1,0}
        return vectors[(angle/90)+1]
    end,

    sin = function(angle)
        vectors = {0,1,0,-1}
        return vectors[(angle/90)+1]
    end
}
4

1 回答 1

0

所以这个问题非常广泛:

如何解决这个问题?答案是:通过调试你的程序。

问题很模糊,视频中的测试用例很大而且没有重点。要了解发生了什么,您至少需要详细查看程序的某些步骤。也就是说,如果墙左侧的瓷砖是空的,那么问题是 mrspacman 可以穿过墙。这暗示检查墙壁是否存在的代码可能存在一些错误。

问题中的代码不完整,因此错误可能来自它之外。尽管如此,在过去的一个小时里,我已经建立了一个原始框架来测试来自问题的代码。(它列在答案的末尾)

在玩弄了代码之后,我现在相信整个 pacman 的编写不仅没有单个三角函数,而且根本没有浮点数。

浮点数的问题是它们不适合精确计算。一些编译器和计算系统甚至会因为你这样做而对你吐口水。


在编辑之前,有问题的代码有正弦和余弦表达式:

math.floor(math.sin(mrspacman.dir*pi/180))

我现在已经在我的电脑上对它进行了评估,实际上它dir的值是. 但那是在 64 位机器上。我不确定是否可以信任 TI-Nspire 以产生相同的精度。0, 90, 180, 2700, 1, 0, -1


在我的测试中,我遇到了另一个浮动问题:

mrspacman.x = math.floor(mrspacman.x*10)/10

会在十进制数字中引入错误:mrspacman.x等于4.3它会4.2导致 pacwoman 冻结。奇怪的是,当直接在提示中求值时,它的行为是正确的。无论如何,该函数的常见实现math.floor(mrspacman.x*10+0.5)/10在地板之前增加了一些偏差: .


除此之外,还有一些轴方向的小问题,这要归功于框架转储的所有调试信息。

就这样。该代码有效(尽管,我将把留在这里而不需要更多参数)。如果代码仍然无法在您的设备上运行,您需要在其他地方查找问题。

调试代码

我已将您的变量保留为全局变量,因为它们可能在您的程序中,这在单文件脚本中可能无关紧要。local但总的来说,除非有非常好的理由,否则设置任何变量都是一个好习惯。

nodemap = {--I've used small field for debugging
  {1,1,1,1,1,1,1},
  {1,0,0,0,0,0,1},
  {1,0,1,0,1,1,1},
  {1,0,0,0,1,1,1},
  {1,1,1,1,1,1,1},
}

mrspacman={dz=0,dir=false,x=2,y=2,speed=0.1,nextDirection=false}

local dlog=function(...)--debug function
  print(string.format(...))
end

math2 = {
    cos = function(angle)
        vectors = {1,0,-1,0}--this would be better of by being a local variable
        return vectors[(angle/90)+1]
    end,

    sin = function(angle)
        vectors = {0,1,0,-1}--this too
        return vectors[(angle/90)+1]
    end
}


local graphic_lines={}
for _,line in ipairs(nodemap) do
  local l=table.concat(line):gsub("0"," ")
  table.insert(graphic_lines,l)
end

local time_step=1
function draw()--quick and dirty output to terminal
  print(string.format("\nStep %i",time_step))
  time_step=time_step+1
  local x,y = mrspacman.x,mrspacman.y
  local px,py=math.floor(x+0.5),math.floor(y+0.5)
  for index,line in ipairs(graphic_lines) do
    if index~= py then 
      print(line) 
    else
      print( line:sub(1,px-1) .. "@" .. line:sub(px+1))
    end
  end
  print(string.format('Real position (x,y): (%f , %f)',x,y))
  print(string.format('Current direction: %i',mrspacman.dz))
  if mrspacman.nextDirection then
    print(string.format("Trying to turn at %i",mrspacman.dir or "error"))
  end
end


local inputs={--i've mixed up directions due to my renderer
  w=90,
  a=180,
  s=270,
  d=0,
}
local skips={['']=true,[' ']=true}

function input(macro_input)--read wasd from terminal
  print('your move')
  local key= (macro_input and macro_input()) or io.read();
  if skips[key] then return end
  local dir = inputs[key];
  if not dir then print('wrong key') return end
  mrspacman.dir = dir
  mrspacman.nextDirection=true
end

function sim_step()
dlog('"Smoothing" (%f,%f) through (%f,%f) to (%f,%f)',mrspacman.x,mrspacman.y,mrspacman.x*10,mrspacman.y*10,math.floor(mrspacman.x*10)/10,math.floor(mrspacman.y*10)/10)
dlog('"Fixing smoothing" (%f,%f) through (%f,%f) to (%f,%f)',mrspacman.x,mrspacman.y,(mrspacman.x+0.05)*10,(mrspacman.y+0.05)*10,math.floor((mrspacman.x+0.05)*10)/10,math.floor((mrspacman.y+0.05)*10)/10)

mrspacman.x = math.floor(mrspacman.x*10+.5)/10--these lines are different
mrspacman.y = math.floor(mrspacman.y*10+.5)/10--

--If an arrowkey was pressed (Think of this as "A new direction was queued")
if (mrspacman.nextDirection) then
    dlog("trying to turn")
    -- If Pacman is in the center of a tile, then
    if (mrspacman.x == math.floor(mrspacman.x)) and (mrspacman.y == math.floor(mrspacman.y)) then
        dlog("Pacman is in the center of a tile")
        -- If the tile in front of Pacman is empty, set direction to that
        dlog("lookup dy: %f dx: %f",-math2.sin(mrspacman.dir),math2.cos(mrspacman.dir))
        dlog("lookup y: %f x: %f",mrspacman.y-math2.sin(mrspacman.dir),mrspacman.x+math2.cos(mrspacman.dir))
        dlog(nodemap[mrspacman.y-math2.sin(mrspacman.dir)][mrspacman.x+math2.cos(mrspacman.dir)]==1 and "Its a wall" or "")
        if (nodemap[mrspacman.y-math2.sin(mrspacman.dir)][mrspacman.x+math2.cos(mrspacman.dir)]~=1) then
            dlog("turning")
            mrspacman.dz = mrspacman.dir

            -- Disable this queue
            mrspacman.nextDirection = false
          else
            dlog"not turning"
        end
    end
end

-- If Pacman is NOT in the center of a tile
if (mrspacman.x ~= math.floor(mrspacman.x)) or (mrspacman.y ~= math.floor(mrspacman.y)) then
    dlog("Pacman is NOT in the center of a tile, moving with no collision check")
    -- Constantly move forwards
    dlog("deltaX, deltaY : %f, %f",(math2.cos(mrspacman.dz)*mrspacman.speed),-(math2.sin(mrspacman.dz)*mrspacman.speed))
    dlog("before (%f, %f)",mrspacman.x,mrspacman.y)
    mrspacman.x = mrspacman.x + (math2.cos(mrspacman.dz)*mrspacman.speed)
    mrspacman.y = mrspacman.y - (math2.sin(mrspacman.dz)*mrspacman.speed)
    dlog("after (%f, %f)",mrspacman.x,mrspacman.y)
else
    dlog("moving with collision check")
    -- If the tile in front of Pacman is empty, move to that tile
    if (nodemap[mrspacman.y-math2.sin(mrspacman.dz)][mrspacman.x+math2.cos(mrspacman.dz)] ~= 1) then
        dlog("can move")
        mrspacman.x = mrspacman.x + (math2.cos(mrspacman.dz)*mrspacman.speed)
        mrspacman.y = mrspacman.y - (math2.sin(mrspacman.dz)*mrspacman.speed)
    else
        dlog("hit wall")
    end
end

end


local gen_macro = function(sequence)--function generating predefined set of keypresses
  sequence=sequence or ""
  local step=0
  return function()
    step=step+1
    if (step>sequence:len()) then return end--return to manual control
    return sequence:sub(step,step)
  end
end

local simple_macro= function() return gen_macro("wasd") end--simple sample, "--will do nothing
local horizontal_bend = function()--will bring pacman to bottom left
  local _x10=string.rep(" ",10);
  local sequence=_x10:rep(2).."s".._x10:rep(2).."a".._x10:rep(2)
  return gen_macro(sequence)
end

local angry_monkey=function(length)--gen random sequence of buttons
  length=length or 0
  local keys={"a","s","d","w"," "}
  local seq = {}
  for i=1,length do
    table.insert(seq,keys[math.random(#keys)])
  end
  return gen_macro(table.concat(seq))
end

local macro
--macro=simple_macro()
--macro=horizontal_bend()
--macro = angry_monkey(100)

--dlog=function() end --uncomment this to disable debug

while true do
  draw()
  input(macro)
  sim_step()
--  if time_step>10 then return end
end
于 2018-05-16T13:08:47.707 回答