Having trouble using AngleJoint

Jul 17, 2009 at 1:31 PM

Hey, I know I'm totaly noobing it up right now, but I can't the Angle Joint to work.

I declared these first:

 

PhysicsSimulator physicsSimulator;
        Body leg1Body;
        Geom leg1Geom;
        Body leg2Body;
        Geom leg2Geom;

 

PhysicsSimulator physicsSimulator;

Body leg1Body;

Geom leg1Geom;

Body leg2Body;

Geom leg2Geom;

 

 

Then I put this in Load Content:

 

physicsSimulator = new PhysicsSimulator(new Vector2(0, 600));


legTexture = Content.Load<Texture2D>(@"Images\leg100,10");


leg1Body = BodyFactory.Instance.CreateRectangleBody(

    physicsSimulator, 

    100, 

    10, 

    1);

leg1Geom = GeomFactory.Instance.CreateRectangleGeom(

    physicsSimulator, 

    leg1Body, 

    100, 

    10);


leg2Body = BodyFactory.Instance.CreateRectangleBody(

    physicsSimulator,

    100,

    10,

    1);

leg2Geom = GeomFactory.Instance.CreateRectangleGeom(

    physicsSimulator,

    leg2Body,

    100,

    10);


leg1Body.Position = new Vector2(200, 200);

leg2Body.Position = new Vector2(300, 200);

 

legJoint = JointFactory.Instance.CreateAngleJoint(

    physicsSimulator,

    leg1Body,

    leg2Body);

 

physicsSimulator.Add(leg1Body);

physicsSimulator.Add(leg1Geom);

physicsSimulator.Add(leg2Body);

physicsSimulator.Add(leg2Geom);

physicsSimulator.Add(legJoint);

 

In the update method I just tell the leg2Body to rotate, then I go ahead and draw them in the Draw method.

For one neither of them fall off the screen like I realised they should be doing after a little while, and leg1 doesnt seem to be connected to the other.

I tried looking in the manual, but I couldn't really understand it.

Jul 17, 2009 at 1:39 PM

The angle joint doesn't connect two bodies, it only sets the limit of the angle of the body relative to the other. You need to add an additional joint, like a pinjoint to stick both bodies together.

Jul 17, 2009 at 2:03 PM

Ok thanks pnikosis.

I feel a bit of a fool anyway! It would never have worked without this line of code in the Update method:

physicsSimulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);

My code now works:

 

JointFactory.Instance.CreateRevoluteJoint(
                physicsSimulator,
                leg1Body,
                leg2Body,
                leg1Body.Position + new Vector2(50, 0));
            //legJoint = JointFactory.Instance.CreateAngleJoint(
            //    physicsSimulator,
            //    leg1Body,
            //    leg2Body);
            JointFactory.Instance.CreateFixedRevoluteJoint(
                physicsSimulator,
                leg2Body,
                leg2Body.Position + new Vector2(50,0));

JointFactory.Instance.CreateRevoluteJoint(

physicsSimulator,

                leg1Body,

                leg2Body,

                leg1Body.Position + new Vector2(50, 0));


            JointFactory.Instance.CreateFixedRevoluteJoint(

                physicsSimulator,

                leg2Body,

                leg2Body.Position + new Vector2(50,0));

 

Thanks.

 

Jul 17, 2009 at 2:14 PM

No problem, another thing you should consider (as I guess you are making legs that walk :P ), is that the rotation over a body is relative to the body's center and not relative to the position of the joint. This sometimes could lead to strange results, maybe you should make your own rotation function relative to the joint's position.

Jul 17, 2009 at 6:10 PM

Ok thanks pnikosis. It may take a little while for me to digest that information. :)