How do I detect and handle collisions using a tile property with Slick2D?
Posted
by
oracleCreeper
on Game Development
See other posts from Game Development
or by oracleCreeper
Published on 2013-07-17T17:03:12Z
Indexed on
2013/10/17
22:23 UTC
Read the original article
Hit count: 436
I am trying to set up collision detection in Slick2D based on a tilemap. I currently have two layers on the maps I'm using, a background layer, and a collision layer. The collision layer has a tile with a 'blocked' property, painted over the areas the player can't walk on. I have looked through the Slick documentation, but do not understand how to read a tile property and use it as a flag for collision detection.
My method of 'moving' the player is somewhat different, and might affect how collisions are handled. Instead of updating the player's location on the window, the player always stays in the same spot, updating the x and y the map is rendered at. I am working on collisions with objects by restricting the player's movement when its hitbox intersects an object's hitbox. The code for the player hitting the right side of an object, for example, would look like this:
if(Player.bounds.intersects(object.bounds)&&(Player.x<=(object.x+object.width+0.5))&&Player.isMovingLeft){
isInCollision=true;
level.moveMapRight();
}
else if(Player.bounds.intersects(object.bounds)&&(Player.x<=(object.x+object.width+0.5))&&Player.isMovingRight){
isInCollision=true;
level.moveMapRight();
}
else if(Player.bounds.intersects(object.bounds)&&(Player.x<=(object.x+object.width+0.5))&&Player.isMovingUp){
isInCollision=true;
level.moveMapRight();
}
else if(Player.bounds.intersects(object.bounds)&&(Player.x<=(object.x+object.width+0.5))&&Player.isMovingDown){
isInCollision=true;
level.moveMapRight();
}
and in the level's update code:
if(!Player.isInCollision)
Player.manageMovementInput(map, i);
However, this method still has some errors. For example, when hitting the object from the right, the player will move up and to the left, clipping through the object and becoming stuck inside its hitbox. If there is a more effective way of handling this, any advice would be greatly appreciated.
© Game Development or respective owner