平铺碰撞检测

时间:2012-05-01 07:06:24

标签: lua tile love2d

所以,我一直在研究这个游戏。然而,在过去的一天里,我一直无法弄清楚如何进行碰撞检测。

默认情况下的比例等于2.

玩家的比例为41 *,比例为64 *。

我的播放器在x轴和y轴的中间位于屏幕中间。

由于玩家居中,世界就是移动,这些变量是世界的和世界的。播放器始终位于屏幕中央。

我的图块地图存储在一个数组中,并基于图像像素颜色。如果map [x] [y]处的像素为白色,则该值设置为0,否则将其设置为块。意味着块不会被渲染。

for x = 0, w-1 do --scans the image and builds the map array
   amap[x] = {}
   for y = 0, h-1 do
      local r, g, b, a = source:getPixel(x, y)
      if r == 255 and g == 255 and b == 255 then
         block = 0
      end
      if r == 255 and g == 100 and b == 0 then
         block = 1
      end
      if r == 130 and g == 125 and b == 0 then
         block = 2
      end
      if r == 76 and g == 76 and b == 76 then
         block = 3
      end
      if r == 255 and g == 0 and b == 255 then
         --this is the spawn pixel yet to build
      end
      amap[x][y] = block
   end
end --end function

绘制地图的功能

for x = 0, w-1 do --draws the map
   for y = 0, h-1 do
      if amap[x][y] ~= 0 then
         love.graphics.drawq(ImgBlocks, Blocks[amap[x][y]], 32*x*(3/bscale) + worldx, 32*y*(3/bscale) + worldy + jy, 0 , 3/bscale, 3/bscale)
      end
      if amap[x][y] == 4 then  
      end
   end
end --end function

如果播放器和块之间发生冲突,该函数需要返回true或false。

2 个答案:

答案 0 :(得分:1)

你的瓷砖是32x32,对吗? (来自drawq调用)我建议你创建一个函数来检查一个点是否在实心图块中:

function pointCollisionTest(x, y)
    -- find which tile the point is in
    local tx, ty = math.floor(x / 32), math.floor(y / 32)
    -- check the tile
    if map[tx][ty] == solid then
        return true
    else
        return false
    end
end

您必须根据确定实体图块的方式更改if map[x][y] == solid逻辑,但此代码应该可以正常工作。

一旦你有点碰撞,你让玩家碰撞的方式是每当玩家移动时,通过检查其击球箱的每个角落(你应该很容易确定)来对抗这个功能。有几种方法可以做到这一点;我使用相对简单的方法计算玩家的新位置,测试它,然后在碰撞测试返回true时完全取消移动。但是,你必须分别检查/取消移动的x和y分量,这样玩家才能沿着墙壁移动而不是坚持它们。

答案 1 :(得分:0)

您是否要求进行基本的2d碰撞检测?

简化公式: if(playerx> blockminx)和(playery< blockmaxx)和(playery> blockminy)和(playery< blockmaxy)然后结合