So in each frame I record the current coordinates - if there's a collision it resets the x and y. This mean if you held "right" while holding "up" you could not slide "up" along the tile because of the way the resetting works. I changed the code so it updates the X and Y separately, but now I still have an issue where my character cannot slide along a texture if it is collides with the top or bottom of the texture (side collisions work okay and the character can "slide" up or down the side of a texture).
Edit: I added 3 tags to this post but only c++ appears
int keyInputX(SDL_Rect playerarea, Input input) {
input.update();
int playerSpeed;
playerSpeed = 4;
if (input.keyDown(SDL_SCANCODE_RIGHT)) playerarea.x += playerSpeed;
if (input.keyDown(SDL_SCANCODE_LEFT)) playerarea.x -= playerSpeed;
return playerarea.x;
}
int keyInputY(SDL_Rect playerarea, Input input) {
input.update();
int playerSpeed;
playerSpeed = 4;
if (input.keyDown(SDL_SCANCODE_UP)) playerarea.y -= playerSpeed; // key input
if (input.keyDown(SDL_SCANCODE_DOWN)) playerarea.y += playerSpeed;
return playerarea.y;
}
void CheckPlayerCollision(SDL_Rect & player, std::list<Tile> & TileList, SDL_Rect & oldLocation) {
for (Tile& mytile : TileList) { // this is the collision code for the rock tiles
if (player.x + 40 > mytile.location.x && player.x - 100 < mytile.location.x) {
if (player.y + 100 > mytile.location.y && player.y - 76 < mytile.location.y) { // nest this for speed
player.x = oldLocation.x;
player.y = oldLocation.y;
}
}
}
}
main loop below:
originalLocation.x = area.x;
originalLocation.y = area.y;
area.x = keyInputX(area, input); // update player location X
CheckPlayerCollision(area, TileList, originalLocation);
area.y = keyInputY(area, input); // update player location Y
CheckPlayerCollision(area, TileList, originalLocation);