简体   繁体   English

Java,libgdx,box2d,创建焊点当两个对象发生碰撞时

[英]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 beginContact函数中的代码是

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: 当代码运行时,存在c ++错误,但它不是很具描述性:

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. 不要在联系人监听器中创建关节,首先将主体添加到某种列表中,然后在world.step()之后创建关节。

You check how they do here: http://www.iforce2d.net/b2dtut/sticky-projectiles 你可以看看他们在这里的表现: 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. -onContactListener - >将BodyA和BodyB引用添加到列表中。 (Like they do with "StickyInfo Object"). (就像他们使用“StickyInfo对象”一样)。

-After world.step() --> using the previous list, you create joints for desired bodies. -the world.step() - >使用上一个列表,可以为所需的实体创建关节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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