LibGDX 的 Box2D 在设置角速度时具有奇怪的旋转行为

问题描述

我从 polygonShape 创建了一个简单的动态主体,并将其渲染为由 polygonspriteBatch 渲染的 polygonsprite。渲染工作正常,但物理不是。由速度引起的平移效果很好,但是当我设置角速度或施加扭矩时,物体也会围绕一个点“绕”一个点,同时在其原点上旋转。问题不在于起源,因为在调试时我注意到身体的位置也在变化。唯一的旋转不应该使身体平移,对吗?

这是它的旋转方式。我只设置了角速度。

rotation behavior

调试的时候线速度一直是0,但是位置发生了变化(没有任何意义),所以应该不是旋转原点错误导致的渲染问题。

这是我用多边形创建身体的地方。

public DrawableBody createFullBody(float[] vertices,Body body,Texture tex,Vector2 coord){
    //in our example: vertices = new float[]{0.0f,0.0f,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,d_body.poly.getRotation());
}

我已经在这个问题上苦苦挣扎了两天,所以任何帮助都将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)