Guest User

Untitled

a guest
Oct 27th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.83 KB | None | 0 0
  1. $(import nape);
  2.  
  3. class Main {
  4.  
  5.     static function main() {
  6.         var c = flash.Lib.current;
  7.  
  8.         var space = new Space(new Vec2(0,600));
  9.  
  10.         var debug = new BitmapDebug(600,600,0x333333);
  11.         c.addChild(debug.display);
  12.  
  13.         var border = new Body(BodyType.STATIC);
  14.         border.shapes.add(new Polygon(Polygon.rect(0,0,-40,600)));
  15.         border.shapes.add(new Polygon(Polygon.rect(600,0,40,600)));
  16.         border.shapes.add(new Polygon(Polygon.rect(0,0,600,-40)));
  17.         border.shapes.add(new Polygon(Polygon.rect(0,600,600,40)));
  18.         border.space = space;
  19.  
  20.         var gp:GeomPoly = null;
  21.         var pre:Vec2 = null;
  22.         c.stage.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, function (_) {
  23.             gp = new GeomPoly();
  24.             gp.push(pre = Vec2.get(c.mouseX,c.mouseY));
  25.         });
  26.         c.stage.addEventListener(flash.events.MouseEvent.MOUSE_UP, function (_) {
  27.             if(gp!=null && gp.size()>3) {
  28.                 var polys = gp.simple_decomposition();
  29.                 for(p in polys) {
  30.                     var body = new Body();
  31.                     var cons = p.convex_decomposition();
  32.                     for(c in cons) body.shapes.add(new Polygon(c));
  33.                     body.align();
  34.                     body.space = space;
  35.                 }
  36.             }
  37.             gp = null;
  38.         });
  39.  
  40.         (new haxe.Timer(0)).run = function() {
  41.             debug.clear();
  42.  
  43.             if(gp!=null) {
  44.                 var cur = Vec2.get(c.mouseX,c.mouseY);
  45.                 if(cur.sub(pre).length>5) {
  46.                     gp.push(cur);
  47.                     pre = cur;
  48.                 }
  49.                 debug.drawFilledPolygon(gp,0x505050);
  50.             }
  51.  
  52.             space.step(1/60,10,10);
  53.             debug.draw(space);
  54.             debug.flush();
  55.         }
  56.     }
  57.  
  58. }
Add Comment
Please, Sign In to add comment