Pookie
Pookie

Reputation: 1279

Changing FixtureDef properties Java Libgdx

I use a Factory Pattern for creating Box2D bodies in my game. Here is the code that creates the bodies:

public static HashMap<String, Object> createAndGet(Vector2 position, Vector2 dimensions,
        BodyType type, boolean isCircle){
    HashMap<String, Object> bodyObjectsHash = new HashMap<String, Object>();

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

    Body body = EntityManager.createBody(bodyDef, dimensions);

    FixtureDef fixtureDef = new FixtureDef();
    Fixture fixture;
    if(isCircle){
        CircleShape circle = new CircleShape();
        circle.setRadius(dimensions.x);
        fixtureDef.shape = circle;
        fixture = body.createFixture(fixtureDef);
        circle.dispose();
    }else{
        PolygonShape rectangle = new PolygonShape();
        rectangle.setAsBox(dimensions.x, dimensions.y);
        fixtureDef.shape = rectangle;
        fixture = body.createFixture(fixtureDef);
        rectangle.dispose();
    }


    bodyObjectsHash.put(BodyReferences.BODY, body);
    bodyObjectsHash.put(BodyReferences.BODY_DEF, bodyDef);
    bodyObjectsHash.put(BodyReferences.FIXTURE, fixture);
    bodyObjectsHash.put(BodyReferences.FIXTURE_DEF, fixtureDef);


    return bodyObjectsHash;


}

Now, you will see this returns a HashMap. The key is strings and returns an object. This is so code like this can be accomplished:

public void attachNewSprite(String internalPath){
    entitySprite = new Sprite(new Texture(Gdx.files.internal(internalPath)));
    ((Body)bodyObjects.get(BodyReferences.BODY)).setUserData(entitySprite);
}

The HashMap returned is stored in bodyObjects

However you will notice, in the static method for creating bodies, properties such as density and friction are not set. This is because I thought by returning the objects I may edit them. Here is how I assumed I would edit the FixtureDef:

public void addFixtureDefProperties(float density, float friction, float restitution){
    ((FixtureDef)(bodyObjects.get(BodyReferences.FIXTURE_DEF))).density = density;
    ((FixtureDef)(bodyObjects.get(BodyReferences.FIXTURE_DEF))).friction = friction;
    ((FixtureDef)(bodyObjects.get(BodyReferences.FIXTURE_DEF))).restitution = restitution;

    /*This line will be discussed */
    ((Body)(bodyObjects.get(BodyReferences.BODY))).getFixtureList().removeRange(0, ((Body)(bodyObjects.get(BodyReferences.BODY))).getFixtureList().size - 1);
    ((Body)(bodyObjects.get(BodyReferences.BODY))).createFixture(((FixtureDef)(bodyObjects.get(BodyReferences.FIXTURE_DEF))));
} 

SO I essentially set the new properties, remove the fixturedef from the body then add the new one. I get a fatal error when doing this. I removed the line that removes the fixturedef's and get the same fatal error. Here is that fatal error:

enter image description here

Can someone please help me find a way to edit these properties so I don't have to flood my static createBody method with even more parameters. Thank you!

Upvotes: 0

Views: 448

Answers (1)

Peter Stampfli
Peter Stampfli

Reputation: 11

You can change the properties of a fixture using its setter-methods, except its Shape. You can get all properties with getter-methods. Have a closer look on the Fixture API. Thus you do not need to save the fixtureDef objects. BodyDef and Body are related similarly.

Upvotes: 1

Related Questions