deltaluca

Oval SymbolicConstraint demo

Apr 3rd, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package;
  2.  
  3. import symbolic.SymbolicConstraint;
  4.  
  5. import nape.space.Space;
  6. import nape.geom.Vec2;
  7. import nape.util.ShapeDebug;
  8. import nape.phys.BodyType;
  9. import nape.phys.Body;
  10. import nape.shape.Circle;
  11. import nape.shape.Polygon;
  12. import nape.constraints.PivotJoint;
  13.  
  14. import flash.events.MouseEvent;
  15. import flash.display.Sprite;
  16.  
  17. class Main {
  18.     static function main() {
  19.         var stage = flash.Lib.current.stage;
  20.  
  21.         var space = new Space();
  22.         var debug = new ShapeDebug(1,1);
  23.         stagae.addChild(debug.display);
  24.  
  25.         var walls = new Body(BodyType.STATIC); 
  26.         var w = stage.stageWidth;
  27.         var h = stage.stageHeight;
  28.         walls.shapes.add(new Polygon(Polygon.rect(0,0,-50,h)));
  29.         walls.shapes.add(new Polygon(Polygon.rect(0,0,w,-50)));
  30.         walls.shapes.add(new Polygon(Polygon.rect(w,0,50,h))); 
  31.         walls.shapes.add(new Polygon(Polygon.rect(0,h,w,50))); 
  32.         walls.space = space;
  33.  
  34.         var body1 = new Body(); body1.shapes.add(new Circle(20));
  35.         var body2 = new Body(); body2.shapes.add(new Circle(20));
  36.         body1.space = body2.space = space;
  37.         body1.position.setxy(150,150);
  38.         body2.position.setxy(250,150);
  39.  
  40.         var ovaldefn = "
  41.             body body1, body2
  42.             vector anchor1a, anchor1b, anchor2
  43.             scalar jointMin jointMax
  44.  
  45.             limit jointMin 0 jointMax
  46.  
  47.             constraint
  48.                 let r1a = relative body1.rotation anchor1a in
  49.                 let r1b = relative body1.rotation anchor1b in
  50.                 let r2 = relative body2.rotation anchor2 in
  51.                 let del = (r2 + body2.position) - body1.position in
  52.                   | del - r1a | + | del - r1b |
  53.    
  54.             limit constraint jointMin jointMax
  55.         ");
  56.         trace(ovaldefn);
  57.  
  58.         var oval = new SymbolicConstraint(ovaldefn);
  59.         oval.setVector("anchor1a",new Vec2(40,0));
  60.         oval.setVector("anchor1b",new Vec2(-40,0));
  61.         oval.setVector("anchor2", new Vec2(20,0));
  62.         oval.setScalar("jointMin",100);
  63.         oval.setScalar("jointMax",120);
  64.  
  65.         oval.space = space;
  66.         oval.setBody("body1",body1);
  67.         oval.setBody("body2",body2);
  68.  
  69.         space.worldLinearDrag = space.worldAngularDrag = 0.8;
  70.  
  71.         var hand = new PivotJoint(space.world,null,new Vec2(), new Vec2());
  72.         hand.active = hand.stiff = false;
  73.         hand.frequency = 10;
  74.         hand.space = space;
  75.  
  76.         stage.addEventListener(MouseEvent.MOUSE_DOWN, function (_) {
  77.             var mp = new Vec2(stage.mouseX,stage.mouseY);
  78.             for(b in space.bodiesUnderPoint(mp)) {
  79.                 if(!b.isDynamic()) continue;
  80.                 hand.body2 = b;
  81.                 hand.anchor2 = b.worldToLocal(mp);
  82.                 hand.active = true;
  83.             }
  84.         });
  85.         stage.addEventListener(MouseEvent.MOUSE_UP, function (_) hand.active = false);
  86.  
  87.         var ellipse = new Sprite();
  88.         stage.addChild(ellipse);
  89.  
  90.         (new haxe.Timer(0)).run = function() {
  91.             hand.anchor1.setxy(stage.mouseX,stage.mouseY);
  92.             space.step(1/60);
  93.             debug.clear();
  94.             debug.draw(space);
  95.  
  96.             var p0a = body1.localToWorld(oval.getVector("anchor1a"));
  97.             var p0b = body1.localToWorld(oval.getVector("anchor1b"));
  98.             debug.drawCircle(p0a,2,0xff00ff);
  99.             debug.drawCircle(p0b,2,0xff00ff);
  100.             debug.drawCircle(body2.localToWorld(oval.getVector("anchor2")),2,0xff);
  101.  
  102.             var centre = p0a.add(p0b).muleq(0.5);
  103.             var f = (p0b.sub(p0a)).length/2;
  104.             var min = oval.getScalar("jointMin"); var h0 = Math.sqrt(0.25*min*min-f*f)*2;
  105.             var max = oval.getScalar("jointMax"); var h1 = Math.sqrt(0.25*max*max-f*f)*2;
  106.  
  107.             ellipse.rotation = body1.rotation*180/Math.PI+Math.PI/2;
  108.             ellipse.x = centre.x;
  109.             ellipse.y = centre.y;
  110.             var g = ellipse.graphics;
  111.             g.clear();
  112.             g.lineStyle(1,0xff0000,1);
  113.             g.drawEllipse(-min/2,-h0/2,min,h0);
  114.             g.drawEllipse(-max/2,-h1/2,max,h1);
  115.         };
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment