1
\$\begingroup\$

In my game I use the Physics Body Editor to add custom shapes to my bodies. In my show() method I use the follownig code to create my Jetski Body:

BodyDef jetskiDef = new BodyDef();
    jetskiDef.position.set(new Vector2(2, WATER_HEIGHT));
    jetskiDef.type = BodyDef.BodyType.DynamicBody;

    jetskiFixture = new FixtureDef();
    jetskiFixture.density = 0.25f;
    jetskiFixture.friction = 0.5f;
    jetskiFixture.restitution = 0.5f;
    jetskiFixture.isSensor = false;

    jetski = world.createBody(jetskiDef);
    jetski.setUserData(jetskiTexture);

Using bodyEditorLoader.attachFixture(jetski, "jetski", jetskiFixture, 1, JETSKI_BOX_WIDTH, JETSKI_BOX_HEIGHT); I add the custom shape to the fixture.

I try to render the jetskiTexture in my render() method like this:

for (Body body : worldBodies) {
        if (body.getUserData() instanceof Sprite) {
            Sprite sprite = (Sprite) body.getUserData();

            Vector2 position = body.getPosition();

            sprite.setPosition(position.x, position.y);
            sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
        }

In my beginContact() method I check if one of the fixtures is the fixture from a dynamic body and if the other one is a sensor, in order to add them to my BuoyancyController for the buoyancy effect.

public void beginContact(Contact contact) {
    Fixture fixtureA = contact.getFixtureA();
    Fixture fixtureB = contact.getFixtureB();


    if (fixtureA.isSensor()
            && fixtureB.getBody().getType() == BodyDef.BodyType.DynamicBody) {
        buoyancyController.addFixture(fixtureB);
    } else if (fixtureB.isSensor()
            && fixtureA.getBody().getType() == BodyDef.BodyType.DynamicBody) {
        buoyancyController.addFixture(fixtureA);
    }
    }

If I now start my game I can see the custom shape but there is no jetskiTexture and it also doesnt float, like it is supposed to. I tried changing the beginContact() method but it didn't work either.

public void beginContact(Contact contact) {
    Fixture fixtureA = contact.getFixtureA();
    Fixture fixtureB = contact.getFixtureB();
    String userDataA = contact.getFixtureA().getUserData().toString();
    String userDataB = contact.getFixtureB().getUserData().toString();


    if (fixtureA.isSensor()
            && userDataB == jetskiTexture) {
        buoyancyController.addFixture(fixtureB);
    } else if (fixtureB.isSensor()
            && userDataA == jetskiTexture) {
        buoyancyController.addFixture(fixtureA);
    }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.