2013-04-10 93 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(轴对齐边界框)并验证每一帧碰撞。