package { import Box2D.Collision.Shapes.*; import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.*; import Box2D.Dynamics.Joints.b2PulleyJointDef; import Box2D.Dynamics.Joints.b2RevoluteJointDef; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; [SWF(width="500", height="400", frameRate="30")] public class My5thBox2D extends Sprite { private var world:b2World; private var scale:Number; public function My5thBox2D() { super(); initBg(); initWorld(); initDebugDraw(); var ground1:b2Body = createCircle(100, 0, 5, false); var ground2:b2Body = createCircle(400, 0, 5, false); var scale1:b2Body = createScale(100, 200, 140, 90); var scale2:b2Body = createScale(400, 200, 140, 90); createPulleyJoint(scale1, scale2, ground1.GetWorldCenter(), ground2.GetWorldCenter(), scale1.GetWorldCenter(), scale2.GetWorldCenter()); addEventListener(Event.ENTER_FRAME, onEnterFrame); addEventListener(MouseEvent.CLICK, onClick); } private function initBg():void { var bg:Sprite = new Sprite(); bg.graphics.beginFill(0xDDDDDD); bg.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight); bg.graphics.endFill(); addChildAt(bg,0); } private function initWorld():void { world = new b2World(new b2Vec2(0, 10), true); scale = 30; } private function initDebugDraw():void { var canvas:Sprite = new Sprite(); addChild(canvas); var dd:b2DebugDraw = new b2DebugDraw(); dd.SetDrawScale(scale); dd.SetSprite(canvas); dd.SetFlags(b2DebugDraw.e_jointBit|b2DebugDraw.e_shapeBit); world.SetDebugDraw(dd); } private function onEnterFrame(event:Event):void { world.Step(1/60,10,10); world.ClearForces(); world.DrawDebugData(); } private function onClick(event:MouseEvent):void { createBox(mouseX, mouseY, 30, 30, 0, true); } private function createBox(px:Number, py:Number, w:Number, h:Number, angle:Number, isDynamic:Boolean):b2Body { var bodyDef:b2BodyDef = new b2BodyDef(); bodyDef.angle = angle / 180 * Math.PI; bodyDef.position.Set(px/scale, py/scale); bodyDef.type = isDynamic ? b2Body.b2_dynamicBody:b2Body.b2_staticBody; var body:b2Body = world.CreateBody(bodyDef); var fixDef:b2FixtureDef = new b2FixtureDef(); fixDef.density = 100; fixDef.friction = 0.5; // fixDef.restitution = 1; var shape:b2PolygonShape = new b2PolygonShape(); shape.SetAsBox(w/2/scale, h/2/scale); fixDef.shape = shape; body.CreateFixture(fixDef); return body; } private function createCircle(px:Number, py:Number, r:Number, isDynamic:Boolean):b2Body { var bodyDef:b2BodyDef = new b2BodyDef(); bodyDef.position.Set(px/scale,py/scale); bodyDef.type = isDynamic ? b2Body.b2_dynamicBody:b2Body.b2_staticBody; var body:b2Body = world.CreateBody(bodyDef); var fixDef:b2FixtureDef = new b2FixtureDef(); fixDef.density = 100; fixDef.friction = 1; // fixDef.restitution = 1; var shape:b2CircleShape = new b2CircleShape(r/scale); fixDef.shape = shape; body.CreateFixture(fixDef); return body; } private function createRevoluteJoint(body1:b2Body, body2:b2Body, anchor:b2Vec2):void { var jointDef:b2RevoluteJointDef = new b2RevoluteJointDef(); jointDef.Initialize(body1, body2, anchor); jointDef.collideConnected = true; jointDef.enableLimit = false; jointDef.enableMotor = false; world.CreateJoint(jointDef); } private function createScale(px:Number, py:Number, w:Number, h:Number):b2Body { var center:b2Body = createCircle(px, py, 5, true); var flate:b2Body = createBox(px, py+h, w, 10, 0, true); createRevoluteJoint(flate, center, new b2Vec2((px-w/2)/scale, (py+h)/scale)); createRevoluteJoint(flate, center, new b2Vec2((px+w/2)/scale, (py+h)/scale)); return center; } private function createPulleyJoint(body1:b2Body, body2:b2Body, ground1:b2Vec2, ground2:b2Vec2, anchor1:b2Vec2, anchor2:b2Vec2):void { var jointDef:b2PulleyJointDef = new b2PulleyJointDef(); jointDef.Initialize(body1, body2, ground1, ground2, anchor1, anchor2, 1); jointDef.collideConnected = true; jointDef.maxLengthA = 190; jointDef.maxLengthB = 190; world.CreateJoint(jointDef); } } }