Box2Dwrapper – More Examples with Joints
Hi!
Here are a few examples on how to set up joints using Box2Dwrapper. More to come! Remember, you can grab the wrapper source here : https://box2dwrapper.googlecode.com/svn/trunk I’ll be posting more complex examples soon so stay tuned
Prismatic Joint Example
{
import Box2D.Common.Math.b2Vec2;
import com.box2dwrapper.math.Vec;
import com.box2dwrapper.object.Box;
import com.box2dwrapper.object.Joint;
import com.box2dwrapper.view.Box2DView;
public class PrismaticJointExample extends Box2DView
{
public function PrismaticJointExample()
{
super();
initialize();
}
private function initialize():void
{
createBoundingWalls();
showDebug = true;
allowDragging = true;
createObjects();
startPhysicsRender();
}
private function createObjects():void
{
var plank:Box = new Box();
plank.scale(100, 20);
plank.position(stage.stageWidth * 0.5, stage.stageHeight * 0.5);
plank.type = "dynamic";
addBody(plank);
var joint:Joint = new Joint(Joint.PRISMATIC);
joint.initialize(world.GetGroundBody(), plank.body, plank.body.GetWorldCenter(), new b2Vec2(1, 0));
joint.lowerTranslation = -3;
joint.upperTranslation = 3
joint.enableLimit = true;
joint.maxMotorForce = 100;
joint.motorSpeed = 4.0;
joint.enableMotor = true;
addJoint(joint);
}
}
}
Revolute Joint Example
{
import com.box2dwrapper.math.Vec;
import com.box2dwrapper.object.Box;
import com.box2dwrapper.object.Circle;
import com.box2dwrapper.object.Joint;
import com.box2dwrapper.view.Box2DView;
public class RevoluteJointExample extends Box2DView
{
public function RevoluteJointExample()
{
super();
initialize();
}
private function initialize():void
{
createBoundingWalls();
showDebug = true;
allowDragging = true;
createObjects();
startPhysicsRender();
}
private function createObjects():void
{
var pivot:Circle = new Circle();
pivot.radius = 10;
pivot.position(stage.stageWidth * 0.5, stage.stageHeight * 0.5);
pivot.type = "static";
addBody(pivot);
var plank:Box = new Box();
plank.scale(20, 100);
plank.position(pivot.x, pivot.y + 35);
plank.type = "dynamic";
addBody(plank);
var tac:Joint = new Joint(Joint.REVOLUTE);
tac.initialize(pivot.body, plank.body, pivot.body.GetWorldCenter());
tac.enableMotor = true;
tac.motorSpeed = 2;
tac.maxMotorTorque = 50;
addJoint(tac);
}
}
}
Gear Joint Example
{
import Box2D.Dynamics.Joints.b2GearJoint;
import Box2D.Dynamics.Joints.b2GearJointDef;
import com.box2dwrapper.object.Circle;
import com.box2dwrapper.object.Joint;
import com.box2dwrapper.view.Box2DView;
public class GearJointExample extends Box2DView
{
public function GearJointExample()
{
super();
showDebug = true;
allowDragging = true;
initialize();
startPhysicsRender();
}
private function initialize():void
{
var gear1:Circle = new Circle();
gear1.radius = 40;
gear1.density = 2;
gear1.position(stage.stageWidth * 0.45, stage.stageHeight * 0.5);
gear1.type = "dynamic";
addBody(gear1);
var gear2:Circle = new Circle();
gear2.radius = 60;
gear2.density = 3;
gear2.position(stage.stageWidth * 0.65, stage.stageHeight * 0.5);
gear2.type = "dynamic";
addBody(gear2);
var joint1:Joint = new Joint(Joint.REVOLUTE);
joint1.initialize(world.GetGroundBody(), gear1.body, gear1.body.GetWorldCenter());
addJoint(joint1);
var joint2:Joint = new Joint(Joint.REVOLUTE);
joint2.initialize(world.GetGroundBody(), gear2.body, gear2.body.GetWorldCenter());
addJoint(joint2);
var gearJoint:Joint = new Joint(Joint.GEAR);
gearJoint.body1 = gear1.body;
gearJoint.body2 = gear2.body;
gearJoint.joint1 = joint1.joint;
gearJoint.joint2 = joint2.joint;
gearJoint.ratio = 1;
addJoint(gearJoint);
}
}
}
Experiment, Lab, as3

Recent Comments