1
votes

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!

1
Do you know what line the error occurs on in the code you've given us? - kabb
No, unfortunately. The only thing that appeared in the console box was the message above. - josephoneill
I just ran it, and it worked. Ran it again, same message. Sometimes it works, sometimes it doesn't. What's going on. :| - josephoneill
Could it be that your setAsBox() does set the vertices incorrect? Like width=5, height=0. or width=-2, height=3? - noone
No. :/ Everything is proper. The strange thing is, every 1/20 tries, it'll actually run. It runs just how I need it too, with boxes in the correct spots. Could it possibly be not an error in my code, but a build error? - josephoneill

1 Answers

2
votes

This happens when you add/destroy obects 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