Search code examples
javacollisionslick2d

How to prevent the player from entering "blocked tiles" - java


I'm using Slick2d to create a game in java it i have run into a problem. I have no idea how to prevent the user from entering "blocked tiles". Does anyone have any tips on how to do this?

Edit: It's not the actual checking if there is a collision that's a problem, it's stopping the player from moving, i have one speed variable of the type float.


Solution

  • There are two basic methods for that:

    • Pre-processing: Upon receiving the move command, check if the tile the player is moving to is blocked or not, and prevent the user from moving into blocked tiles. (I recommend this method)

    or

    • Post-processing: Move the player instantly, detect the current tile's state, and move the player back if needed.

    The same principles apply for non-tiled games: Check if there is a collision, either before or after the actual movement, and then do the appropriate actions.