package { import de.polygonal.motor2.collision.shapes.data.BoxData; import de.polygonal.motor2.dynamics.joints.data.RevoluteJointData; import de.polygonal.motor2.dynamics.joints.RevoluteJoint; import de.polygonal.motor2.dynamics.RigidBody; import de.polygonal.motor2.dynamics.RigidBodyData; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.geom.Point; import flash.utils.Timer; // motor2 physics engine import de.polygonal.motor2.math.AABB2; import de.polygonal.motor2.World; import de.polygonal.motor2.collision.shapes.ShapeSkeleton; import de.polygonal.motor2.math.V2; /** * @author Brett Jephson */ public class Motor2Demo extends Sprite { private var world:World; private var joints:Array = []; private var smallBox:BoxData = new BoxData(1, 5, 5); private var rigidBodyData:RigidBodyData; private var timer:Timer; public function Motor2Demo() { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); defineWorld(); defineObjects(); startTime(); } private function defineWorld():void { World.doWarmStarting = true; World.doPositionCorrection = true; var bounds:AABB2 = new AABB2(-100, -100, stage.stageWidth+100, stage.stageHeight+100); var doSleep:Boolean = true; world = new World(bounds, doSleep); world.setGravity(0, 10); } private function defineObjects():void { var i:int, j:int = 0; var rowsOfBoxes:int = 7; var columnsOfBoxes:int = 7; var box:BoxData; // Create the ground world.getGroundBody().x = stage.stageWidth/2; world.getGroundBody().y = stage.stageHeight - 10; // Create a simple weighing scale box = new BoxData(1, 300, 5); rigidBodyData = new RigidBodyData(stage.stageWidth/2, stage.stageHeight - 150); rigidBodyData.addShapeData(box); var pivot:RigidBody = world.createBody(rigidBodyData); var revJointData:RevoluteJointData = new RevoluteJointData(world.getGroundBody(), pivot, new Point(pivot.x, pivot.y)); joints.push(world.createJoint(revJointData) as RevoluteJoint); // Add lots of little boxes for (; i < rowsOfBoxes; i++) { j = 0; for (; j < columnsOfBoxes; j++) { addBox((stage.stageWidth / 2) - 100 + (i * 5), 25 + (j * 5)); } } for (i=0; i < rowsOfBoxes; i++) { j = 0; for (; j < columnsOfBoxes; j++) { addBox((stage.stageWidth / 2) + 100 - (i * 5), 25 + (j * 5)); } } } private function startTime():void { timer = new Timer(30); timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true); timer.start(); } private function addBox(x:int, y:int):void { rigidBodyData = new RigidBodyData(x, y); rigidBodyData.addShapeData(smallBox); world.createBody(rigidBodyData); } public function onTimer(e:TimerEvent):void { update(); render(); } private function update():void { world.step(0.15, 30) } private function render():void { graphics.clear(); graphics.lineStyle(1, 0xffcc00, 1); graphics.beginFill(0xffcc00, 0.5); var vertex:V2; var shape:ShapeSkeleton; var i:int = 0; var shapeList:Vector. = world.getShapeList(); for ( ; i < shapeList.length; i++) { shape = shapeList[i]; //transform the shape's vertices into world space so we can render the shape shape.toWorldSpace(); //vertex is a circular linked list of vertices vertex = shape.worldVertexChain; //draw the shape's outline graphics.moveTo(vertex.x, vertex.y); for (var j:int = 0; j < shape.vertexCount; j++) { vertex = vertex.next; graphics.lineTo(vertex.x, vertex.y); } } } public function dispose():void { world.deconstruct(); joints = null; timer.removeEventListener(TimerEvent.TIMER, onTimer, false); } } }