2012-05-01 118 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