2012-05-01 117 views
0

所以,我一直对这个游戏有点。但是,在过去的一天中,我一直无法弄清楚如何进行碰撞检测。瓷砖碰撞检测

规模在默认等于2

球员41 *标度由64 *规模。

我的播放机在屏幕的在x和y轴两者的中间居中。

由于玩家为中心的世界是什么动作,这些变量是worldx和worldy。玩家总是停留在屏幕的中心。

我的瓷砖地图被存储在数组中,并且基于该图像的像素的颜色。如果像素在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基础上,如果有球员和块之间的碰撞。

回答

1

瓷砖经过32×32,正确的吗? (从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组件,所以玩家可以沿着墙壁移动而不是坚持移动。

0

你问一个基本的2D碰撞检测?

的简化公式: 如果(PlayerX的> blockminx)和(playery < blockmaxx)和(playery> blockminy)和(playery < blockmaxy)然后collission