# libgdx - Problem occurring after collision detected

I have just implemented collision detection in my little 2d rpg-like game. I'm using Tiled for the maps, mechanism is simple - I have one layer called 'collision' in my tiled map, and while loading this map in game, I'm adding Rectangle objects to the tiles in 'collision' layer. Therefore, when player's rectangle overlaps tile's rectangle, collision is detected. My problem is - what proper action should be taken after this detection? Note - my player object has field like (float) movementSpeed, (Vector2) position. By pressing arrow keys player moves in a particular direction, algorithm looks like: Keys.RIGHT clicked ---> player.position.x += movementSpeed; Keys.DOWN clicked ---> player.position.y -= movementSpeed; ...and so on. The REAL problem begins here - after collision has been detected, I'm adjusting player's position, just like in code below.

public void adjustPosition() {
switch (currentMove) {
case UP:
position.y -= movementSpeed;
rectangle.set(position.x, position.y, playerWidth, playerHeight);
break;
case DOWN:
position.y += movementSpeed;
rectangle.set(position.x, position.y, playerWidth, playerHeight);
break;
case LEFT:
position.x += movementSpeed;
rectangle.set(position.x, position.y, playerWidth, playerHeight);
break;
case RIGHT:
position.x -= movementSpeed;
rectangle.set(position.x, position.y, playerWidth, playerHeight);
break;
}
}


But it doesn't work as expected - after spamming arrow keys in different directions near the collision tile, player somehow is put onto the collision tile or is going just through it (and player definitely shouldn't be able to do so). My question is - what should I do after collision has been detected in such a situation?

• Have you taken a look through previous 2D rectangle questions in the collision-resolution tag? You may find some useful starting points there. – DMGregory Aug 26 '18 at 12:54