C++ 检查移动线路是否堵塞
我已经编写了一个自上而下的小型射击程序,但有时双方的AI都会被困在分隔他们的墙上射击。我曾尝试编写一个函数来检查这一点,但最终得到了一些结果,即使您输入相同的内容,也会返回true或false 我试图检查它的方式是通过这个类C++ 检查移动线路是否堵塞,c++,collision-detection,sfml,C++,Collision Detection,Sfml,我已经编写了一个自上而下的小型射击程序,但有时双方的AI都会被困在分隔他们的墙上射击。我曾尝试编写一个函数来检查这一点,但最终得到了一些结果,即使您输入相同的内容,也会返回true或false 我试图检查它的方式是通过这个类 class Level{ public: sf::Sprite* background; sf::Texture* tex; sf::Image* im; std::vector<std:
class Level{
public:
sf::Sprite* background;
sf::Texture* tex;
sf::Image* im;
std::vector<std::reference_wrapper<sf::Color>> colCollisions;
int Width, Height;
Level(std::string name, int width, int height,std::vector<std::reference_wrapper<sf::Color>>col){
Width = width;
Height = height;
tex = new sf::Texture();
im = new sf::Image();
im->loadFromFile(name);
tex->loadFromImage(*im);
background = new sf::Sprite(*tex);
background->setTextureRect(sf::IntRect(0, 0, width, height));
colCollisions = col;
}
Level(std::string name, int width, int height){
Width = width;
Height = height;
tex = new sf::Texture();
im = new sf::Image();
im->loadFromFile(name);
tex->loadFromImage(*im);
background = new sf::Sprite(*tex);
background->setTextureRect(sf::IntRect(0, 0, width, height));
}
void modulate(sf::RenderWindow& window){
window.draw(*background);
}
bool check(sf::Sprite* s){
sf::Vector2f pos = s->getPosition();
if (pos.x<0 || pos.x>Width || pos.y<0 || pos.y>Height){
return true;
}
for (int i = 0; i < colCollisions.size(); i++){
if (colourCollides(pos.x,pos.y, colCollisions[i], im)){
return true;
}
}
return false;
}
bool check(int x, int y){
if (x<0 || x>Width || y<0 || y>Height){
return true;
}
for (int i = 0; i < colCollisions.size(); i++){
if (colourCollides(x,y, colCollisions[i], im)){
return true;
}
}
return false;
}
};
类级别{
公众:
sf::精灵*背景;
sf::纹理*tex;
sf::Image*im;
向量碰撞;
int宽度、高度;
级别(标准::字符串名称、整数宽度、整数高度、标准::矢量){
宽度=宽度;
高度=高度;
tex=新sf::Texture();
im=新sf::Image();
im->loadFromFile(名称);
tex->loadFromImage(*im);
背景=新sf::雪碧(*tex);
背景->setTextureRect(sf::IntRect(0,0,宽度,高度));
col=col;
}
级别(标准::字符串名称、整数宽度、整数高度){
宽度=宽度;
高度=高度;
tex=新sf::Texture();
im=新sf::Image();
im->loadFromFile(名称);
tex->loadFromImage(*im);
背景=新sf::雪碧(*tex);
背景->setTextureRect(sf::IntRect(0,0,宽度,高度));
}
无效调制(sf::渲染窗口和窗口){
窗口。绘制(*背景);
}
布尔检查(sf::Sprite*s){
向量2f pos=s->getPosition();
如果(位置X宽度| |位置Y高度){
返回true;
}
对于(int i=0;i
任何建议或建议都将不胜感激 将指针设为std::unique\u ptr,初始化成员初始值设定项列表中的成员,第二个构造函数应委托给第一个:
级别(std::string name,int-width,int-height):级别(name,width,height,{}){}
。不幸的是,这些都不能解决您的问题-对不起,代码不够(我想colorcollides()
可能是问题的根源)。除此之外,f(xWidth | | yHeight){
最有可能是f(x=Width | | y=Height){
,如果坐标是基于0的。if(pos.xWidth | | pos yHeight)
在边界框外返回true。可能是正确的,但违反直觉。