C++ SFML平台与贴图分幅的碰撞
嗨,只是一个简单的问题。想知道我设置的贴图块实现冲突的最佳方法是什么。我打算为播放器设置一个矩形,并检查它是否与实心矩形相交。我唯一的问题是,由于某种原因,如果我在头文件中声明矩形,它会给我一个错误。这意味着我无法检查我的玩家是否与我的地图平铺相交,因为我无法在玩家类中使用我的地图类中的矩形,反之亦然。任何帮助都将不胜感激 这是我的地图课:C++ SFML平台与贴图分幅的碰撞,c++,sfml,C++,Sfml,嗨,只是一个简单的问题。想知道我设置的贴图块实现冲突的最佳方法是什么。我打算为播放器设置一个矩形,并检查它是否与实心矩形相交。我唯一的问题是,由于某种原因,如果我在头文件中声明矩形,它会给我一个错误。这意味着我无法检查我的玩家是否与我的地图平铺相交,因为我无法在玩家类中使用我的地图类中的矩形,反之亦然。任何帮助都将不胜感激 这是我的地图课: #include "Map.h" #include "Block.h" #include <sstream> using namespace s
#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.h”
#包括“Block.h”
#包括
使用名称空间std;
Map::Map()
{
//地图绘制者;
}
地图::~Map()
{
//地图数据记录器
}
sf::Shape rect=sf::Shape::Rectangle(0,0,BLOCKSIZE,BLOCKSIZE,sf::Color(255,255,255,255));
无效映射::初始化(常量字符*文件名)
{
LoadFromFile(“Images/block.png”);
std::ifstream openfile(文件名);
std::向量tempvector;
std::字符串行;
while(std::getline(openfile,line))
{
对于(int i=0;i
您可以创建另一个类来处理地图和玩家类。
对于碰撞,我建议您使用AABB(轴对齐边界框)并验证碰撞的每个帧