Hello,
I'm working on collision detection when a sprite hits a wall.
In my Zone.cpp class, where tilemaps are handled I have this block in the update function:
for (auto& tileMap : renderAfterPlayerTM) {
tileMap->tileMap->update(player, &movement, &this->frameItem, &zoneInfo, deltaTime);
playerUpdate = movement.ghostMove(deltaTime, currentPlayerPosition, *tileMap->tileMap->getTilesGroupVector());
}
player->update(deltaTime, playerUpdate.newPosition, playerUpdate.playerPosition);
for (auto& tileMap : renderBeforePlayerTM) {
tileMap->tileMap->update(player, &movement, &this->frameItem, &zoneInfo, deltaTime);
playerUpdate = movement.ghostMove(deltaTime, currentPlayerPosition, *tileMap->tileMap->getTilesGroupVector());
}
player->update(deltaTime, playerUpdate.newPosition, playerUpdate.playerPosition);
I have multiple tilemaps that render different type of objects, like things that should render/update before and after the player is.
The ghostMove function is where the sprites new position is formed:
MovePlayerResponse Movement::ghostMove(sf::Time deltaTime, sf::Vector2f playerPosition, std::vector<TileGroup*> tileGroupsVector) {
MovePlayerResponse mpr;
AnimatedSprite ghost;
ghost.setPosition(playerPosition);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
ghost.move(sf::Vector2f((-1 * speed), 0.0f));
LL::MapUpdateResponseObject collideResponse = LL::Collision::collide(ghost.getPosition(), tileGroupsVector);
mpr.playerPosition = LL::PlayerPosition::Left;
if (collideResponse.collided) {
mpr.newPosition = sf::Vector2f(speed, 0.0f);
mpr.tileNumber = collideResponse.tileNumber;
return mpr;
}
mpr.newPosition = sf::Vector2f((-1 * speed), 0.0f);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
ghost.move(sf::Vector2f((speed), 0.0f));
LL::MapUpdateResponseObject collideResponse = LL::Collision::collide(sf::Vector2f(ghost.getPosition().x, ghost.getPosition().y), tileGroupsVector);
mpr.playerPosition = LL::PlayerPosition::Right;
if (collideResponse.collided) {
mpr.newPosition = sf::Vector2f(-1 * (speed), 0.0f);
mpr.tileNumber = collideResponse.tileNumber;
return mpr;
}
mpr.newPosition = sf::Vector2f((speed), 0.0f);
}
mpr.tileNumber = -1;
return mpr;
}
On every movement, I move the position to the desired position and if a collision occurs I move it back. The problem occurs when I hold down S (which goes down on the screen) and the sprite continues to collide with the wall, and if I were to press D the sprite would move as if i'm pressing A because it's stuck in that collide block or the sprite would just not move at all, or continuously bounce off and on the wall.
Image for an idea of when this occurs, collision on a wall:
Any ideas on why this behavior occurs, or if I have some fundamental flaw in my design?