找到了正确的解决方案,失去了我生命中大约一天的时间:)
上述链接中的信息不正确和/或过时。当前平铺根据对象的类型保存对象位置。对于相对于左下角位置的图像。
Box2d实际上没有“原点”,但可以考虑是它的中心,附着到主体的固定装置的形状应该相对于(0,0)定位。
步骤1:读取平铺属性
float rotation = textureMapObject.getRotation();
float x = textureMapObject.getX();
float y = textureMapObject.getY();
float width = textureMapObject.getProperties()
.get("width", Float.class).floatValue();
float height = textureMapObject.getProperties()
.get("height", Float.class).floatValue();
第2步:根据你的box2d世界大小缩放这些,例如x=x*1/25;等
步骤3:创建没有任何位置或角度的实体。
步骤4:使用以下方法变换身体位置和角度:
private void applyTiledLocationToBody(Body body,
float x, float y,
float width, float height,
float rotation) {
// set body position taking into consideration the center position
body.setTransform(x + width / 2, y + height / 2, 0);
// bottom left position in local coordinates
Vector2 localPosition = new Vector2(-width / 2, -height / 2);
// save world position before rotation
Vector2 positionBefore = body.getWorldPoint(localPosition).cpy();
// calculate angle in radians
float angle = -rotation * MathUtils.degreesToRadians;
// set new angle
body.setTransform(body.getPosition(), angle);
// save world position after rotation
Vector2 positionAfter = body.getWorldPoint(localPosition).cpy();
// adjust position with the difference (before - after)
// so that the bottom left position remains unchanged
Vector2 newPosition = body.getPosition()
.add(positionBefore)
.sub(positionAfter);
body.setTransform(newPosition, angle);
}
希望有帮助。