QuickBox2D Mini-Machine

Since this is a rather large snippet check out this demo first:

View Demo...

and here is the source:

Actionscript:
  1. import com.actionsnippet.qbox.*
  2. import Box2D.Collision.Shapes.*;
  3. import Box2D.Common.Math.*;
  4.  
  5. [SWF(width = 800, height = 600, backgroundColor = 0x000000, frameRate = 60)]
  6.  
  7. var sim:QuickBox2D = new QuickBox2D(this, {debug:true});
  8.  
  9. var sw:Number = stage.stageWidth / 30;
  10. var sh:Number = stage.stageHeight / 30;
  11.            
  12. sim.addBox({x:sw / 2, y:sh, width:sw - 3, height:1,  density:.0})
  13. sim.addBox({x:sw / 2, y:0, width:sw - 3, height:1,  density:.0 })
  14. sim.addBox({x:0, y:sh / 2, width:1, height:sh ,  density:.0})  
  15. sim.addBox({x:sw, y:sh / 2, width:1, height:sh,  density:.0})  
  16.  
  17. var slider:QuickObject = sim.addBox({x:3, y:6, width:6, height:0.25, density:0, angle:0.05});
  18. var pusher:QuickObject = sim.addBox({x:15, y:18, width:4, height:4, density:0});
  19.  
  20. sim.setDefault({collideConnected:false, frequencyHz:10});
  21. var anchor:QuickObject = sim.addCircle({x:3, y:2, radius:0.2, density:0});
  22.  
  23. var m0:QuickObject = sim.addBox({x:3, y:5, width:1, height:0.25, fixedRotation:true});
  24.  
  25. var j0:QuickObject = sim.addJoint({type:"distance", a:anchor.body, b:m0.body});
  26.  
  27. var m1:QuickObject = sim.addBox({x:2.5, y:9.5, width:0.25, height:6, fixedRotation:true});
  28. var m2:QuickObject = sim.addBox({x:3.5, y:9.5, width:0.25, height:6, fixedRotation:true});
  29.  
  30. var j1:QuickObject = sim.addJoint({type:"distance", a:m0.body, b:m1.body});
  31. var j2:QuickObject = sim.addJoint({type:"distance", a:m0.body, b:m2.body});
  32. var j3:QuickObject = sim.addJoint({type:"distance", a:m2.body, b:m1.body});
  33.  
  34. var boxes:Array = [];
  35. var boxNum:int = 21;
  36. var index:int = boxNum;
  37. var filter:b2FilterData;
  38. var lookupQuickObject:Dictionary = new Dictionary();
  39. for (var i:int = 0; i<boxNum; i++){
  40.     boxes[i] = sim.addBox({x:3, y:6.3 + i * 0.3, width:0.6, height:0.3,  friction:0.0, allowSleep:false});
  41.     lookupQuickObject[boxes[i].shape] = boxes[i];
  42. }
  43.  
  44. var m3:QuickObject = sim.addBox({x:3, y:12.25 , width:1, height:0.25, allowSleep:false,fixedRotation:true, groupIndex:-1});
  45.  
  46. var j4:QuickObject = sim.addJoint({type:"revolute", a:m1.body, b:m3.body});
  47. var j5:QuickObject = sim.addJoint({type:"revolute", a:m2.body, b:m3.body});
  48.  
  49. var t:Number = Math.PI;
  50. var dropBox:int = 0;
  51. var prevBox:QuickObject;
  52. var resetVec:b2Vec2 = new b2Vec2(0,0);
  53.  
  54. function setGroupIndex(b:QuickObject, index:int):void{
  55.     filter = b.shape.GetFilterData();
  56.     filter.groupIndex = index;
  57.     b.shape.SetFilterData(filter);
  58. }
  59.  
  60. sim.start();
  61. sim.addEventListener(QuickBox2D.STEP, onStep);
  62. function onStep(evt:Event):void{
  63.     dropBox++;
  64.    
  65.     if (dropBox % 80 == 0){
  66.         index--;
  67.         if (index> -1){
  68.             if (prevBox){
  69.                 setGroupIndex(prevBox, 1);
  70.             }
  71.             prevBox = boxes[index]
  72.             setGroupIndex(boxes[index], -1);
  73.             m3.y += 0.05;
  74.         }
  75.     }
  76.    
  77.     if (index <0){
  78.         if (pusher.x> 3.5){
  79.             pusher.x -= 0.05;
  80.         }
  81.     }
  82.     for (var i:int = 0; i<boxNum; i++){
  83.         if (boxes[i].y> 22){
  84.             boxes[i].angle = 0;
  85.             boxes[i].y = -2;
  86.             boxes[i].x = 1;
  87.             boxes[i].body.SetLinearVelocity(resetVec);
  88.         }
  89.     }
  90.    
  91.     if (index> 0){
  92.       anchor.x = 6 + 3 * Math.cos(t);
  93.       t += 0.01;
  94.     }
  95. }

This entry was posted in Box2D, QuickBox2D and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

8 Comments

  1. robodude666
    Posted October 1, 2009 at 3:40 pm | Permalink

    That’s freaking awesome! Is it supposed to stop once the green block slides over? (I didn’t read all of the source)

  2. Posted October 1, 2009 at 4:44 pm | Permalink

    yeah it stops once the thing that drops the blocks gets mostly refilled… this is unfinished… it will eventually have skinned graphics, sound a few other things…

  3. Posted October 9, 2009 at 8:47 am | Permalink

    Just wanted to thank you for making this! It makes using Box2D (and some of it’s more advance features) more accessible and easy to use, so keep up the great work! :)

  4. Posted October 9, 2009 at 9:04 am | Permalink

    no problem Leroy … glad you like it

  5. Posted October 16, 2009 at 8:30 am | Permalink

    Thanx for QBox2D!
    One question: how can I make static bodies draggable? Is it possible?

  6. Posted October 18, 2009 at 8:37 pm | Permalink

    hey flashcom… it is possible… you can do it by setting the .x and .y properties of the static QuickObject instance…. you can put a mouse event for click on the userData property of the QuickObject if you are using a skin…. if your using debug mode userData won’t be a DisplayObject…

  7. Posted November 6, 2009 at 8:20 am | Permalink

    hi Zevan,

    i finished my new portfolio that i hope i will find a job with :
    http://www.bertrandgaillard.com/

    i used QuickBox2d to make it so big thanks to you !

    Bertrand from France

  8. Posted November 6, 2009 at 8:54 am | Permalink

    nice work bertrand…. :)

One Trackback

  1. [...] setGroupIndexによる衝突判定有無の切り替え – QuickBox2D Mini-Machine [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*