2012-01-25 4 views
1

XNA를 배우기 위해 팩맨 게임을 만들려고하고 있지만 충돌 감지 기능을 작동시키는 데 문제가 있습니다. 게임은 타일 기반이며, 1은 벽이고 0은 걷기가 가능합니다. 그러면 서있는 타일과 주변의 타일 4가 걸리고, 그 중 하나와 충돌하고 타일 값이 0이 아닌 경우 이동 전의 위치로 재설정됩니다. 그러나 어떤 이유로 그것은 단지 작동하지 않습니다, 그것은 무작위로 붙어 얻고 때로는 심지어 벽을 움직일 수 있습니다. 내 충돌 감지가 작동하지 않는 이유팩맨 충돌 감지

var oldPos = Position; 
    // Updates the Position 
    base.Update(theGameTime, mSpeed, mDirection); 

    // Test Collidetion 
    Rectangle objRect = new Rectangle((int)Position.X, (int)Position.Y, 32, 32); 
    bool isCollided = false; 
    Vector2 curTitle = GetCurrentTitle(); 

    // Test UP, DOWN, LEFT, RIGHT 
    int tile; 
    Rectangle testRect; 

    if ((int)curTitle.Y < 0 || (int)curTitle.X < 0 || (int)curTitle.Y >= map.MapSizeWidth - 1 || (int)curTitle.X >= map.MapSizeHeight - 1) 
     isCollided = true; 

    if (!isCollided) 
    { 
     tile = map.Tiles[(int)curTitle.Y, (int)curTitle.X]; 
     testRect = new Rectangle(((int)curTitle.X) * map.TileSize, ((int)curTitle.Y) * map.TileSize, map.TileSize, map.TileSize); 
     if (tile != 0 && rectangle_collision(testRect, objRect)) 
      isCollided = true; 

     if (curTitle.Y != 0) 
     { 
      tile = map.Tiles[(int)curTitle.Y - 1, (int)curTitle.X]; 
      testRect = new Rectangle(((int)curTitle.X) * map.TileSize, ((int)curTitle.Y - 1) * map.TileSize, map.TileSize, map.TileSize); 
      if (tile != 0 && rectangle_collision(testRect, objRect)) 
       isCollided = true; 
     } 

     tile = map.Tiles[(int)curTitle.Y + 1, (int)curTitle.X]; 
     testRect = new Rectangle(((int)curTitle.X) * map.TileSize, ((int)curTitle.Y - 1) * map.TileSize, map.TileSize, map.TileSize); 
     if (tile != 0 && rectangle_collision(testRect, objRect)) 
      isCollided = true; 

     if (curTitle.X != 0) 
     { 
      tile = map.Tiles[(int)curTitle.Y, (int)curTitle.X - 1]; 
      testRect = new Rectangle(((int)curTitle.X - 1) * map.TileSize, ((int)curTitle.Y) * map.TileSize, map.TileSize, map.TileSize); 
      if (tile != 0 && rectangle_collision(testRect, objRect)) 
       isCollided = true; 
     } 

     tile = map.Tiles[(int)curTitle.Y, (int)curTitle.X + 1]; 
     testRect = new Rectangle(((int)curTitle.X + 1) * map.TileSize, ((int)curTitle.Y) * map.TileSize, map.TileSize, map.TileSize); 
     if (tile != 0 && rectangle_collision(testRect, objRect)) 
      isCollided = true; 
    } 
    if (isCollided) 
     Position = oldPos; 

가 하나라도 볼 수 있습니다 여기에

enter image description here 내 충돌 감지입니까?

편집 : 내가 업로드 한 http://sogaard.us/Pacman.zip

+0

나는 그것을 뒤로 움직이게하지 않을 것입니다. 나는 그것을 더 효율적으로하고 더 나은 방법으로 움직이는 것을 멈출 것입니다. – niico

답변

1

이것이 전체적인 문제를 일으키는 지 확실하지 않습니다. 그러나 세 번째 타일 검사 (아래 타일을 확인)에서 위의 타일을 다시 확인합니다. 여기

이 부분 :

tile = map.Tiles[(int)curTitle.Y + 1, (int)curTitle.X]; 

다음 줄에 당신이 여전히 testRect에 대한 PARAMS 안에이 있습니다

((int)curTitle.Y - 1) * map.TileSize 

은 다음과 같아야합니다

((int)curTitle.Y + 1) * map.TileSize 

전체 수정 조각 :

tile = map.Tiles[(int)curTitle.Y + 1, (int)curTitle.X]; 
    testRect = new Rectangle(((int)curTitle.X) * map.TileSize, ((int)curTitle.Y + 1) * map.TileSize, map.TileSize, map.TileSize); 
    if (tile != 0 && rectangle_collision(testRect, objRect)) 
     isCollided = true; 

도움이 되길 바랍니다.

0

이 줄은 나에게 이상한 모양으로 전체 프로젝트 :

testRect = new Rectangle(
    ((int)curTitle.X) * map.TileSize, 
    ((int)curTitle.Y) * map.TileSize, 
    map.TileSize, 
    map.TileSize); 

왜 X를 곱 Y는 TileSize와 협력 하는가?

Rectangle의 매개 변수가 의미하는 바를 모르겠지만 처음 두 개는 위치이고 나머지 두 개는 너비와 높이라고 가정합니다. 당신이 쓸 줄 알았는데

testRect = new Rectangle(
    ((int)curTitle.X), 
    ((int)curTitle.Y), 
    map.TileSize, 
    map.TileSize); 
+0

curTitle.X는 제목 번호이지만 각 제목은 map.TileSize * map.TileSize이므로 제목 3,2의 시작 위치가 (3 * map)이기 때문에 내 제목 크기를 다중화 할 수있는 이유입니다. TileSize), (2 * map.TileSize) – Androme

+0

충돌 감지를 위해 화면 좌표 (타일 크기)보다는 논리적 인 세계 좌표 (타일 인덱스)로 작업하는 것이 좋습니다. 타일의 측면에서 팩맨의 위치를 ​​계산할 수 있습니까? 색인? – MattDavey

+0

@DoomStone : 그건 의미가 있습니다. 이 경우에는 오류가 즉시 표시되지 않습니다. – blubb