Java LibGDX';s Box2D在设置角速度时具有奇怪的旋转行为
我从PolygonShape创建了一个简单的动态实体,并将其渲染为PolygonSpriteBatch渲染的PolygonSprite。渲染工作正常,但物理工作不正常。由速度引起的平移效果很好,但当我设置角速度或施加扭矩时,物体也会围绕一个点“旋转”,同时在原点旋转。问题不在于原点,因为在调试时我注意到身体的位置也在变化。唯一的旋转不应该使身体平移,对吗 这是它如何旋转的。我只设定了角速度 正如我在调试期间检查的那样,线速度始终等于0,但位置会发生变化(这没有任何意义),因此它不应该是由错误的旋转原点引起的渲染问题 这里是我用多边形创建实体的地方Java LibGDX';s Box2D在设置角速度时具有奇怪的旋转行为,java,libgdx,game-physics,box2d,Java,Libgdx,Game Physics,Box2d,我从PolygonShape创建了一个简单的动态实体,并将其渲染为PolygonSpriteBatch渲染的PolygonSprite。渲染工作正常,但物理工作不正常。由速度引起的平移效果很好,但当我设置角速度或施加扭矩时,物体也会围绕一个点“旋转”,同时在原点旋转。问题不在于原点,因为在调试时我注意到身体的位置也在变化。唯一的旋转不应该使身体平移,对吗 这是它如何旋转的。我只设定了角速度 正如我在调试期间检查的那样,线速度始终等于0,但位置会发生变化(这没有任何意义),因此它不应该是由错误的
public DrawableBody createFullBody(float[] vertices, Body body, Texture tex, Vector2 coord){
//in our example: vertices = new float[]{0.0f, 0.0f, 0.0f, 1f, 1f, 1f, 1f, 0.0f};
DrawableBody out = new DrawableBody(); //a class that contains Body and PolygonSprite
ShortArray indices = triangulate(vertices); //uses EarClippingTriangulator
PolygonShape shape = new PolygonShape();
shape.set(vertices);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
fixtureDef.restitution = 0.9f;
Fixture fixture = body.createFixture(fixtureDef);
shape.dispose(); //this is useless now, so we can dispose it.
PolygonRegion region = new PolygonRegion(new TextureRegion(tex), vertices, indices.toArray());
PolygonSprite sprite = new PolygonSprite(region);
body.setTransform(coord, 0);
sprite.setPosition(coord.x, coord.y);
out.body = body;
out.poly = sprite;
return out;
}
这就是我渲染正方形的方式:
public void render(PolygonSpriteBatch batch, float delta){
Vector2 pos = body.getPosition(); //this changes even if velocity is zero?!?
Vector2 size = model.robotSize; //this is Vector2(0.1f, 0.1f)
float angle = body.getAngle();
d_body.poly.setPosition(pos.x - size.x / 2.0f, pos.y - size.y / 2.0f);
d_body.poly.setSize(size.x, size.y);
d_body.poly.setOrigin(size.x / 2.0f, size.y / 2.0f);
d_body.poly.setRotation(angle);
batch.draw(d_body.poly.getRegion(),
d_body.poly.getX(), d_body.poly.getY(),
d_body.poly.getOriginX(), d_body.poly.getOriginY(),
d_body.poly.getWidth(), d_body.poly.getHeight(),
1.0f, 1.0f,
d_body.poly.getRotation());
}
我已经为这个问题挣扎了两天,因此非常感谢您的帮助。如果您使用Box2DebuggerRenderer渲染它,它会是什么样子?@bornander我设法解决了这个问题。基本上,这是box2d和图形之间坐标的错误转换。box2d的坐标系比图形坐标系大10倍,我必须缩放它。