Advertisement
liuwong

nape examples - base class

Jan 19th, 2013
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package example
  2. {
  3.     import flash.display.DisplayObjectContainer;
  4.     import flash.display.Sprite;
  5.     import flash.events.MouseEvent;
  6.     import flash.geom.Matrix;
  7.     import flash.geom.Point;
  8.     import nape.constraint.PivotJoint;
  9.     import nape.geom.Vec2;
  10.     import nape.phys.Body;
  11.     import nape.phys.BodyList;
  12.     import nape.phys.BodyType;
  13.     import nape.shape.Circle;
  14.     import nape.shape.Polygon;
  15.     import nape.space.Space;
  16.     import nape.util.BitmapDebug;
  17.     import nape.util.ShapeDebug;
  18.    
  19.     /**
  20.      * ...
  21.      * @author zver
  22.      *
  23.      */
  24.    
  25.     public class baseExample extends Sprite
  26.     {
  27.         protected var space:Space;
  28.        
  29.         protected var debugRender:BitmapDebug;
  30.        
  31.         private var hand:PivotJoint = null;
  32.        
  33.         public function baseExample(prnt:DisplayObjectContainer):void
  34.         {
  35.             super();
  36.            
  37.             prnt.addChild(this);
  38.            
  39.             init();
  40.         }
  41.        
  42.         protected function init():void
  43.         {
  44.             space = new Space(new Vec2(0, 98 * 1));
  45.             space.worldLinearDrag = 0.25;
  46.             space.worldAngularDrag = 0.25;
  47.            
  48.             debugRender = new BitmapDebug(640, 480, 0xbebebe);
  49.             addChild(debugRender.display);
  50.         }
  51.        
  52.         protected function removeHand(removeListenersToo:Boolean = true):void
  53.         {
  54.             if (hand)
  55.             {
  56.                 hand.active = false;
  57.                 hand.space = null;
  58.                 hand = null;
  59.                
  60.                 if (removeListenersToo)
  61.                 {
  62.                     if (stage.hasEventListener(MouseEvent.MOUSE_DOWN))
  63.                         stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  64.                        
  65.                     if (stage.hasEventListener(MouseEvent.MOUSE_UP))
  66.                         stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  67.                 }
  68.             }
  69.         }
  70.        
  71.         protected function addHand():void
  72.         {
  73.             removeHand(false);
  74.            
  75.             hand = new PivotJoint(space.world, null, Vec2.weak(), Vec2.weak());
  76.             hand.active = false;
  77.             hand.stiff = false;
  78.             hand.maxForce = 1e5;
  79.             hand.space = space;
  80.            
  81.             stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  82.             stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  83.         }
  84.        
  85.         private function onMouseDown(event:MouseEvent):void
  86.         {
  87.             var mp:Vec2;
  88.             var m:Matrix = debugRender.transform.toMatrix();
  89.             m.invert();
  90.             mp = Vec2.fromPoint(m.transformPoint(new Point(mouseX, mouseY)));
  91.            
  92.             if (hand)
  93.             {
  94.                 // re-use same list each time.
  95.                 var bodyList:BodyList = space.bodiesUnderPoint(mp, null, bodyList);
  96.                
  97.                 var k:int = bodyList.length;
  98.                 var i:int;
  99.                 for (i = 0; i < k; i++)
  100.                 {
  101.                     var body:Body = bodyList.at(i);
  102.                     if (body.isDynamic())
  103.                     {
  104.                         hand.body2 = body;
  105.                         hand.anchor2 = body.worldPointToLocal(mp, true);
  106.                         hand.active = true;
  107.                         break;
  108.                     }
  109.                 }
  110.                
  111.                 // recycle nodes.
  112.                 bodyList.clear();
  113.             }
  114.            
  115.             mp.dispose();
  116.         }
  117.        
  118.         private function onMouseUp(event:MouseEvent):void
  119.         {
  120.             if (hand)
  121.                 hand.active = false;
  122.         }
  123.        
  124.         protected function addRectangle(xx:Number, yy:Number, ww:Number, hh:Number, angle:Number = 0, isStatic:Boolean = true):Body
  125.         {
  126.             var tmp:Body;
  127.            
  128.             if (isStatic)
  129.                 tmp = new Body(BodyType.STATIC, new Vec2(xx, yy))
  130.             else
  131.                 tmp = new Body(BodyType.DYNAMIC, new Vec2(xx, yy));
  132.                
  133.             tmp.shapes.add(new Polygon(Polygon.box(ww, hh)));
  134.             tmp.rotation = angle;
  135.             tmp.space = space;
  136.            
  137.             return tmp;
  138.         }
  139.        
  140.         protected function addCircle(xx:Number, yy:Number, radius:Number, angle:Number = 0, isStatic:Boolean = false):Body
  141.         {
  142.             var tmp:Body;
  143.            
  144.             if (isStatic)
  145.                 tmp = new Body(BodyType.STATIC, new Vec2(xx, yy))
  146.             else
  147.                 tmp = new Body(BodyType.DYNAMIC, new Vec2(xx, yy));
  148.                
  149.             tmp.shapes.add(new Circle(radius));
  150.             tmp.rotation = angle;
  151.             tmp.space = space;
  152.            
  153.             return tmp;
  154.         }
  155.        
  156.         protected function addWalls():void
  157.         {
  158.             addRectangle(320, 0, 640, 10);
  159.             addRectangle(320, 480, 640, 10);
  160.             addRectangle(0, 240, 10, 480);
  161.             addRectangle(640, 240, 10, 480);
  162.         }
  163.        
  164.         public function free():void
  165.         {
  166.             removeHand();
  167.            
  168.             space.clear();
  169.             space = null;
  170.            
  171.             removeChild(debugRender.display);
  172.             debugRender = null;
  173.         }
  174.        
  175.         protected function random(min:Number, max:Number):Number
  176.         {  
  177.              return (Math.floor(Math.random() * (max - min + 1)) + min);
  178.         }
  179.        
  180.         protected function miniUpdate(delta:Number):void
  181.         {
  182.            
  183.         }
  184.        
  185.         protected function miniPostUpdate(delta:Number):void
  186.         {
  187.            
  188.         }
  189.        
  190.         public function update():void
  191.         {
  192.             if (space == null)
  193.                 return;
  194.            
  195.             var k:int = 4;
  196.             var i:int;
  197.            
  198.             for (i = 0; i < k;i++)
  199.             {
  200.                 miniUpdate(1 / (30 * k));
  201.                
  202.                 space.step(1 / (30 * k));
  203.                
  204.                 miniPostUpdate(1 / (30 * k));
  205.             }
  206.                
  207.             if (hand)
  208.             {
  209.                 /*
  210.                 var mp:Vec2 = Vec2.get(mouseX, mouseY);
  211.                 mp.x -= debugRender.transform.tx;
  212.                 mp.y -= debugRender.transform.ty;
  213.                 */
  214.                
  215.                 var mp:Vec2;
  216.                 var m:Matrix = debugRender.transform.toMatrix();
  217.                 m.invert();
  218.                 mp = Vec2.fromPoint(m.transformPoint(new Point(mouseX, mouseY)));
  219.                
  220.                 hand.anchor1.set(mp);
  221.             }  
  222.            
  223.             if (debugRender.display.visible)
  224.             {
  225.                 debugRender.clear();
  226.                 debugRender.draw(space);
  227.                 debugRender.flush();
  228.             }
  229.         }
  230.     }
  231.    
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement