DISTANCE JOINT

21
Jun
2012



One of the simplest joint is a distance joint which says that the distance between two points on two bodies must be constant. When you specify a distance joint the two bodies should already be in place. Then you specify the two anchor points in world coordinates. The first anchor point is connected to body 1, and the second anchor point is connected to body 2. These points imply the length of the distance constraint. Distance Joint
Make sure the following codes are at the beginning.

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
,      b2MouseJointDef =  Box2D.Dynamics.Joints.b2MouseJointDef
,	   b2DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef
,      b2DebugDraw = Box2D.Dynamics.b2DebugDraw
,      b2Fixture = Box2D.Dynamics.b2Fixture
,      b2AABB = Box2D.Collision.b2AABB;

Line 10 should be present.

Distance Joint Definition

var myjoint = new b2DistanceJointDef();
//var worldAnchorOnBody1 = new b2Vec2(12, 2);
//var worldAnchorOnBody2 = new b2Vec2(12, 8);
myjoint.Initialize(box1, box2, box1.GetWorldCenter(),box2.GetWorldCenter());
myjoint.collideConnected = true;
myjoint.frequencyHz = 1.0;
myjoint.dampingRatio = 0.1;
var distance_joint = world.CreateJoint(myjoint);
The above code will create a distance joint named "distance_joint" between box1 (Name of first body) and box2 (name of second body).

Both the anchor points will be at the center of the the two boxes respectively. If we want any other anchor point other than the center we can create a new vector like the ones commented on lines 2 & 3 and replace box1.GetWorldCenter() with created vector. It will look like:
var myjoint = new b2DistanceJointDef();
var worldAnchorOnBody1 = new b2Vec2(12, 2);
var worldAnchorOnBody2 = new b2Vec2(12, 8);
myjoint.Initialize(box1, box2, worldAnchorOnBody1, worldAnchorOnBody2);
myjoint.collideConnected = true;
myjoint.frequencyHz = 1.0;
myjoint.dampingRatio = 0.1;
var distance_joint = world.CreateJoint(myjoint);
The line:
myjoint.collideConnected = true;
means the two bodies will collide if they touch each other. If set to false they may overlap.

The lines
myjoint.frequencyHz = 1.0;
myjoint.dampingRatio = 0.1;
are optional. If set the distance joint will behave like a spring. The first line defines frequency and second line defines the damping ratio.

To know more about Damping Ratio --- Google "Mechanical Vibrations Damping Ratio"

By administrator at 04:21:21 PM 0 Comment(s)

Comments

There are no comments yet. Be the first one to add a comment!

Add a Comment

Please enter the email address.Invalid format. (Won't be Displayed)
Notify me of followup comments
Enter the displayed Code: captcha