Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package example
- {
- import flash.display.DisplayObjectContainer;
- import flash.display.Sprite;
- import flash.events.MouseEvent;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import nape.constraint.PivotJoint;
- import nape.geom.Vec2;
- import nape.phys.Body;
- import nape.phys.BodyList;
- import nape.phys.BodyType;
- import nape.shape.Circle;
- import nape.shape.Polygon;
- import nape.space.Space;
- import nape.util.BitmapDebug;
- import nape.util.ShapeDebug;
- /**
- * ...
- * @author zver
- *
- */
- public class baseExample extends Sprite
- {
- protected var space:Space;
- protected var debugRender:BitmapDebug;
- private var hand:PivotJoint = null;
- public function baseExample(prnt:DisplayObjectContainer):void
- {
- super();
- prnt.addChild(this);
- init();
- }
- protected function init():void
- {
- space = new Space(new Vec2(0, 98 * 1));
- space.worldLinearDrag = 0.25;
- space.worldAngularDrag = 0.25;
- debugRender = new BitmapDebug(640, 480, 0xbebebe);
- addChild(debugRender.display);
- }
- protected function removeHand(removeListenersToo:Boolean = true):void
- {
- if (hand)
- {
- hand.active = false;
- hand.space = null;
- hand = null;
- if (removeListenersToo)
- {
- if (stage.hasEventListener(MouseEvent.MOUSE_DOWN))
- stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
- if (stage.hasEventListener(MouseEvent.MOUSE_UP))
- stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
- }
- }
- }
- protected function addHand():void
- {
- removeHand(false);
- hand = new PivotJoint(space.world, null, Vec2.weak(), Vec2.weak());
- hand.active = false;
- hand.stiff = false;
- hand.maxForce = 1e5;
- hand.space = space;
- stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
- stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
- }
- private function onMouseDown(event:MouseEvent):void
- {
- var mp:Vec2;
- var m:Matrix = debugRender.transform.toMatrix();
- m.invert();
- mp = Vec2.fromPoint(m.transformPoint(new Point(mouseX, mouseY)));
- if (hand)
- {
- // re-use same list each time.
- var bodyList:BodyList = space.bodiesUnderPoint(mp, null, bodyList);
- var k:int = bodyList.length;
- var i:int;
- for (i = 0; i < k; i++)
- {
- var body:Body = bodyList.at(i);
- if (body.isDynamic())
- {
- hand.body2 = body;
- hand.anchor2 = body.worldPointToLocal(mp, true);
- hand.active = true;
- break;
- }
- }
- // recycle nodes.
- bodyList.clear();
- }
- mp.dispose();
- }
- private function onMouseUp(event:MouseEvent):void
- {
- if (hand)
- hand.active = false;
- }
- protected function addRectangle(xx:Number, yy:Number, ww:Number, hh:Number, angle:Number = 0, isStatic:Boolean = true):Body
- {
- var tmp:Body;
- if (isStatic)
- tmp = new Body(BodyType.STATIC, new Vec2(xx, yy))
- else
- tmp = new Body(BodyType.DYNAMIC, new Vec2(xx, yy));
- tmp.shapes.add(new Polygon(Polygon.box(ww, hh)));
- tmp.rotation = angle;
- tmp.space = space;
- return tmp;
- }
- protected function addCircle(xx:Number, yy:Number, radius:Number, angle:Number = 0, isStatic:Boolean = false):Body
- {
- var tmp:Body;
- if (isStatic)
- tmp = new Body(BodyType.STATIC, new Vec2(xx, yy))
- else
- tmp = new Body(BodyType.DYNAMIC, new Vec2(xx, yy));
- tmp.shapes.add(new Circle(radius));
- tmp.rotation = angle;
- tmp.space = space;
- return tmp;
- }
- protected function addWalls():void
- {
- addRectangle(320, 0, 640, 10);
- addRectangle(320, 480, 640, 10);
- addRectangle(0, 240, 10, 480);
- addRectangle(640, 240, 10, 480);
- }
- public function free():void
- {
- removeHand();
- space.clear();
- space = null;
- removeChild(debugRender.display);
- debugRender = null;
- }
- protected function random(min:Number, max:Number):Number
- {
- return (Math.floor(Math.random() * (max - min + 1)) + min);
- }
- protected function miniUpdate(delta:Number):void
- {
- }
- protected function miniPostUpdate(delta:Number):void
- {
- }
- public function update():void
- {
- if (space == null)
- return;
- var k:int = 4;
- var i:int;
- for (i = 0; i < k;i++)
- {
- miniUpdate(1 / (30 * k));
- space.step(1 / (30 * k));
- miniPostUpdate(1 / (30 * k));
- }
- if (hand)
- {
- /*
- var mp:Vec2 = Vec2.get(mouseX, mouseY);
- mp.x -= debugRender.transform.tx;
- mp.y -= debugRender.transform.ty;
- */
- var mp:Vec2;
- var m:Matrix = debugRender.transform.toMatrix();
- m.invert();
- mp = Vec2.fromPoint(m.transformPoint(new Point(mouseX, mouseY)));
- hand.anchor1.set(mp);
- }
- if (debugRender.display.visible)
- {
- debugRender.clear();
- debugRender.draw(space);
- debugRender.flush();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement