0
\$\begingroup\$

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?

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

I have this problem before and for me, the best solution (at least for my 2D game) was to calculate where the player is going to be before moving, and if the location is not valid just don't move the player. This will prevent lot of issues even with spam, as you directly won't move the player.

As this is done before moving the player you can tweak how it's done. For example, you could stop completely the player if it's going to be a collision, or calculate the angle of the collision and only stop the x or y value of the player (so the player will slide around), or you can bounce the player back (from a valid position to another valid position, without entering the invalid one).

Calculating the direct future position of the player should not affect performance.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.