简体   繁体   English

Libgdx:Tiled Objects图层用于碰撞

[英]Libgdx: Tiled Objects layer for collision

Good day sirs, 美好的一天先生,

I'm trying to figure out now is how to perform a collision detection for my player by using a Tiled Object layer, to be more specific I wanted it to detect a polyline which I have draw in my tiled map. 我现在想弄清楚如何通过使用Tiled Object层对我的播放器执行碰撞检测,更具体地说,我希望它能够检测我在平铺地图中绘制的折线。 As I was researching in google about collision detection for Tiled, I found this simple example superkoala sample for TiledmapLayer . 当我在谷歌研究关于Tiled的碰撞检测时,我发现了TiledmapLayer的这个简单示例superkoala示例 In my understanding of the code (correct me if I'm wrong), It order to detect a collision the player will read each tile of a certain layer for example a foreground layer which contains the ground and other objects. 在我对代码的理解中(如果我错了,请纠正我),为了检测碰撞,玩家将读取某个图层的每个图块,例如包含地面和其他对象的前景图层。

Basically, What I did with my Object layer is I named my polyline to Wall and for the type I put in numbers depending on how many polylines I have used. 基本上,我对我的对象层所做的是将我的折线命名为Wall,并根据我使用的折线数量输入数字。 So that for future use I could call this wall numbers for my collision. 因此,为了将来使用,我可以将此墙数称为我的碰撞。

Can anyone give me a sample code about reading objects and using them for collision detection?or just a simple tip or Idea on how I could solve this?any help would be appreciated. 任何人都可以给我一个关于阅读对象和使用它们进行碰撞检测的示例代码吗?或者只是一个简单的提示或想法如何解决这个问题?任何帮助将不胜感激。

What i recommend on doing in your case is not to use object layers but properties for the specific tiles. 我建议你做的是不使用对象图层,而是使用特定图块的属性。 Let's say you have one tile in 'Tiled' and you are sure to only use it as a wall. 假设你在'Tiled'中有一块瓷砖,你肯定只能用它作墙。 Click on that specific tile and add the property "blocked". 单击该特定磁贴并添加属性“已阻止”。 Then, in your code, you can check every cell next to the player and if it contains the property "blocked", not allow the player to go in that direction. 然后,在您的代码中,您可以检查播放器旁边的每个单元格,如果它包含“已阻止”属性,则不允许玩家朝那个方向前进。 Here is a pseudo-code on how I did it: 这是我如何做到的伪代码:

private boolean isCellBlocked(float x, float y) {
    Cell cell = null;
    boolean blocked = false;

    try {
        cell = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (cell != null && cell.getTile() != null) {
        if (cell.getTile().getProperties().containsKey("blocked")) {
            blocked = true;
        }
    }
    return blocked;
}

and you can make the player call isCellBlocked(x, y) every time he updates. 并且你可以在每次更新时让玩家调用isCellBlocked(x, y)

I also did a collidesSide(float x, float y) for every side ( collidesTop(float x, float y) , etc). 我还为每一方做了一个collidesSide(float x, float y)collidesTop(float x, float y)等)。

I hope this helps you out, even if it doesn't use the system you wanted to. 我希望这可以帮助你,即使它没有使用你想要的系统。 For any more help on this, I'm heavily using libGDX for my collision detection on a big project, so don't hesitate to contact me! 对于这方面的任何帮助,我在一个大项目中大量使用libGDX进行碰撞检测,所以不要犹豫与我联系!

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

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