简体   繁体   中英

Java, libgdx, box2d, Creating a Weld Joint When two objects collide

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM