c++ - SFML 平台游戏与 map 图 block 发生碰撞

标签 c++ sfml

你好,只是一个简单的问题。想知道与我设置的 map 图 block 实现碰撞的最佳方法是什么。我打算为播放器设置一个矩形并检查它是否与实心矩形相交。我唯一的问题是,出于某种原因,如果我在头文件中声明我的矩形,它会给我一个错误。这意味着我无法检查我的播放器是否与我的 map 图 block 相交,因为我不能在播放器类中使用我的 map 类中的矩形,反之亦然。任何帮助将不胜感激。

这是我的 map 类:

#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);

                    }

                }
            }

最佳答案

您可以创建另一个类来处理 map 和玩家类。 对于碰撞,我建议您使用 AABB(轴对齐边界框)并验证每一帧碰撞。

关于c++ - SFML 平台游戏与 map 图 block 发生碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920062/

相关文章:

c++ - 我怎样才能让我的 va_list 参数自己重复?

c++ - 在 GPU 上计算积分图像真的比在 CPU 上更快吗?

c++ - C++ 中的多维数组值到字符串

c++ - Boost Graph Library astar和导航网格

c++ - 将一个旋转的矩形拆分成更小的矩形,如何旋转它们以保持原来的大矩形?

c++ - 使用getline从输入文件打印时如何忽略空行

python - Swig 和指针问题(python)

c++ - 如何使用 C++ 在 SFML 中正确生成随机地形

c++ - 2D 等距 - SFML - 正确的公式,错误的坐标范围

c++ - extern int main(int argc, char* argv[]) 使用?