Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LibGDX Box2D移动体的精灵位置不正确_Libgdx_Box2d - Fatal编程技术网

LibGDX Box2D移动体的精灵位置不正确

LibGDX Box2D移动体的精灵位置不正确,libgdx,box2d,Libgdx,Box2d,我想做一个2D的汽车侧滚游戏。我用车轮接头来移动汽车 这是汽车不移动时的屏幕截图 当汽车行驶时。您可以看到精灵的位置不正确 这是car对象的构造函数 BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; bodyDef.position.set(x, y); //Chassis PolygonShape chassisShape = new PolygonShape

我想做一个2D的汽车侧滚游戏。我用车轮接头来移动汽车

这是汽车不移动时的屏幕截图

当汽车行驶时。您可以看到精灵的位置不正确

这是car对象的构造函数

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(x, y);

    //Chassis
    PolygonShape chassisShape = new PolygonShape();
    chassisShape.setAsBox(width, height);

    chassisFixtureDef.shape = chassisShape;

    chassis = world.createBody(bodyDef);
    // Car Body Sprite
    Sprite body = new Sprite(new Texture(Gdx.files.internal("data/body.png")));
    body.setSize(5f, 2f);
    body.setPosition(0f, 0);
    body.setOrigin(body.getWidth() / 2, body.getHeight() / 2);
    chassis.setUserData(body);
    chassis.createFixture(chassisFixtureDef);

    //Left Wheel
    CircleShape wheelShape = new CircleShape();
    wheelShape.setRadius(height / 1.5f);

    wheelFixtureDef.shape = wheelShape;

    leftWheel = world.createBody(bodyDef);
    //Sprite Test
    wheel = new Sprite(new Texture(Gdx.files.internal("data/wheel.png")));
    wheel.setSize(1f, 1f);
    wheel.setOrigin(wheel.getWidth() / 2, wheel.getHeight() / 2);
    leftWheel.setUserData(wheel);
    leftWheel.createFixture(wheelFixtureDef);

    //Right Wheel
    rightWheel = world.createBody(bodyDef);
    rightWheel.setUserData(wheel);
    rightWheel.createFixture(wheelFixtureDef);

    //Left Axis
    WheelJointDef def = new WheelJointDef();
    def.bodyA = chassis;
    def.bodyB = leftWheel;
    def.frequencyHz = chassisFixtureDef.density;
    def.localAnchorA.set(-width / 2 * 1.7f + wheelShape.getRadius(), -height / 2 * 2.5f);
    def.localAxisA.set(Vector2.Y);
    def.maxMotorTorque = chassisFixtureDef.density * 30;
    leftAxis = (WheelJoint) world.createJoint(def);

    def.bodyB = rightWheel;
    def.localAnchorA.x *= -1;

    rightAxis = (WheelJoint) world.createJoint(def);
这是在屏幕上绘制与身体相关的精灵的代码

for (Body body : bodies)
        if (body.getUserData() != null && body.getUserData() instanceof Sprite){
            Sprite sprite = (Sprite)body.getUserData();
            sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);
            sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
            sprite.draw(batch);
        }
对不起,如果我不能把我的问题说得非常具体或清楚。我是新来的

编辑[已解决] 刚刚放置在渲染方法中[正确]

renderer.render(world, camera.combined);
德国劳埃德船级社出清后
我在屏幕上画完所有的东西后就开始做了。[错误]

似乎在渲染后更新了physic,因为physic调试的多边形移动的距离已经超过精灵显示的距离。你可以尝试先更新你的物理,然后画你的精灵

代码/计算本身不应该有问题

如果您已经这样做了,可能是因为物理模拟的更新比渲染循环的更新更频繁