简体   繁体   English

libgdx在tiledmap图块上绘制矩形-碰撞检测

[英]libgdx drawing rectangles over tiledmap tiles - collision detection

I am creating a RPG game using the Libgdx library and I am having difficulty with setting up the rectangles for the collision detection. 我正在使用Libgdx库创建一个RPG游戏,但在设置用于碰撞检测的矩形时遇到了困难。 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 瓦尺寸为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;
    }

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. 您好,对不起我的英文,希望您能引导您,最后一个变量被阻止,重命名您在tile中设置的字符串设置图层模式,单击按钮右侧的矩形,属性赞助人,名称String您被阻止的名称,必须等于最终变量。

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 检查是否与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;
}

} }

在此处输入图片说明

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. 例如,在播放器的更新方法中,创建播放器在Y轴上移动直到发生碰撞为止的重力,希望对此有所解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM