Dyn4J integration - updating body fixtures doubt

Hi all,

I’m experimenting with the dyn4j integration on my editor, and I think I may not be updating the debug state properly:

When editing the shape of a body, I call removeAllFixtures() and add a new one.
It works as expected (only the new fixture is used for collision). However, the removed fixtures are still being drawn by the debug state, leaving a trail

What should I call to clear that, as that’s the only thing being dirty on screen?

Maybe you ask in the thread about the dyn4j editor, I doubt the creator checks every post here.

@iyasu said: Hi all,

I’m experimenting with the dyn4j integration on my editor, and I think I may not be updating the debug state properly:

When editing the shape of a body, I call removeAllFixtures() and add a new one.
It works as expected (only the new fixture is used for collision). However, the removed fixtures are still being drawn by the debug state, leaving a trail

What should I call to clear that, as that’s the only thing being dirty on screen?


I will take a look. Can you send a testcase?

Sure, here it is. It’s just a box over another that shrinks.

[java]
import com.jme3.app.SimpleApplication;
import com.jme3.physics.dyn4j.Dyn4jAppState;
import org.dyn4j.collision.AxisAlignedBounds;
import org.dyn4j.dynamics.Body;
import org.dyn4j.dynamics.BodyFixture;
import org.dyn4j.geometry.Mass;
import org.dyn4j.geometry.Polygon;
import org.dyn4j.geometry.Rectangle;
import org.dyn4j.geometry.Vector2;

public class PhysicsTestCase extends SimpleApplication {

Dyn4jAppState dyn4jAppState;

Body body;

Vector2 vectors[] = {new Vector2(-1, 1), new Vector2(-1, -1), new Vector2(1, -1), new Vector2(1, 1)};

float speed = 0.4f;

@Override
public void simpleInitApp() {
    dyn4jAppState = new Dyn4jAppState(new AxisAlignedBounds(30, 30));
    this.dyn4jAppState.setDebugEnabled(true);
    this.dyn4jAppState.setEnabled(true);
    this.stateManager.attach(dyn4jAppState);

    // create the geometry that will be edited later

    Polygon polygon = new Polygon(vectors);
    body = new Body();
    body.addFixture(new BodyFixture(polygon));
    body.setMass(Mass.Type.INFINITE);
    dyn4jAppState.getPhysicsSpace().addBody(body);

    // create a dynamic body to show collision

    final Rectangle boxShape = org.dyn4j.geometry.Geometry.createRectangle(.5f, .5f);
    Body dynBody = new Body();
    dynBody.addFixture(boxShape);
    dynBody.translate(0, 2);
    dynBody.setMass();
    dyn4jAppState.getPhysicsSpace().addBody(dynBody);
}

@Override
public void simpleUpdate(float tpf) {

    // notice that when the shape changes, the previous fixtures are still rendered

    vectors[0].y -= speed * tpf;
    vectors[3].y -= speed * tpf;

    if (vectors[0].y < 0.25f) {
        speed = 0;
    }
    body.removeAllFixtures();
    Polygon polygon = new Polygon(vectors);
    body.addFixture(new BodyFixture(polygon));
    body.setMass(Mass.Type.INFINITE);
}

public static void main(String args[]) {
    new PhysicsTestCase().start();
}

}
[/java]

@iyasu,

I think I fixed the issue. Take a look and let me know!

Regards,

H

@H,

Yep, that did the trick. Thanks!