2016-06-07 5 views
0

어떤 이유로이 충돌 감지 알고리즘이 작동하지 않습니다. 충돌이 일어 났을 때 화면에 경고 메시지가 출력되기를 기대하지만, 이런 일은 일어나지 않습니다. 나는 그것이 반환 진술과 관련이 있다고 믿는다. 나는 여기서 명백한 것을 놓치고 있는가?충돌 감지가 작동하지 않음 HTML5 게임

// checkHit is called in the game loop 

     Player.prototype.checkHit = function() { 
      for (var i = 0; i < enemies.length; i++) { 
      var colliding = testCollisionEntity(this, enemies[i]); 
      if (colliding) { 
       alert('hello'); 
      } 
      } 
     }; 


     testCollisionEntity = function (entity1,entity2){ 
      var rect1 = { 
      x: entity1.x - entity1.width/2, 
      y: entity1.y - entity1.height/2, 
      width: entity1.width, 
      height: entity1.height, 
      } 

      var rect2 = { 
      x: entity2.x - entity2.width/2, 
      y: entity2.y - entity2.height/2, 
      width: entity2.width, 
      height: entity2.height, 
      } 

      return testCollisionRectRect(rect1,rect2); 
     } 


     testCollisionRectRect = function(rect1,rect2) { 
      return rect1.x <= rect2.x + rect2.width 
      && rect2.x <= rect1.x + rect1.width 
      && rect1.y <= rect2.y + rect2.height 
      && rect2.y <= rect1.y + rect1.height; 
     } 
+0

너비/높이 X/Y 단위 동일한가되어야 하는가? 그들은 픽셀입니까? 픽셀 수가 특정 (예 : 32)입니까? 어떻게 그들을 받고/업데이트하고 있습니까? 왜 4가 필요할 때만 8 개의 정보를 충돌 감지 기능에 전달합니까? 당신은 그 모든 것이'&&' '이어야한다고 확신합니까? 경계는'a && b || c && d' 보통. 당신의 논리가 얻을 수있는 상태를 설명하지 않는다고 생각합니다. –

+0

감사합니다. 자레드, 당신은 내가 이것을 알아낼 수있게했습니다. 많은 감사합니다 – stckpete

+0

답변으로 게시하겠습니다. 동의하십시오. –

답변

0

(rect1.x <= rect2.x + rect2.width && rect1.y <= rect2.y + rect2.height) || 
(rect2.x <= rect1.x + rect1.width && rect2.y <= rect1.y + rect1.height) 
관련 문제