josephoneill
josephoneill

Reputation: 853

Box2D program crashes when using multiple fixtures

This should be a quick question, with hopefully a quick answer. :) I am using box2d for a side scroller project I'm doing. All's going good, thanks for asking. :D Anyways, my character needed to differently sized boxes used for bounding. I thought it would be as simple as adding a new fixture to the body, but it didn't work. It crashes and I get this:

java: ./Box2D/Collision/b2Distance.h:103: const b2Vec2& b2DistanceProxy::GetVertex(int32) const: Assertion `0 <= index && index < m_count' failed.

I have no idea what that means. Here is the code I use to create the body and fixtures:

        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.position.set(level.character.position);
        Body body = b2world.createBody(bodyDef);
        level.character.body = body;

        PolygonShape polygonShapeHead = new PolygonShape();
        origin.x = level.character.circleBoundOrigin.x * 2.0f;
        origin.y = level.character.circleBoundOrigin.y * 3.0f;
        polygonShapeHead.setAsBox(level.character.circleBoundOrigin.x,
                level.character.circleBoundOrigin.y, origin, 0);
        FixtureDef fixtureDefHead = new FixtureDef();
        fixtureDefHead.shape = polygonShapeHead;
        fixtureDefHead.friction = level.character.friction.x;
        body.createFixture(fixtureDefHead);

        polygonShapeHead.dispose();

        PolygonShape polygonShapeBod = new PolygonShape();
        origin = level.character.rectBoundOrigin;
        polygonShapeHead.setAsBox(level.character.rectBoundOrigin.x,
                level.character.rectBoundOrigin.y, origin, 0);
        FixtureDef fixtureDefBod = new FixtureDef();
        fixtureDefBod.shape = polygonShapeBod;
        fixtureDefBod.friction = level.character.friction.x;
        body.createFixture(fixtureDefBod);

        polygonShapeBod.dispose();

As far as I know, I'm doing everything properly. So why is it not working? Why does it crash? I'm using fixturedef. Could that possibly be the problem. Thanks!

Upvotes: 1

Views: 807

Answers (1)

JMD83
JMD83

Reputation: 115

This happens when you add/destroy an obect during world.step(). If you're dynamically adding/destroying objects you need to queue them up and ensure you make your changes before or after you call world.step()

It's discussed in this tutorial series. http://youtu.be/ACQaU2Vr1ao?t=15m26s

Upvotes: 2

Related Questions