2014-03-29 4 views
0

간단한 플랫폼 게임을 만들기 위해 노력 중이며 분명히 타일 충돌이 필요합니다. 내가 지금까지 가지고있는 코드의 문제점은 캐릭터를 먼저 움직이고 충돌이 있는지를 확인하는 것이지만 때로는 충돌을 위해 x 축을 먼저 검사하는지에 따라 잘못된 시간에 충돌을 생각하는 경우가 있습니다 또는 y 축을 먼저 찾습니다. 내가 잘못된 방향으로가는거야? 여기에 몇 가지 코드가 있습니다.타일 기반 충돌을 수행하는 방법

function checkCollision(val, axis, oldPos) 
    if axis == "x" and char.tX then 
     local tileX = math.ceil(val/absoluteTileSize) 
     local tileY = math.floor(oldPos/absoluteTileSize) 

     local tl, tr, bl ,br = getTouchingTiles(tileX, tileY) 
     local isOnFlatSurface = math.abs(oldPos/absoluteTileSize-tileY) <= .00001--might not be a good i 

     if isOnFlatSurface then 
      if tr.canCollide then 
       char.tX = nil 
       char.x = tileX * absoluteTileSize - absoluteTileSize 
      end 
     else 
      if br.canCollide then 
       char.tX = nil 
       char.x = tileX * absoluteTileSize - absoluteTileSize 
      end 
     end 
    elseif axis == "y" then 
     local tileX = math.ceil(oldPos/absoluteTileSize) 
     local tileY = math.floor(val/absoluteTileSize) 

     local tl, tr, bl ,br = getTouchingTiles(tileX, tileY) 

     if bl.canCollide or br.canCollide then 
      char.tY = nil 
      char.y = tileY * absoluteTileSize --// - absoluteTileSize 
      --/////////////idk why i don't need to subtract that but it works 
     elseif not char.tY then--start falling if walk off something 
      char.tY = love.timer.getTime() 
      char.yi = char.y 
      char.vyi = 0 
     end 
    end 
end 

답변

0

지역는 tileX = math.ceil (발/absoluteTileSize)
지역 tileY의 = math.floor (oldPos/absoluteTileSize는)

그것은 당신이 math.ceil를 사용하는 것이 이상한 것 같다 x 값과 y에 대한 math.floor. 이것은 이상한 사건이 발생하는 이유 일 수 있습니다.

-- Since you are using LÖVE, this is what you would use: 
love.graphics.setColor(255, 0, 0, 255) 
love.graphics.rectangle('line', (tileX - 1) * absoluteTileSize, (tileY - 1) * absoluteTileSize, absoluteTileSize, absoluteTileSize) 
-- assuming absoluteTileSize represents the width/height of the tiles? 

이 드로잉 함수의 말에 갈 것이라고하고 "타일"에 빨간색 상자를 그립니다 것 플레이어는 현재 내부에 : 나는 당신을 도울 수도 있습니다 약간의 디버깅 트릭을 추천 할 것입니다.

관련 문제