2016-08-14 7 views
0

안녕하세요. C#/XNA에 약간의 게임을 만들고 있는데 충돌로 인해이 문제가 있습니다.C# Rectangle 충돌 감지가 작동하지 않습니다.

내 코드에서 내가하는 일은 정상적인 플레이어 이동을 만드는 것입니다. 예를 들어 위로 키를 누른 경우 플레이어 CanGoTop == true를 확인하는 것뿐입니다. bollean이 false 인 경우

if (keyState.IsKeyDown(Keys.Up) && CanGoTop == true) 
{ 
bla bla bla 
} 

나는 왼쪽이 작업을 수행, 오른쪽, 위, 아래, 그리고 플레이어는 속도, x와 .Y을 설정 movemnt의 그 4 문에서도 후 다른 사람의 한 Statment이 있습니다 (이동하지 않습니다 0으로)

이제 Player 클래스 내에서 메서드를 만들었습니다. 그냥보고있는 것만으로도 여기에 무슨 일이 일어나는지 설명해야한다고 생각하지 않습니다.

 public void RectangleInteraction(Rectangle anotherRectangle) 
    { 
     if (playerRectangle.Bottom <= anotherRectangle.Top && 
      playerRectangle.Top >= (anotherRectangle.Top - playerRectangle.Height) && 
      playerRectangle.Left <= anotherRectangle.Right && 
      playerRectangle.Right >= anotherRectangle.Left) 
     { 
      CanGoBot = false; 
     } 
     else CanGoBot = true; 


     if (playerRectangle.Top >= anotherRectangle.Bottom && 
      playerRectangle.Bottom <= (anotherRectangle.Bottom + playerRectangle.Height) && 
      playerRectangle.Left <= anotherRectangle.Right && 
      playerRectangle.Right >= anotherRectangle.Left) 
     { 
      CanGoTop = false; 
     } 
     else CanGoTop = true; 


     if (playerRectangle.Top <= anotherRectangle.Bottom && 
      playerRectangle.Bottom >= anotherRectangle.Top && 
      playerRectangle.Right <= anotherRectangle.Left && 
      playerRectangle.Left >= (anotherRectangle.Left - playerRectangle.Width)) 
     { 
      CanGoRight = false; 
     } 
     else CanGoRight = true; 


     if (playerRectangle.Top <= anotherRectangle.Bottom && 
      playerRectangle.Bottom >= anotherRectangle.Top && 
      playerRectangle.Left >= anotherRectangle.Right && 
      playerRectangle.Right <= (anotherRectangle.Right + playerRectangle.Width)) 
     { 
      CanGoLeft = false; 
     } 
     else CanGoLeft = true; 

    } 

내 Game1.cs Update() 메소드에서 플레이어 insta를 사용합니다. nce는 그 방법을 호출하고, 지금은 저를 괴롭히는 것입니다. CanGoLeft에서는 작동하지만 다른 3 개의 bool에서는 작동하지 않습니다.

나는 왜 여기에 내 InGameMsgs가있는 4 스크린 샷이 있는지 알려주지 않습니다. 내 코드로 메시지를 확인하면 CollisionLogic이 좋다고 알려주지 만 다른 것은 잘못되었습니다. 그럼 왜 CanGoLeft 만 작동합니까? :/

귀하의 시간과 도움에 감사드립니다.

CanGoLeft CanGoBot

답변

1

당신의 논리는 몇 가지 점에서 깨진 것 같다. Rectangle의 방법 (OffsetIntersects)을 사용하여 대상 Rectangle이 콜리 드되는지 확인해야합니다. 나는 이것이 당신의 의도에 근접 생각 (distance를 가정하는 것은 당신이 플레이어 이동있어하는 값입니다) : .NET 프레임 워크의 다른 Rectangle 구조체가있다, 이것은 당신이 Microsoft.Xna.Framework.Rectangle를 사용하는 가정한다 (

public void RectangleInteraction(Rectangle anotherRectangle) 
{ 
    Rectangle down = playerRectangle; down.Offset(0, distance); 
    Rectangle up = playerRectangle; up.Offset(0, -distance); 
    Rectangle left = playerRectangle; left.Offset(-distance, 0); 
    Rectangle right = playerRectangle; right.Offset(distance, 0); 

    ConGoBot = !down.Intersects(anotherRectanlge); 
    ConGoTop = !up.Intersects(anotherRectanlge); 
    ConGoLeft = !left.Intersects(anotherRectanlge); 
    ConGoRight = !right.Intersects(anotherRectanlge); 
} 

, 하지만 질문에 이 표시되었습니다.