2014-03-13 5 views
1

그래서 약간의 변경을가 했음에도 불구하고 this blog, 과 충돌 시스템을 기반으로 작업하고 있습니다. 그 코드에 몇 가지 문제가 있었으며, 여기 righttopleft과 문제 : 아래, 여기에 스크린 샷 (녹색 해상도 이전 인 빨강, 후입니다)이 있습니다이 내 코드입니다 bottom2D 충돌 해결 문제

(미안의 혼란과 최적화되지 않은, 내가 한 번을 다시 계획했다 나는 알고리즘을 이해했다 :

public function ResolveCollision(a : CollisionComponent, b : CollisionComponent) 
    { 
     var aAABB = new AABB(new Point(a.GetBounds().x, a.GetBounds().y), 
          new Point(a.GetBounds().x + a.GetBounds().width, 
          a.GetBounds().y + a.GetBounds().height)); 
     var bAABB = new AABB(new Point(b.GetBounds().x, b.GetBounds().y), 
          new Point(b.GetBounds().x + b.GetBounds().width, 
          a.GetBounds().y + a.GetBounds().height)); 

     var direction : Point = new Point(); 
     direction.x = aAABB.topLeft.x - bAABB.topLeft.x; 
     direction.y = aAABB.topLeft.y - bAABB.topLeft.y; 

     var end : AABB = new AABB();  
     end.bottomRight.x = Math.min(aAABB.bottomRight.x, bAABB.bottomRight.x); 
     end.bottomRight.y = Math.min(aAABB.bottomRight.y, bAABB.bottomRight.y); 

     end.topLeft.x = Math.max(aAABB.topLeft.x, bAABB.topLeft.x); 
     end.topLeft.y = Math.max(aAABB.topLeft.y, bAABB.topLeft.y); 

     var overlap : Point = new Point(); 
     overlap.x = end.bottomRight.x - end.topLeft.x; 
     overlap.y = end.bottomRight.y - end.topLeft.y; 

     var moveAxis : Int; //0:x, 1:y 
     if (overlap.x < overlap.y) 
     { 
      moveAxis = 0; 
     } 
     else 
     { 
      moveAxis = 1; 
     } 

     if (moveAxis == 0) 
     { 
      a.Move(new Point(sign(direction.x) * overlap.x, 0)); 
     } 
     else if (moveAxis == 1) 
     { 
      a.Move(new Point(0, sign(direction.y) * overlap.y)); 
     } 
    } 

    private function sign(i : Float) : Int 
    { 
     if (i < 0) 
     { 
      return -1; 
     } 
     else if (i > 0) 
     { 
      return 1; 
     } 
     else 
     { 
      return 0; 
     } 
    } 

만약 누군가가 저에게 문제를 지적한다면, 그것은 좋을 것입니다, 그렇지 않다면, 나는 그것을 간단한 방법으로 글을 쓸만큼 충분히 이해하고 있고, 원인을 찾아 낼 수 있다고 생각합니다. ...

thanks , 니코

+0

다이어그램과 모든 질문에 대한 정말 좋은 설명을 위해 Upvoted : –

+0

감사합니다, 나는 고맙습니다 :) – Nico

답변

0

는 (여기서 물론 내가 확인하지 않았다), I는 "A"상자들에 "B"상자의 속성 중 일부를 초기화 한 AABB의 창조 바보 같은 실수 죄송합니다 :

var bAABB = new AABB(new Point(b.GetBounds().x, b.GetBounds().y), 
          new Point(b.GetBounds().x + b.GetBounds().width, 
          **a.GetBounds()**.y + **a.GetBounds()**.height));