2012-09-09 6 views
2

저는 Love2d 프레임 워크에서 간단한 게임에서 오브젝트와 충돌을 관리하기위한 기본 아키텍처를 만들려고합니다. 모든 개체는 테이블 (objects:activeObjects)에 저장되고 objects:calculateCollisions() 함수의 루프는 모든 개체를 반복합니다. 각 반복마다 다른 중첩 루프가 해당 객체가 동일한 테이블의 다른 객체와 겹치는 지 확인합니다. objects:calculateCollisions()의 끝에는 각 오브젝트가 이상적으로 현재 시점에서 오버랩하는 모든 오브젝트에 대한 참조를 포함하는 테이블을 가지게됩니다. 그러나 개체에는 항상 빈 충돌 테이블이 있습니다.

지금은 두 가지 테스트 개체가 있습니다. 하나는 마우스로 움직이고 다른 하나는 항상 오른쪽 상단 구석에 머물러 있습니다. 두 객체는 ​​겹칠 때 동시에 사라져야하지만, 앞서 언급했듯이 collidingObjects 테이블은 항상 비어 있습니다.
main.lua :

나는 세 개의 소스 파일이
(중요한 물건의 대부분을 기록, 아마 어디에 문제가) http://pastebin.com/xuGBSv2j
objects.lua :
http://pastebin.com/sahB6GF6
customObjects.lua (여기서 두 테스트 객체의 생성자) 정의 :
사각 충돌 시스템이 love2d에서 작동하지 않습니다.

function objects:newCollidingObject(x, y) 
    local result = self:newActiveObject(x, y, 50, 50) 
    result.id = "collidingObject" 
    function result:collide(other) 
     if other.id == "collidingObject" then self.remove = true end 
    end 
    return result 
end 
function objects:newMovingObject(x, y) 
    local result = self:newCollidingObject(x, y) 
    function result:act() 
     self.x = love.mouse.getX() 
     self.y = love.mouse.getY() 
    end 
    return result 
end 

미안 해요, 두 개 이상의 하이퍼 링크를 게시 할 수 없습니다.

편집 : 좀 더 디버깅을 한 후에는 collidesWith(obj) 기능으로 문제를 좁혔습니다. 항상 거짓을 반환하는 것 같습니다.

function result:collidesWith(obj) 
    if self.bottom < obj.top then return false end 
    if self.top > obj.bottom then return false end 
    if self.right < obj.left then return false end 
    if self.left > obj.right then return false end 
    return true 
end 
+1

해당 속성이 유효합니까? (두 개체의 맨 아래, 맨 위, 오른쪽 및 왼쪽) – Darkwater

답변

1

을 통해 이동하고 몇 가지 테스트 입력을 종이에 논리를 추적 :
여기에 코드입니다. 논리가 무의미하다는 것을 빨리 알 수 있습니다.

function result:collidesWith(obj) 
    if self.bottom <= obj.top and self.bottom >= obj.bottom then return true end 
    if self.top >= obj.bottom and self.top <= obj.top then return true end 
    if self.right >= obj.left and self.right <= obj.right then return false end 
    if self.left <= obj.right and self.left >= obj.left then return false end 
    return false 
end 

트릭을 할해야 : 당신은 당신의 비교의 일부는 그래서 등, "잘못된 방향을 가리키는"된다, 테스트의 절반을 놓치고있어.

+0

이동 한 개체의 변수를 왼쪽, 오른쪽 등으로 업데이트하지 않았습니다. 모두 고쳐졌습니다. – teasplurt

관련 문제