3

こんにちは。

私は今、タイル オブジェクト レイヤーを使用してプレーヤーの衝突検出を実行する方法を理解しようとしています。より具体的には、タイル マップに描画したポリラインを検出する必要がありました。Tiled の衝突検出について Google で調査していたときに、TiledmapLayer のこの簡単な例の superkoala サンプルを見つけました。私のコードの理解では(間違っている場合は修正してください)、衝突を検出するために、プレーヤーは特定のレイヤーの各タイルを読み取ります。たとえば、地面やその他のオブジェクトを含む前景レイヤーです。

基本的に、オブジェクトレイヤーで行ったことは、ポリラインに壁という名前を付け、使用したポリラインの数に応じて番号を付けたタイプです。将来の使用のために、衝突のためにこの壁番号を呼び出すことができます。

オブジェクトを読み取って衝突検出に使用するためのサンプル コードを教えてもらえますか?または、これを解決する方法に関する簡単なヒントやアイデアを教えてください。

4

2 に答える 2

0

この質問/回答に出くわし、私が使用したものを共有したかったのですが、まだすべてを正確に把握していませんが、うまくいきました:

    void buildMap() {
        MapLayer collisionLayer = (MapLayer) map.layers.find { it.name == 'collision' }
        collisionLayer.objects.each { MapObject mapObject ->
            Body body = null

            if(mapObject instanceof RectangleMapObject) {
                Rectangle rectangle = adjustRectangleDimensions(mapObject.rectangle)
                body = bodyFactory.makeBoxPolyBody(
                        rectangle.x,
                        rectangle.y,
                        rectangle.width,
                        rectangle.height,
                        BodyFactory.STONE,
                        BodyDef.BodyType.StaticBody
                )
            }

            if(mapObject instanceof PolygonMapObject) {
                Polygon polygon = adjustPolygonDimensions(mapObject.polygon)
                body = bodyFactory.makePolygonShapeBody(
                        polygon.vertices,
                        polygon.x,
                        polygon.y,
                        BodyFactory.STONE,
                        BodyDef.BodyType.StaticBody
                )
            }


            body.fixtureList.first().filterData.categoryBits = GROUND_BIT
            body.fixtureList.first().filterData.maskBits = PLAYER_BIT // can combine with | if multiple i.e. GROUND_BIT | PLAYER_BIT
        }
    }

    static Rectangle adjustRectangleDimensions(Rectangle rectangle) {
        rectangle.x = rectangle.x * RenderingSystem.PIXELS_TO_METRES * 2 as float
        rectangle.y = rectangle.y * RenderingSystem.PIXELS_TO_METRES * 2 as float
        rectangle.width = rectangle.width * RenderingSystem.PIXELS_TO_METRES * 2 as float
        rectangle.height = rectangle.height * RenderingSystem.PIXELS_TO_METRES * 2 as float
        return rectangle
    }

    static Polygon adjustPolygonDimensions(Polygon polygon) {
        float x = polygon.x * RenderingSystem.PIXELS_TO_METRES as float
        float y = polygon.y * RenderingSystem.PIXELS_TO_METRES as float
        polygon.setPosition(x, y)

        float[] vertices = polygon.vertices //might need to get transformedVertices at some point, seems to work now
        def adjustedVertices = []
        vertices.each {
            adjustedVertices.add(it * RenderingSystem.PIXELS_TO_METRES as float)
        }

        polygon.vertices = adjustedVertices

        return polygon
    }

この場合RenderingSystem.PIXELS_TO_METRES、OrthogonalTiledMapRenderer に使用した単位スケールです。

私が使用した bodyFactory はhttps://github.com/lifeweaver/learningGames/blob/master/core/src/net/stardecimal/game/BodyFactory.groovyにあります。

別の例については、 https://github.com/yichen0831/Pacman_libGdx/blob/master/core/src/com/ychstudio/builders/WorldBuilder.javaも参照してください。

于 2021-06-21T22:32:03.803 に答える