Saturday, July 21, 2012

Box2D - Revolute joint Skimming

Joins are such buggers, I have made a simple example for Revolute joint.
Joints are used to constraint two bodies together in various fashions. Revolute joint is a one degree joint, an anchor point around which the bodies rotate like a door hinge or a pendulum.
Joint definitions are used to construct the joints.
               e.g.,  jointDef.Initialize(box1, box2, refPoint);
here, refPoint is the anchor point where the bodies, box1 and box2 are joined.
Also,
localAnchor1, localAnchor2 - Anchor is a point on each body. It is the location around which the bodies must interact. Depending on the joint type, this point will be the center of rotation or the locations to keep a certain distance apart. In Revolute joint it is the rotation angle.

referenceAngle - The reference angle allows you to say what the 'joint angle' between the bodies is at the time of creation. In  Revolute joint the body2 angle minus body1 angle in the reference state (radians).
enableLimit, lowerAngle, upperAngle - Are the joint limits. It places a restriction on how far the bodies can rotate or slide.
enableMotor, maxMotorTorque,  motorSpeed - This enables you to apply torque or angular impulse to rotate to control the movement of the bodies. 
<html>
   <head>
      <title>Box2dWeb</title>
   </head>
   <body>
      <canvas id="canvas" width="600" height="400" style="background-color:#333333;" ></canvas>
   </body>
   <script type="text/javascript" src="libs/Box2dWeb-2.1.a.3.min.js"></script>
   <script type="text/javascript">
        var    b2Vec2 = Box2D.Common.Math.b2Vec2
        ,      b2BodyDef = Box2D.Dynamics.b2BodyDef
        ,      b2Body = Box2D.Dynamics.b2Body
        ,      b2FixtureDef = Box2D.Dynamics.b2FixtureDef
        ,      b2World = Box2D.Dynamics.b2World
        ,      b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
        ,      b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
        ,      b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef
        ,      b2DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef
        ,      b2RopeJointDef = Box2D.Dynamics.Joints.b2RopeJointDef
        ,      b2MouseJointDef =  Box2D.Dynamics.Joints.b2MouseJointDef
        ,      b2DebugDraw = Box2D.Dynamics.b2DebugDraw
        ,      b2Fixture = Box2D.Dynamics.b2Fixture
        ,      b2AABB = Box2D.Collision.b2AABB
        ,      b2Color = Box2D.Common.b2Color;

        var world = new b2World(new b2Vec2(0,10), true);

//create the ground
var groundDef = new b2BodyDef;
        groundDef.type = b2Body.b2_staticBody;
        groundDef.position.Set(10,14);
        var fixDef1 = new b2FixtureDef;
        fixDef1.shape = new b2PolygonShape;
        fixDef1.shape.SetAsBox(10,1);
        var ground = world.CreateBody(groundDef);
        ground.CreateFixture(fixDef1);

        //box1
        var box1Def = new b2BodyDef;
        box1Def.type = b2Body.b2_staticBody;
        box1Def.position.Set(10,5);
        var fixDef2 = new b2FixtureDef;
        fixDef2.density = 10.0;
        fixDef2.friction = 0.5;
        fixDef2.restitution = .5;
        fixDef2.shape = new b2PolygonShape;
        fixDef2.shape.SetAsBox(.3,.3);
        var box1 = world.CreateBody(box1Def);
        box1.CreateFixture(fixDef2);

        //box2
        var box2Def = new b2BodyDef;
        box2Def.type = b2Body.b2_dynamicBody;
        box2Def.position.Set(5,10);
        var fixDef3 = new b2FixtureDef;
        fixDef3.density = 10.0;
        fixDef3.friction = 0.5;
        fixDef3.restitution = .2;
        fixDef3.shape = new b2PolygonShape;
        fixDef3.shape.SetAsBox(.3,.3);
        var box2 = world.CreateBody(box2Def);
        box2.CreateFixture(fixDef3);

        //Revolute joint
  var jointDef = new b2RevoluteJointDef();
jointDef.Initialize(box1, box2, box1.GetWorldCenter());
        world.CreateJoint(jointDef);

        var debugDraw = new b2DebugDraw();
        debugDraw.SetSprite ( document.getElementById ("canvas").getContext ("2d"));
        debugDraw.SetDrawScale(30);     //define scale
        debugDraw.SetFillAlpha(0.3);    //define transparency
        debugDraw.SetLineThickness(1.0);
        debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
        world.SetDebugDraw(debugDraw);

        window.setInterval(update,1000/60);
        function update() {
            world.Step(1 / 60, 10, 10);
            world.DrawDebugData();
            world.ClearForces();
        };
   </script>
</html>
A joint with respect to a reference point, can be made as follows
        // Revolute joint
var refPoint = new b2Vec2(5,5);
var jointDef = new b2RevoluteJointDef();
jointDef.Initialize(box1, box2, refPoint);
        world.CreateJoint(jointDef);

Reference,
http://www.iforce2d.net/b2dtut/joints-revolute
http://www.box2dflash.org/docs/2.0.2/reference/Box2D/Dynamics/Joints/b2RevoluteJointDef.html
http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/

1 comment: