简体   繁体   English

box2d中的旋转关节

[英]Revolute Joints in box2d

I have been trying to follow the tutorial here to construct a revolving joint; 我一直在尝试按照此处的教程来构建旋转关节。 essentially, an arm. 本质上是一条手臂。 I need the arm, a rectangle, to be pinned to a point on a square and revolve around it. 我需要将手臂(一个矩形)固定到正方形上的某个点并围绕它旋转。 When force is applied I expect the two shapes to behave as one, pinned together while the arm flies around the box like a ragdoll. 施加力时,我希望这两个形状像一个一样,固定在一起,而手臂像布娃娃一样在盒子周围飞来飞去。

Unfortunately, this is not working at all. 不幸的是,这根本不起作用。 The arm is joined at first, then when I apply force the two shapes separate and behave in strange ways that I cannot explain. 首先将手臂连接起来,然后当我施加力时,两个形状分开并以我无法解释的奇怪方式表现出来。 Its almost as though the anchors were in some wonky positions. 它几乎好像锚在一些不稳固的位置上。

I'm using jbox2d and a lot of the code from this tutorial also. 我也在使用jbox2d和本教程中的很多代码。

(I have set the initial anchors to the centres just to see if it will work) (There are some strange conversions because I am using openGl) (我已经将初始锚点设置到中心,只是为了看看它是否可以工作)(有一些奇怪的转换,因为我使用的是openGl)

Here's the gist: 这是要点:

    public class New_char
    {
    Vec2             torso_pos,    arm_pos;
    Body             torso,        arm;
    PolygonShape     torso_shape,  arm_shape;
    BodyDef          torso_def,    arm_def;
    FixtureDef       torso_fix,    arm_fix;

    RevoluteJointDef torsoArmDef; 
    RevoluteJoint    torsoArmJoint; 

    //float[] torSize = {0.5f, 0.5f}, armSize={0.75f, 0.10f};


    public New_char(World world, float[] pos)
    {
        //this.torso_pos = new Vec2(pos[0], pos[1]) ;   this.arm_pos = new Vec2(pos[0]+10,pos[1]+10);   
        //out.println(this.arm_pos+" thepos "+this.torso_pos);

        this.torso_def = new BodyDef()            ;  this.arm_def = new BodyDef();
        torso_def.type = BodyType.DYNAMIC         ;  arm_def.type = BodyType.DYNAMIC;
        torso_def.position.set(320 / 30 / 2, 240 / 30 / 2)    ;  arm_def.position.set(320 / 30 / 2, 240 / 30 / 2);

        this.torso_shape = new PolygonShape()     ;  this.arm_shape = new PolygonShape();
        this.torso_shape.setAsBox(0.50f, 0.50f)   ;  this.arm_shape.setAsBox(0.75f, 0.10f);

        this.torso_fix = new FixtureDef()         ;  this.arm_fix = new FixtureDef();
        this.torso_fix.density = 0.1f             ;  this.arm_fix.density = 0.5f;
        this.torso_fix.shape = this.torso_shape   ;  this.arm_fix.shape = this.arm_shape;

        this.torso = world.createBody(this.torso_def) ;  this.arm = world.createBody(this.arm_def);
        this.torso.createFixture(this.torso_fix)      ;  this.arm.createFixture(this.arm_fix);

        this.torsoArmDef = new RevoluteJointDef();
        this.torsoArmDef.bodyA = this.torso ; this.torsoArmDef.bodyB = this.arm;
        this.torsoArmDef.collideConnected = false;
        this.torsoArmDef.localAnchorA.set(this.torso.getWorldCenter());
        //Vec2 armpin = new Vec2(1f, 1f);
        this.torsoArmDef.localAnchorB.set(this.arm.getWorldCenter());
        this.torsoArmJoint = (RevoluteJoint)world.createJoint(this.torsoArmDef);

The problem was getWorldCenter (which was recommended in every tutorial I found) 问题是getWorldCenter(在我发现的每个教程中都推荐使用)

The solution is getLocalCenter: 解决方案是getLocalCenter:

    this.torsoArmDef.localAnchorA.set(this.torso.getLocalCenter());
    this.torsoArmDef.localAnchorB.set(this.arm.getLocalCenter());

It now rotates magically! 现在,它神奇地旋转!

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

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