瓷砖碰撞检测

所以,我已经在做这个游戏一段时间了。但是,在过去的一天里,我无法弄清楚如何使我的碰撞检测工作起来。

默认的比例尺是2。

玩家是41比例尺乘以64比例尺。

我的玩家在x轴和y轴的中间位置,始终保持在屏幕中心。

由于玩家处于中心位置,世界会移动,这些变量是worldx和worldy。玩家始终保持在屏幕中心。

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

对于x = 0,w-1 do --扫描图像并构建地图数组
   amap[x] = {}
   for y = 0,h-1 do
      local r,g,b,a = source:getPixel(x,y)
      如果r == 255 and g == 255 and b == 255 then
         block = 0
      end
      如果r == 255 and g == 100 and b == 0 then
         block = 1
      end
      如果r == 130 and g == 125 and b == 0 then
         block = 2
      end
      如果r == 76 and g == 76 and b == 76 then
         block = 3
      end
      如果r == 255 and g == 0 and b == 255 then
         -这是生成像素,但还没有构建
      end
      amap[x][y] = block
   end
end --结束函数

绘制地图的函数

对于x = 0,w-1 do --绘制地图
   for y = 0,h-1 do
      如果amap[x][y] ~= 0 then
         love.graphics.drawq(ImgBlocks,Blocks[amap[x][y]],32*x*(3/bscale)+worldx,32*y*(3/bscale)+worldy+jy,03/bscale,3/bscale)
      end
      if amap[x][y] == 4 then
      end
   end
end --结束函数

该函数需要根据玩家和瓷砖之间是否存在碰撞来返回true或false。

原文链接 https://stackoverflow.com/questions/10394630

点赞
stackoverflow用户910087
stackoverflow用户910087

您是否要求基本的2D碰撞检测?

一个简化的公式: 如果(playerx > blockminx)且(playery < blockmaxx)且(playery > blockminy)且(playery < blockmaxy),则发生碰撞。

2012-05-01 22:48:13
stackoverflow用户685933
stackoverflow用户685933

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

function pointCollisionTest(x, y)
    -- 查找点在哪个瓷砖中
    local tx, ty = math.floor(x / 32), math.floor(y / 32)
    -- 检查瓷砖
    if map[tx][ty] == solid then
        return true
    else
        return false
    end
end

你将不得不根据你如何确定实心瓷砖来更改 if map[x][y] == solid 逻辑,但是除此之外,此代码应该可行。

一旦你有了点碰撞,你让玩家碰撞的方式是每当玩家移动时检查其碰撞框的每个角落(你应该能够轻松确定)是否与这个函数相交。有几种方法可以做到这一点;我使用相对简单的方法计算玩家的新位置,进行测试,如果碰撞测试返回true,则完全取消移动。但是,你必须分别检查/取消移动的x和y分量,这样玩家就可以沿着墙壁移动而不是粘在上面。

2012-05-19 19:32:33