2012-01-25 86 views
1

我想創建一個pacman遊戲來學習XNA,但是我碰到了一些問題以使碰撞檢測工作。遊戲是基於瓦片的,其中1是牆壁,0是可步行的。 然後,它會將您正在站立的拼圖加上其周圍的4個拼圖,並且如果它與其中一個碰撞並且拼塊值不爲0,它會將位置重置爲移動之前的位置。由於某種原因,但它不起作用,它會隨機卡住,有時我甚至可以移動牆壁。 enter image description herePacman碰撞檢測

這裏是我的碰撞檢測:

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; 

任何一個能看到爲什麼我的碰撞檢測是不工作?

編輯:我已經上傳整個項目http://sogaard.us/Pacman.zip

+0

我不會允許它移動和移回來 - 我會阻止它移動在第一個地方 - 方式更有效,可能會提供更好。 – niico

回答

1

我不知道這是否是引起充分的問題。但是,在你的第三個瓷磚檢查(你在哪裏檢查瓷磚),你再次檢查瓷磚。

在這裏,這部分:

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

在下一行你仍然有這個PARAMS爲testRect內:

((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以TileSize Y座標?

我不知道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

在邏輯世界座標(瓦片索引)中工作會更好,而不是屏幕座標(瓦片大小)用於碰撞檢測。可以根據瓦片計算pacman的位置指數? – MattDavey

+0

@DoomStone:這很有道理。在這種情況下,我不會立即看到錯誤。 – blubb