2016-11-19 4 views
0

JavaScript로 게임 엔진에서 작업 중이며 현재 충돌 시스템에서 작업 중입니다. 플레이어와 감지 된 충돌을 해결하는 알고리즘을 만들었지 만 의도 한 동작을 얻지는 못했습니다. 다음은 코드입니다.AABB 충돌 감지/해결 코드에 어떤 문제가 있습니까?

if (gameObjects [i].hasComponent ("BoxCollider") == true) { 
     for (var x = 0; x < gameObjects.length; x++) { 
      if (gameObjects [x].hasComponent ("BoxCollider") == true && gameObjects [i] != gameObjects [x]) { 
       if (gameObjects [i].transform.xPos - (gameObjects [i].getComponent ("BoxCollider").width/2) < gameObjects [x].transform.xPos + (gameObjects [x].getComponent ("BoxCollider").width/2) && gameObjects [i].transform.xPos + (gameObjects [i].getComponent ("BoxCollider").width/2) > gameObjects [x].transform.xPos - (gameObjects [x].getComponent ("BoxCollider").width/2) && gameObjects [i].transform.yPos - (gameObjects [i].getComponent ("BoxCollider").height/2) < gameObjects [x].transform.yPos + (gameObjects [x].getComponent ("BoxCollider").height/2) && gameObjects [i].transform.yPos + (gameObjects [i].getComponent ("BoxCollider").height/2) > gameObjects [x].transform.yPos - (gameObjects [x].getComponent ("BoxCollider").height/2)) { 
        //collision 
        if (gameObjects [i] == player) { 

         var xPenetration = 0; 
         var yPenetration = 0; 

         //i is left of x 
         if (gameObjects [i].transform.xPos < gameObjects [x].transform.xPos) { 
          xPenetration = -1 * ((/*i right edge*/gameObjects [i].transform.xPos + gameObjects [i].boxCollider.width/2) - (/*x left edge*/gameObjects [x].transform.xPos - gameObjects [x].boxCollider.width/2)); 
         } 

         //i is right of x 
         else if (gameObjects [i].transform.xPos > gameObjects [x].transform.xPos) { 
          xPenetration = ((/*x right edge*/gameObjects [x].transform.xPos + gameObjects [x].boxCollider.width/2) - (/*i left edge*/gameObjects [i].transform.xPos - gameObjects [i].boxCollider.width/2)); 
         } 

         //i is top of x 
         if (gameObjects [i].transform.yPos < gameObjects [x].transform.yPos) { 
          yPenetration = -1 * ((/*i bottom edge*/gameObjects [i].transform.yPos + gameObjects [i].boxCollider.height/2) - (/*x top edge*/gameObjects [x].transform.yPos - gameObjects [x].boxCollider.height/2)); 
         } 

         //i is bottom of x 
         else if (gameObjects [i].transform.yPos > gameObjects [x].transform.yPos) { 
          yPenetration = ((/*x bottom edge*/gameObjects [x].transform.yPos + gameObjects [x].boxCollider.height/2) - (/*i top edge*/gameObjects [i].transform.yPos - gameObjects [i].boxCollider.height/2)); 
         } 

         if (Math.abs (xPenetration) > Math.abs (yPenetration)) { 
          gameObjects [i].transform.xPos += xPenetration; 
         } else { 
          gameObjects [i].transform.yPos += yPenetration; 
         } 

        } 
       } 
      } 
     } 
    } 
} 
+2

"하지만 의도 한 동작을 얻지는 못했습니다." 문제가 무엇인지 이해하는 데 도움이되지 않습니다. 실행 가능한 예제가 도움이 될 수 있습니다. – InferOn

+0

'내 [...] 코드에 문제가 있습니까? '코드에 목적/지정된 동작을 나타내는 설명이 없습니다. 코드는 SE의 코드 엿보기에서 특히 읽기가 어렵습니다. 테스트'gameObjects [i] == player'는 변하지 않습니다. 루프 밖으로 이동하십시오. 들여 쓰기 수준 줄이기 - 리팩토링이 부족하고,'continue' 문을 사용하십시오. 소스 코드 수준에서'(gameObjects [i] .getComponent ("BoxCollider") .width/2)와 같은 공통 하위 표현식을 "제거"합니다. 디버깅 지원을 요청할 때 필수적이고 관찰 된 동작을 언급하지 않으면 질문을 닫을 수 있습니다. – greybeard

+0

무슨 일입니까? 충돌을 전혀 감지하지 못합니까? 가양 성 충돌을 감지합니까? 그것을 감지하고 있지만 잘못 응답하고 있습니까? – samgak

답변

1

두 가지 문제가 있습니다. 첫째, gameObjects [i].transform.xPos이 정확히 gameObjects [x].transform.xPos 인 경우 (y 위치에서 동일)를 처리해야합니다. 현재 귀하는보다 적거나 같거나 그 이상을 처리하고 있습니다. 그러니 그냥 <=< 또는 >=> 변경 예 :

if (gameObjects [i].transform.xPos <= gameObjects [x].transform.xPos) 

다른 문제는 당신이 충돌 할 때 당신이이 축을 따라 충돌 한 개체에서 개체를 밀어 경우, 그것은 더 자연스러운 모습이다 최소 침투 거리. 따라서이 행을 변경하여 max 대신 x 및 y를 선택하십시오.

if (Math.abs (xPenetration) < Math.abs (yPenetration)) 
+0

감사합니다! 대답의 두 번째 부분에서 간단한 변화가 일어난 후에 모두 효과가 있습니다. –