1

I have 2 circles that are colliding.

I want to weld them together when they collide. The code, within a beginContact function, is

WeldJointDef def = new WeldJointDef();

def.collideConnected=false;
Vector2 worldCoordsAnchorPoint = bodyA.getWorldPoint(new Vector2(0.0f,0.0f)); 

def.bodyA = bodyA;
def.bodyB = bodyB;

def.localAnchorA.set( def.bodyA.getLocalPoint(worldCoordsAnchorPoint) );
def.referenceAngle = def.bodyB.getAngle() - def.bodyA.getAngle();


def.initialize(def.bodyA, def.bodyB, worldCoordsAnchorPoint);

world.createJoint(def); 

When the code runs, there is a c++ error, but it isn't very descriptive:

AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!

Program: C:\Program Files\Java\jre7\bin\javaw.exe
File: /var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Dynamics/b2World.cpp, Line 214

Expression: IsLocked() == false

How do I get the weld to work? Or can you not create weld joints on the fly in this way?

Ginger
  • 8,320
  • 12
  • 56
  • 99
  • 2
    I am not completely sure what causes this so I wont submit an answer, but it might be that you are editing the objects during the collision simulation. [This](http://gamedev.stackexchange.com/questions/27113/what-is-the-proper-way-to-remove-a-box2d-body-from-the-world-in-libgdx) seems like the same problem but with deleting the body, not welding. – vuolen Jun 14 '14 at 13:38
  • 2
    SneakyB is correct. You cannot modify the simulation during the world step. You'll have to note the colliding bodies and create the joint after the step has completed. – nEx.Software Jun 14 '14 at 16:12
  • 1
    this was answered here. http://stackoverflow.com/questions/21293923/how-to-make-two-bodies-stick-after-a-collision, it's works for me, I hope this help you. – Lematva Aug 01 '14 at 05:15

1 Answers1

3

Dont create joints inside contact listener, first add the bodies to some kind of list, and them after world.step() you create the joint.

You check how they do here: http://www.iforce2d.net/b2dtut/sticky-projectiles

So resuming, the way to do would be this:

-onContactListener --> Add BodyA and BodyB reference to a list. (Like they do with "StickyInfo Object").

-After world.step() --> using the previous list, you create joints for desired bodies.

Tiago Santos
  • 489
  • 5
  • 16