2013-04-10 114 views
0

嗨,只是一個簡單的問題。想知道如何實現與我設置的地圖圖塊的碰撞的最佳方式。我要爲玩家設置一個矩形,並檢查它是否與實體的矩形相交。我唯一的問題是,由於某種原因,如果我在頭文件中聲明我的矩形,它會給我一個錯誤。這意味着我不能檢查我的播放器是否與我的地圖切片相交,因爲我無法在播放器類中使用地圖類中的矩形,反之亦然。任何幫助將非常感激。SFML平臺與地圖平鋪碰撞

這裏是我的地圖類:

#include "Map.h" 
#include "Block.h" 
#include <sstream> 
using namespace std; 
Map::Map() 
{ 
    //map ctor; 
} 

Map::~Map() 
{ 
    // map dtor 
} 



sf::Shape rect = sf::Shape::Rectangle(0, 0, BLOCKSIZE, BLOCKSIZE, sf::Color(255, 255, 255, 255)); 
void Map::Initialise(const char *filename) 
{ 
    MapImage.LoadFromFile("Images/block.png"); 
    std::ifstream openfile(filename); 
    std::vector <int> tempvector; 
    std::string line; 

    while(std::getline(openfile, line)) 

    { 

     for(int i =0; i < line.length(); i++) 
     { 
      if(line[i] != ' ') // if the value is not a space 
      { 
       char value = line[i]; 
       tempvector.push_back(value - '0'); 
      } 

     } 
      mapVector.push_back(tempvector); // push back the value of the temp vector into the map vector 
      tempvector.clear(); // clear the temp vector readt for the next value 
    } 



} 

    void Map::DrawMap(sf::RenderWindow &Window) 
      { 
       sf::Color rectCol; 
       sf::Sprite sprite; 
       for(i = 0; i < mapVector.size(); i++) 
       { 
        for(j = 0; j < mapVector[i].size(); j++) 
        { 
         if(mapVector[i][j] == 0) 
          rectCol = sf::Color(44, 117, 255); 


         else if(mapVector[i][j] == 1) 
          rectCol = sf::Color(255, 100, 17); 


         rect.SetPosition(j * BLOCKSIZE, i * BLOCKSIZE); 
         rect.SetColor(rectCol); 
         Window.Draw(rect); 

        } 

       } 
      } 

回答

1

您可以創建另一個類,將處理地圖和玩家類別。 對於碰撞,我建議您使用AABB(軸對齊邊界框)並驗證每一幀碰撞。