C++ Tilemap冲突sfml c++;平板电脑
我的2d平台有问题。正如我刚开始用C++,我遇到麻烦瓷砖碰撞。我可以阻止玩家进入地砖,也可以远离地砖,但不知怎的,他不能沿着地砖移动 此功能用于检查新位置是否位于实心平铺内: 空地图::drawColMap(玩家和玩家){ } 感谢您的帮助 //编辑1 我试着记录碰撞,结果显示即使我不按任何键,玩家仍然在碰撞区域。但是我如何阻止玩家进入呢?我找不到问题 //编辑2 我想我发现了另一个问题。C++ Tilemap冲突sfml c++;平板电脑,c++,collision-detection,sfml,C++,Collision Detection,Sfml,我的2d平台有问题。正如我刚开始用C++,我遇到麻烦瓷砖碰撞。我可以阻止玩家进入地砖,也可以远离地砖,但不知怎的,他不能沿着地砖移动 此功能用于检查新位置是否位于实心平铺内: 空地图::drawColMap(玩家和玩家){ } 感谢您的帮助 //编辑1 我试着记录碰撞,结果显示即使我不按任何键,玩家仍然在碰撞区域。但是我如何阻止玩家进入呢?我找不到问题 //编辑2 我想我发现了另一个问题。 碰撞检查可能应在移动播放机之前和移动新位置之后运行。播放机碰撞后,更改位置,将其从碰撞区域(适当的方向)移
碰撞检查可能应在移动播放机之前和移动新位置之后运行。播放机碰撞后,更改位置,将其从碰撞区域(适当的方向)移动1个像素。如果角点未正确移出碰撞区域,则它可能仍在碰撞边界内
for (int i = 0; i < _player.tiles.size(); i++)
{
if (colMap[_player.tiles[i].y][_player.tiles[i].x] == 1) //solid tile = 1
{
_player.willCollide = true;
break;
}
else {
_player.willCollide = false;
}
}
sf::Vector2f newPosition;
sf::Vector2f oldPosition;
oldPosition.x = playerImage.getPosition().x; // store the current position
oldPosition.y = playerImage.getPosition().y;
newPosition.x = playerImage.getPosition().x; // store the new position
newPosition.y = playerImage.getPosition().y;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
source.y = Left; //sprite stuff
moving = true;
newPosition.x -= 2;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
source.y = Right;
moving = true;
newPosition.x += 2;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
source.y = Up;
moving = true;
newPosition.y -= 2;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
source.y = Down;
moving = true;
newPosition.y += 2;
}
if (!(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down)))
{
moving = false;
}
//create corners to check collision
bottom = newPosition.y + 32; //tile size is 32 px
left = newPosition.x;
right = newPosition.x + 32;
top = newPosition.y;
sf::Vector2i topLeft(sf::Vector2i((int)left / 32, (int)top / 32)); // get the corners of the new position
sf::Vector2i topRight(sf::Vector2i((int)right / 32, (int)top / 32));
sf::Vector2i bottomLeft(sf::Vector2i((int)left / 32, (int)bottom / 32));
sf::Vector2i bottomRight(sf::Vector2i((int)right / 32, (int)bottom / 32));
tiles.clear();
tiles.push_back(topLeft);
if (std::find(tiles.begin(), tiles.end(), topRight) == tiles.end()) tiles.push_back(topRight); //check the corners
if (std::find(tiles.begin(), tiles.end(), bottomLeft) == tiles.end()) tiles.push_back(bottomLeft);
if (std::find(tiles.begin(), tiles.end(), bottomRight) == tiles.end()) tiles.push_back(bottomRight);
//if no collision set the position to the new position
if (!willCollide)
playerImage.setPosition(newPosition);
else
playerImage.setPosition(oldPosition); //if collision then set the position to the previous position