Home > Lab > Box2Dwrapper – Joints wrapped!

Box2Dwrapper – Joints wrapped!

March 3rd, 2009

Hi all:

What are joints (anchor points)? Basically, joints are used to constrain physics bodies to either the physics world or each other. Different joints have different properties. Some provide “limits” and some provide “motors”. There are six types of joints:

  • Distance
  • Revolute
  • Prismatic
  • Pulley
  • Gear
  • Mouse



All joints in Box2D have a neat little “initialize” method which sets up their geometric data. So how would this work with Box2Dwrapper you ask? Very simple :) . All of the joints (with the exception of Mouse) can be created using the new Joint wrapper class.

The Joint wrapper class extends the flash.utils.Proxy class hence providing full access to the selected joint type. Here’s an example of the code you will see below:

First you create the wheels:

var circ:Circle = new Circle("dynamic");
circ.radius = 25;
circ.position(stage.stageWidth/2, 20);
circ.skin = new Wheel(); // Wheel is the class name of a center registered movie clip in the library
addBody(circ);

var circ2:Circle = new Circle("dynamic");
circ2.radius = 25;
circ2.position(stage.stageWidth/2 + 100, 20);
circ2.skin = new Wheel();
addBody(circ2);

Then we create the joint. In this case, a Distance Joint:

var circleLink:Joint = new Joint(Joint.DISTANCE)
circleLink.initialize(circ.body, circ2.body, circ.body.GetWorldCenter(), circ2.body.GetWorldCenter());
circleLink.collideConnected = true;
addJoint(circleLink);

So the first line defines and creates (constructs) the type of joint we require. It can either of the following types:

  • Joint.DISTANCE
  • Joint.REVOLUTE
  • Joint.PRISMATIC
  • Joint.PULLEY
  • Joint.GEAR


The second line calls the native “Initialize” method of the the joints definition. The initialize method accepts different arguments depending on the joint type:

  • Distance : body1, body2, anchor1:b2Vec2, anchor2:b2Vec2
  • Revolute : body1, body2, anchor:b2Vec2
  • Prismatic : body1:b2Body, body2:b2Body, anchor:b2Vec2, axis:b2Vec2
  • Pulley : body1:b2Body, body2:b2Body, ground_anchor:b2Vec2, ground_anchor2:b2Vec2, anchor1:b2Vec2, anchor2:b2Vec2
  • Gear : none (this joint has no initialize method. I will explain it’s use in another post)


In line three, the “collideConnected” property is not a property of the Joint class, rather a property of the joint definition created by the wrapper itself. Because the joint class extends the Proxy class, all the properties and methods which pertain to the selected joint can be directly called Via the Joint class.

That’s pretty much it! All classes have been updated over at http://box2dwrapper.googlecode.com .

Lab

Comments are closed.