我遇到了这个问题/答案并想分享我使用的东西,我还没有完全弄清楚一切,但它一直对我有用:
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以获取另一个示例。