2d 플랫폼에 문제가 있습니다. 나는 C++로 시작 했으므로 타일 충돌에 문제가있다. 나는 플레이어가 타일에 들어가는 것을 막을 수 있고 그것으로부터 멀리 이동할 수는 있지만 어떻게 든 그는 타일을 따라 움직일 수 없다.Tilemap collision sfml C++ platformer
이여기 {
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;
}
}
}
그리고 무효지도 :: drawColMap (플레이어 & _player) 일 : 새로운 위치가 단단한 타일 내에 있을지
이
는 확인하기위한 기능입니다 플레이어 이동 코드 :void Player :: update() {
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
은}
은 어떤 도움에 감사드립니다!
// 편집 한
나는 충돌을 기록 시도하고 플레이어가 아무것도를 누르면 해달라고 경우에도 충돌 지역에서 여전히 있다고 말한다. 하지만 어떻게 플레이어가 들어오는 것을 막을 수 있습니까? 나는 문제를 찾을 수 없다.
// 편집 2
다른 문제를 발견했습니다. 충돌 체크는 플레이어를 이동하기 직전과 새 위치를 이동 한 직후에 실행해야합니다.