2012-01-14 5 views
0

나는 게임 엔진을 쓰고 있으며 배우 ​​클래스에 문제가 있습니다. 사각형 (위)과 사각형의 한면으로 충돌을 확인하고 싶습니다. 나는 그들을 위해 두 가지 방법을 썼다.자바 게임 충돌 감지, (측면 충돌) 사각형으로

public boolean isLeftCollision(Actor actor) { 
    boolean bool = false; 
    Rectangle LeftBounds = new Rectangle(x, y, x-velocity, image.getHeight(null)); 
    bool = LeftBounds.intersects(actor.getBounds()); 
    return bool; 
} 

public boolean isRightCollision(Actor actor) { 
    boolean bool = false; 
    Rectangle RightBounds = new Rectangle(x+image.getWidth(null), y, image.getWidth(null)+velocity, image.getHeight(null)); 
    bool = RightBounds.intersects(actor.getBounds()); 
    return bool; 
} 

여기서 속도는 다음 단계의 움직임입니다.

그러나 둘 다 나에게 오류를줍니다 (즉, 잘못된 판단). 배우 클래스에서이를 어떻게 해결할 수 있습니까?

+0

(가정 속도가 호출 될 것이다 당신이 이동하는 방향을 왼쪽이나 오른쪽으로 이동하기 위해 (양의) 거리 만하는 방법입니다) 오류 로그를 추가하십시오 –

+0

@stas 오류 로그를 추가하는 방법 –

+0

권자. 프로그램을 실행하고 오류를 복사하여 붙여 넣습니다. –

답변

1

나는 당신의 코드를 거의 읽을 수 없다는 것을 인정하고 나의 대답이 도움이되지 않는다면 미안하다. 내 추측은 충돌 속도가 오류를 만든다는 것입니다. 얼마나 자주 확인하고 어떤 벨로 시티가 유지되는지에 따라 아직 발생하지 않은 충돌을 등록 할 수 있습니다 ...

두 단계로 충돌 감지를 수행합니다. 위나 한쪽 만약 충돌

  • 위한

    1. 테스트 결정. 다른 배우가 당신에게 상대적 위치 당신이 볼 수

      Rectangle self_shape=this.getBounds(); 
      Rectangle other_shape=actor.getBounds(); 
      bool collision = self_shape.intersects(other_shape); 
      if(collision){ 
          //create two new variables self_centerx and self_centery 
          //and two new variables other_centerx and other_centery 
          //let them point to the coordinates of the center of the 
          //corresponding rectangle 
      
          bool left=self_centerx - other_centerx<0 
          bool up=self_centery - other_centery<0 
      } 
      

      그 방법 :

    는 여기에 몇 가지 의사입니다. 그것이 위 또는 한쪽면.

  • +0

    답변 해 주셔서 감사합니다. –

    1

    Rectangle의 세 번째 매개 변수는 너비이며 다른 쪽의 x는 아닙니다. 그럼, 당신이 정말로 원하는 것은이 같은 아마도 :

    public boolean isLeftCollision(Actor actor) { 
        return new Rectangle(x - velocity, y, velocity, image.getHeight(null)) 
         .intersects(actor.getBounds()); 
    } 
    
    public boolean isRightCollision(Actor actor) { 
        return new Rectangle(x + image.getWidth(null), y, velocity, image.getHeight(null)) 
         .intersects(actor.getBounds()); 
    }