Papervision3D with Box2DFlash Part 4 - Distance Joint

Monday, December 22nd, 2008 | examples

Please check the previous examples on box2dflash before jumping into this one.

This time around, we’re going to create a “Distance Joint” on the large heavy box in the middle:


source

private function createDistanceJoint():void
{
	var boxWidth:Number = 50;
 
	var boxBody:b2BodyDef = new b2BodyDef();
	boxBody.position = new b2Vec2(WIDTH/2/WORLD_SCALE, 420/WORLD_SCALE);
 
	var box:b2Body = world.CreateBody(boxBody);
 
	var boxShape:b2PolygonDef = new b2PolygonDef();
	boxShape.SetAsBox(boxWidth/WORLD_SCALE, boxWidth/WORLD_SCALE);
	boxShape.density = .7;
	boxShape.friction = .3;
	boxShape.restitution = .4;
 
	box.CreateShape(boxShape);
 
	//instead of "SetMassFromShapes", I'm setting the box
	//mass extremely high to keep it swinging back and forth
	var massData:b2MassData = new b2MassData();
	massData.mass = 1000;
 
	box.SetMass(massData);
 
	var joint:b2DistanceJointDef = new b2DistanceJointDef();
	var anchor:b2Vec2 = new b2Vec2();
 
	anchor = new b2Vec2(WIDTH/2/WORLD_SCALE, 250/WORLD_SCALE);
	joint.Initialize(box, world.m_groundBody, box.GetWorldCenter(), anchor);
 
	world.CreateJoint(joint);
 
	//start the box swinging
	box.m_linearVelocity = new b2Vec2(10, 0);
 
	var materialsList:MaterialsList = new MaterialsList();
	materialsList.addMaterial(new WireframeMaterial(0xcc0000, 1, 6), "all");
	var cube:Cube = new Cube(materialsList, boxWidth * 2, boxWidth * 2, boxWidth * 2);
 
	scene.addChild(cube);
	box.m_userData = cube;
}

Tags: ,

No comments yet.

Leave a comment