1

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.

(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)

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);
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • I realize there were similar questions but I feel they were not answered adequately nor are they as specific. – CodeHard_or_HardCode Sep 12 '14 at 04:26
  • Joint constraints can easily be pulled apart if the forces you are applying are large enough. – iforce2d Sep 12 '14 at 13:20
  • What density/force value(s) do you usually use with revolute joints and their bodies? – CodeHard_or_HardCode Sep 12 '14 at 16:56
  • That is not really as important, as the ratio between the density and the force you are applying. Eg. density = 1 and force = 1 would have the same result as density = 10 and force = 10 – iforce2d Sep 12 '14 at 17:44
  • Fair enough. What then is the ratio optimum for revolute joints? – CodeHard_or_HardCode Sep 12 '14 at 18:21
  • That depends entirely on what your game is doing with it. If you have a car with a wheel attached by a revolute joint, then you would need to adjust the numbers to make it behave the way you want your game to play. A large torque would spin the wheel, a small torque would turn it slowly or maybe even not at all. It's totally up to you how you tune these numbers, there is no magic number. – iforce2d Sep 12 '14 at 22:06
  • It seems the problem was getWorldCenter as apposed to getLocalCenter. Do you know why this would be a problem or if using local instead will have some ill affect down the road? – CodeHard_or_HardCode Sep 12 '14 at 23:47
  • Local anchors are given in local coordinates, as the name sugggests :) The tutorial you linked to in your question explains it about as well as I could. – iforce2d Sep 13 '14 at 03:47

1 Answers1

0

The problem was getWorldCenter (which was recommended in every tutorial I found)

The solution is getLocalCenter:

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

It now rotates magically!