0
votes

I am creating a RPG game using the Libgdx library and I am having difficulty with setting up the rectangles for the collision detection. I was able to draw a rectangle around my main character and I know the code for how to handle the collision when it happens. The problem is that I need to draw rectangles around the tiles in my collision layer(my collision layer is not a object layer). If anyone is able to give an example of how to write this or even point me in the right direction, this would be very helpfull.

tile size is 32x32

here is the code that I have come up with so far for drawing the rectangles around the specified tiles

public void collisionSetUp(){
    TiledMapTileLayer layer;
    layer = (TiledMapTileLayer) map.getLayers().get("Bushes");
    float tileWidth = layer.getTileWidth(); //the tile is a perfect square so only one side is required


    //go through the entire layer and assign a rectangle to each tile
    for(int row = 0; row < layer.getHeight(); row++) {
        for(int col = 0; col < layer.getWidth(); col++) {

            debugRenderer.begin(ShapeType.Line);
            collisonRect = new Rectangle(row*32, col*32, 32, 32);
            debugRenderer.setColor(new Color(0, 1, 0, 1));
            debugRenderer.rect(collisonRect.getX(), collisonRect.getY(), collisonRect.width, collisonRect.height);
            debugRenderer.end();
            System.out.println("new Rectangle");
        }
    }

}

code for handling the collision

Rectangle playerRect = new Rectangle(player.position.x, player.position.y, 32, 32);
    debugRenderer.setColor(new Color(0, 1, 0, 1));
    debugRenderer.rect(player.position.x, player.position.y, playerRect.width, playerRect.height);
    debugRenderer.end();


    if(playerRect.overlaps(collisonRect)){
        player.position.x = player.prePosition.x;
        player.position.y = player.prePosition.y;
    }
1
Is the movement fixed to the tiles or freely? Both ways you can just calculate if the player enters a impassable tile, if so you restrict that movement. It is slightly more complicated to implement this if the player moves freely over the tiles.Madmenyo

1 Answers

0
votes

Hello sorry for my English, I hope this guide you, the final variable blocked, the rename the string you have in the tilesSet the layer patterns, click on button right propiedes, attributes patron, name String your blocked name , must be equal to the final variable.

public class pruebasActor extends Sprite{

TiledMapTileLayer collisionLayer;
Rectangle rectangle;

//tu string en las propiedades del tilesmap
//your string in the properties of tilesmap
private final String BLOCKEDKEY = "blockedKey";

private float yourIncrementW;
private float yourIncrementH;


pruebasActor(TiledMapTileLayer collisionLayer){
    //YOUR OTHER CODE ETC
    this.collisionLayer = collisionLayer;
    rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());

    yourIncrementW = collisionLayer.getTileWidth();
    yourIncrementH = collisionLayer.getTileHeight();
}
//YOUR OTHER CODE UPDATE DRAW MOVEMET

Check that the cell this or unlocked

private boolean isCellBlocked(float x, float y) {

    Cell celda = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));

    return celda != null && celda.getTile() 
                 != null && celda.getTile().getProperties().containsKey(BLOCKEDKEY);
}

Check if collides with Right

public boolean collidesRight() {

for(float step = 0; step <= getHeight(); step += yourIncrementH){

    if(isCellBlocked(getX() + getWidth(), getY() + step)){
        Gdx.app.log("collidesRight", "X"+ getX() + getWidth()+"Y"+getY() + step);
        return true;
    }
}       
return false;
}

Check if collides with Left

public boolean collidesLeft() {

for(float step = 0; step <= getHeight(); step += yourIncrementH)
    if(isCellBlocked(getX(), getY() + step)){
        Gdx.app.log("collidesRight", "X"+ getX()+"Y"+ getY() + step);
        return true;
    }
return false;
}

Check if collides with Top

public boolean collidesTop() {

for(float step = 0; step <= getWidth(); step += yourIncrementW)
    if(isCellBlocked(getX() + step, getY() + getHeight())){
        Gdx.app.log("collidesTop", "X"+getX() + step+"Y"+getY() + getHeight());
        return true;
    }
return false;

}

Check if collides with Bottom

public boolean collidesBottom() {

for(float step = 0; step <= getWidth(); step += yourIncrementW)
    if(isCellBlocked(getX() + step, getY())){
        Gdx.app.log("collidesBottom", "X"+getX() + step+"Y"+getY());
        return true;
    }
return false;
}

}

enter image description here

Now with the above methods, you can call to determine the behavior of the player. for example in the update method of the player, create the gravity that the player moves in the Y axis until it collides for example, hope to have explained.