2011-10-12 14 views
0

所以我正在製作一個非常簡單的程序,我希望一個精靈與另一個精靈發生碰撞。 我通過使用二維數組創建一個包含圖像的網格來製作'層次'。 我將如何實現簡單的碰撞?我會很感激代碼示例,所以我實際上可以看到 發生了什麼事情。謝謝。這裏的代碼:與2維數組中的.bmp的簡單衝突?

int pacmanPosX = 32; 
int pacmanPosY = 32; 

Sprite pacman (new Surface("assets/tiles/pacman.png"), 1); 

Surface* tileSet[2]; 
int landTile[14][16] = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}, 
         {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,}, 
         {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,}, 
         {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,}, 
         {1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1,}, 
         {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,}, 
         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}}; 


void Game::Init() 
{ 
    // put your initialization code here; will be executed once 
    tileSet[0] = new Surface("assets/tiles/void.bmp"); // Sets up data for tileSet 
    tileSet[1] = new Surface("assets/tiles/wall.bmp"); 

} 

void Game::Tick(float a_DT) 
{ 
     m_Screen->Clear(0); 

    for (int indexX = 0; indexX <= 14; indexX++) 
    { 
     for (int indexY = 0; indexY <= 16; indexY++) 
     { 
      int tile = landTile[indexY][indexX]; 
      tileSet[tile]->CopyTo(m_Screen, indexX * 32, indexY * 32); 
     } 
    } 

    pacman.Draw(pacmanPosX, pacmanPosY, m_Screen); 



    Sleep(10); 

int colPosX = pacmanPosX /32; 
int colPosY = (pacmanPosY-1)/32; 

bool collision = (bool)landTile[colPosX][colPosY]; 


int direction; 

if(collision = false) 
{ 
    if (GetAsyncKeyState(VK_UP)) pacmanPosY -=2; 
    if (GetAsyncKeyState(VK_DOWN)) pacmanPosY += 2; 
    if (GetAsyncKeyState(VK_RIGHT)) pacmanPosX += 2; 
    if (GetAsyncKeyState(VK_LEFT)) pacmanPosX -= 2; 
} 


} 

這是行不通的,現在角色根本無法移動。爲了您的信息,每個精靈都是 32x32,包括字符。

回答

1

如果通過將pacmanPosX和pacmanPosY除以每個正方形的大小(32)計算pacman對應於landTitle數組的位置,然後將其轉換爲int。那麼你可以簡單地檢查在你的前面的位置是否設置爲1或0,並以這種方式,你可以得到一個非常簡單和快速的碰撞。

告訴我,如果你遇到任何阻礙

編輯

我的意思是,你解釋,所有的牆壁應該在你的地圖陣列。因此,您不必進行任何精靈碰撞,而是可以根據您應該在陣列中的位置進行簡單的碰撞檢查。讓我們考慮,你應該的情況下向上移動上圖:

int colPosX = pacmanPosX /32; 
int colPosY = (pacmanPosY-1)/32; 

bool collision = (bool)landTitle[colPosX][colPosY]; 

如果碰撞是真的,那麼你從你的位置有衝突的一個像素向上,那是什麼在計算中-1colPosY是。

編輯2

我是否應該修改代碼,然後我會做更多這樣的

int blockSize = 32; 
int pacmanPosX = blockSize * 1; 
int pacmanPosY = blockSize * 1; 

Sprite pacman (new Surface("assets/tiles/pacman.png"), 1); 

Surface* tileSet[2]; 
int landTile[14][16] = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}, 
         {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,}, 
         {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,}, 
         {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,}, 
         {1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,}, 
         {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1,}, 
         {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,}, 
         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}}; 


void Game::Init() 
{ 
    // put your initialization code here; will be executed once 
    tileSet[0] = new Surface("assets/tiles/void.bmp"); // Sets up data for tileSet 
    tileSet[1] = new Surface("assets/tiles/wall.bmp"); 

} 

void Game::Tick(float a_DT) 
{ 
     m_Screen->Clear(0); 

    for (int indexX = 0; indexX <= 14; indexX++) 
    { 
     for (int indexY = 0; indexY <= 16; indexY++) 
     { 
      int tile = landTile[indexY][indexX]; 
      tileSet[tile]->CopyTo(m_Screen, indexX * blockSize, indexY * blockSize); 
     } 
    } 

    if (GetAsyncKeyState(VK_UP) && CheckCollision(0,-1)) pacmanPosY -=2; 
    if (GetAsyncKeyState(VK_DOWN) && CheckCollision(0, 1)) pacmanPosY += 2; 
    if (GetAsyncKeyState(VK_RIGHT) && CheckCollision(1, 0)) pacmanPosX += 2; 
    if (GetAsyncKeyState(VK_LEFT) && CheckCollision(-1,0)) pacmanPosX -= 2; 

    Sleep(10); 
} 

bool Game::CheckCollision(int dirX, int dirY){ 
    return (bool)landTile[(pacmanPosX+dirX)/blockSize][(pacmanPosY+dirY)/blockSize]; 
} 
+0

對不起,我不明白。我能得到的唯一的東西是:bool collision = false; int pacmanposition =(pacmanPosX/32)+(pacmanPosX/32); if(pacmanposition = landTile [1])//這是應該是實體的圖塊。 { collision = true; } 我甚至不會嘗試。我只是不知道自己在這裏做什麼,對我來說這太抽象了,不知何故。我從來沒有料到這比指針和類以及我所有的更難。 – Turbosheep

+0

我會在答案中進行編輯,以便進一步解釋 – Udrian

+0

對不起,如果我脫落密密麻麻,但爲什麼除以32部分?我沒有得到最後的...... =(布爾)landtitle [colPosx] [colPosY]行。我從來沒有見過像這樣的數組之前使用大括號的布爾。現在這非常令人沮喪,這應該不是那麼難。我仍然沒有得到任何這一點。哦,檢查我的問題的結束......在你的代碼中似乎有一個邏輯錯誤。 – Turbosheep