Tiled map 对象的位置x,y(以像素为单位)和旋转(以度为单位)。
我正在从 map 加载坐标和旋转,并尝试将它们分配给box2d实体。位置模型之间存在一些差异,例如,平铺对象的旋转度数为度,而box2d体角的度数为弧度。
如何将位置转换为BodyDef坐标x,y和角度,以便在正确的位置创建实体?
背景:
使用代码:
float angle = -rotation * MathUtils.degreesToRadians;
bodyDef.angle = angle;
bodyDef.position.set(x, y);
旋转为0时有效,但是旋转不为0时, body 未正确定位。
我在这里找到了一些提示:
http://www.tutorialsface.com/2015/12/qu ... dx-solved/
和这里:
https://github.com/libgdx/libgdx/issues/2742
这似乎解决了这个确切的问题,但是,没有一种解决方案对我没有用,在应用了这些转换之后, body 对象仍然定位错误。错误定位是指将 body 放置在 map 上应该放置的区域中,但会根据其旋转而略微偏离。
我觉得这应该很简单,但是我不知道如何调和Tiled和box2d位置之间的差异。
作为引用,以下是我从上面的链接尝试过的两个解决方案(将值x,y,宽度,高度从像素转换为世界单位后):
float angle = rotation * MathUtils.degreesToRadians;
bodyDef.angle = -angle;
Vector2 correctionPosition = new Vector2(
height * MathUtils.cosDeg(-rotation - 90),
height + height * MathUtils.sinDeg(-rotation - 90));
bodyDef.position.set(x, y).add(correctionPosition);
和
float angle = rotation * MathUtils.degreesToRadians;
bodyDef.angle = -angle;
// Top left corner of object
Vector2 correctedPosition = new Vector2(x, y + height);
// half of diagonal for rectangular object
float radius = (float)Math.sqrt(width * width + height * height) / 2.0f;
// Angle at diagonal of rectangular object
float theta = (float)Math.tanh(height / width) * MathUtils.degreesToRadians;
// Finding new position if rotation was with respect to top-left corner of object.
// X=x+radius*cos(theta-angle)+(h/2)cos(90+angle)
// Y=y+radius*sin(theta-angle)-(h/2)sin(90+angle)
correctedPosition = correctedPosition
.add(
radius * MathUtils.cos(theta - angle),
radius * MathUtils.sin(theta - angle))
.add(
((height / 2) * MathUtils.cos(MathUtils.PI2 + angle)),
(-(height / 2) * MathUtils.sin(MathUtils.PI2 + angle)));
bodyDef.position.set(correctedPosition);
任何提示将受到高度欢迎。
最佳答案
找到了正确的解决方案,使我失去了大约1天的生命:)
来自以上链接的信息不正确和/或已过时。当前,“平铺”根据对象的类型保存对象的位置。对于图像而言,是相对于左下角的位置。
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:使用以下方法转换 body 位置和角度:
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);
}
希望能帮助到你。
关于Libgdx平铺 map 对象到box2d body 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35114988/