2014-10-15 6 views
0

캔버스에 두 개의 사각형을 그립니다. canvas.drawRect(top,left,right,bottom) 사각형 중 하나는 정적이고 다른 하나는 안드로이드 가속도계를 사용하여 움직입니다.두 개의 Rect 사이의 충돌 감지 안드로이드에서

  1. 이제 내가 많은 방법을 시도

두 사각형 사이의 충돌을 감지하고 싶지만 그들이 두 사각형을 완벽하게되지 않은 중복 때로는 그래서 그들 사이의 충돌을 감지 할 수있는 완벽한 방법은 무엇입니까 . 내가 영웅을 시도 무엇

는 이동 삼각형 미로 구성 요소

  if ((hero.top >= mazeComponent.top) 
       && (hero.top <= mazeComponent.bottom) 
       && (hero.left <= mazeComponent.right)) { 
      collision = true; 
     } 
     if ((hero.bottom >= mazeComponent.top) 
       && (hero.bottom <= mazeComponent.bottom) 
       && (hero.right >= mazeComponent.left)) { 
      collision = true; 
     } 
     if ((hero.left >= mazeComponent.left) 
       && (hero.left <= mazeComponent.right) 
       && (hero.top <= mazeComponent.bottom)) { 
      collision = true; 
     } 
     if ((hero.top >= mazeComponent.top) 
       && (hero.top <= mazeComponent.bottom) 
       && (hero.left >= mazeComponent.left)) { 
      collision = true; 
     } 
     if ((hero.left >= mazeComponent.left) 
       && (hero.left <= mazeComponent.right) 
       && (hero.top >= mazeComponent.top)) { 
      collision = true; 
     } 

답변

1

자바/안드로이드 사각형과 사각형 물체가 충돌을 테스트하는 데 사용할 수있는 intersect 방법이 정적 사각형입니다

+0

이 함수는 collison 예를 감지하지만 두 개의 사각형이 여전히 겹칩니다. 움직이는 하나의 정적 사각형이 병합되지 않도록하고 싶습니다! – Rami

+0

움직이는 사각형은 실제로 어떻게 움직입니까? 상수 루프에서 반복적으로 다시 그려지 는가? 그렇다면 다시 그리기 전에 교차로를 테스트 할 수 없습니까? 새로운 위치가 교차하는 경우 다시 그리지 않습니까? – ali2992