Danish Jamil
Danish Jamil

Reputation: 1090

LibGDX Box2D incorrect sprite position of moving body

I'm trying to make a 2D car side scrolling game. I'm using wheel joints to move the car.

Here is the screenshot when car is not moving.

Screenshot of car when not moving

And when the car is moving. You can see sprites are not in correct position.

Screenshot when the car is moving

Here is the constructor of car object.

    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);

And Here is the code to draw sprites relevant to bodies on screen.

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);
        }

Sorry if couldn't make my question very specific or clear. I'm new to Stackoverflow.

Edit [Solved]

Just placed in render method [Correct]

renderer.render(world, camera.combined);

after gl.clear I was doing it after drawing everything on screen. [Wrong]

Upvotes: 1

Views: 1476

Answers (1)

TheWhiteLlama
TheWhiteLlama

Reputation: 1286

seems like you update your physic after rendering it, because your physic-debugged polygon moved already further than the sprite shows up. You could try to first update your physics and then draw your sprites.

There shouldn't be something wrong with the code / calculations itself.

If you're already doing so it's probably because the updates of your physic simulation updates more frequently than your rendering-loop.

Upvotes: 1

Related Questions