Category Archives: Box2D

QuickBox2D Revolute Walker

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Dynamics.Joints.*;
  3.  
  4. [SWF (backgroundColor=0x222222, width=700, height=600)]
  5.  
  6. var sim:QuickBox2D = new QuickBox2D(this);
  7.  
  8. sim.setDefault({fillColor:0x000000, lineColor:0xCCCCCC});
  9. sim.createStageWalls();
  10.  
  11. var legA:QuickObject = sim.addBox({x:5, y:11, width:2, height:0.2,groupIndex:-2});
  12. var three:QuickObject = sim.addBox({x:6.8 + 1.8, y:11, width:2, height:0.2,groupIndex:-2});
  13. var legB:QuickObject = sim.addBox({x:6.8, y:11, width:2, height:0.5,groupIndex:-2});
  14. sim.setDefault({type:"revolute"});
  15.  
  16. var anchorX:Number = legA.x + (legB.x - legA.x) / 2;
  17. var anchorY:Number = legA.y;
  18. var revJointA:QuickObject = sim.addJoint({a:legA.body, b:legB.body, x1:anchorX, y1:anchorY,enableMotor:true, maxMotorTorque:80});
  19.  
  20. anchorX = legB.x + (three.x - legB.x) / 2;
  21.  
  22. var revJointB:QuickObject = sim.addJoint({a:legB.body, b:three.body, x1:anchorX, y1:anchorY, enableMotor:true, maxMotorTorque:80});
  23.  
  24. setWalkDir(6);
  25. addEventListener(Event.ENTER_FRAME, onLoop);
  26. function onLoop(evt:Event):void {
  27.   if (legB.x <4){
  28.     setWalkDir(6);  
  29.   }else if (legB.x> 19){
  30.     setWalkDir(-6);  
  31.   }
  32. }
  33. function setWalkDir(dir:Number):void{
  34.     var j:b2RevoluteJoint;
  35.     j = revJointA.joint as b2RevoluteJoint
  36.     j.SetMotorSpeed(dir);
  37.     j = revJointB.joint as b2RevoluteJoint
  38.     j.SetMotorSpeed(dir * -1);
  39. }
  40.  
  41. sim.start();
  42. sim.mouseDrag();

This demo is a bit more complex than the last two - mostly because it makes use of motors...


Have a look at the swf...

Also posted in QuickBox2D, motion | Tagged , , , , | 3 Comments

QuickBox2D Pulley Joint

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Common.Math.*;
  3.  
  4. [SWF (backgroundColor=0x000000, width=700, height=600)]
  5.  
  6. var sim:QuickBox2D = new QuickBox2D(this);
  7.  
  8. sim.setDefault({fillColor:0x113366, fillAlpha:0.8, lineColor:0x3355AA});
  9.  
  10. sim.createStageWalls();
  11.  
  12. var boxA:QuickObject = sim.addBox({x:10, y:8, fixedRotation:true});
  13. var boxB:QuickObject = sim.addBox({x:14, y:8, width:2,  fixedRotation:true});
  14.  
  15. sim.addJoint({type:"pulley", a:boxA.body, b:boxB.body, groundAnchor1:new b2Vec2(boxA.x, 2), groundAnchor2:new b2Vec2(boxB.x, 2)});
  16.  
  17. sim.start();
  18. sim.mouseDrag();

QuickBox2D pulley joint demo. This post is really meant to serve as part of the docs so it may seem a bit boring in its simplicity...

Have a look at the swf...

Also posted in QuickBox2D, motion | Tagged , , , , | 4 Comments

QuickBox2D Revolute Ragdoll

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Common.Math.*;
  3.  
  4. [SWF (backgroundColor=0x000000, width=700, height=600, frameRate=60)]
  5.  
  6. var sim:QuickBox2D = new QuickBox2D(this,{debug:false, frim:true, gravityY:1});
  7.  
  8. sim.setDefault({fillColor:0x113366, fillAlpha:0.8, lineColor:0x3355AA});
  9.  
  10. sim.createStageWalls();
  11.  
  12. var circle:QuickObject = sim.addCircle({x:10, y:10, radius:3});
  13. var head:QuickObject = sim.addCircle({x:5, y:5, radius:0.5});
  14. var torsoA:QuickObject = sim.addBox({x:5, y:6, width:1.5, height:0.8})
  15. var torsoB:QuickObject = sim.addBox({x:5, y:6.5, width:1.25, height:0.8})
  16. var torsoC:QuickObject = sim.addBox({x:5, y:7, width:1.0, height:0.8})
  17. var pelvis:QuickObject = sim.addBox({x:5, y:7.5, width:1.0, height:0.8})
  18. var leftLegA:QuickObject = sim.addBox({x:4.75, y:8.5, width:0.4, height:1.75});
  19. var leftLegB:QuickObject = sim.addBox({x:4.75, y:9.75, width:0.4, height:1.5});
  20. var rightLegA:QuickObject = sim.addBox({x:5.29, y:8.5, width:0.4, height:1.75});
  21. var rightLegB:QuickObject = sim.addBox({x:5.29, y:9.75, width:0.4, height:1.5});
  22. var leftArmA:QuickObject = sim.addBox({x:3.6, y:5.8, width:1.3, height:0.35});
  23. var leftArmB:QuickObject = sim.addBox({x:2.5, y:5.8, width:1.25, height:0.35});
  24. var rightArmA:QuickObject = sim.addBox({x:6.4, y:5.8, width:1.3, height:0.35});
  25. var rightArmB:QuickObject = sim.addBox({x:7.5, y:5.8, width:1.25, height:0.35});
  26.  
  27. torsoA.body.SetLinearVelocity(new b2Vec2(0, 50));
  28.  
  29. var anchor:b2Vec2 = new b2Vec2();
  30. function connect(a:QuickObject, b:QuickObject, lower:Number, upper:Number, offX:Number=0, offY:Number = 0):QuickObject{
  31.      var min:Number = Math.min(a.y, b.y);
  32.      var max:Number = Math.max(a.y, b.y);
  33.      anchor.y = min + (max - min) * 0.5 + offY;
  34.      min = Math.min(a.x, b.x);
  35.      max = Math.max(a.x, b.x);
  36.      anchor.x = min + (max - min) * 0.5 + offX;
  37.     return sim.addJoint({a:a.body, b:b.body, x1:anchor.x, y1:anchor.y, lowerAngle:lower, upperAngle:upper});
  38. }
  39.  
  40. sim.setDefault({type:"revolute", collideConnected:false, enableLimit:true, lineColor:0xFFFFFF});
  41.  
  42. connect(head, torsoA, -.2, .2);
  43. connect(torsoA, torsoB,-.2, .2);
  44. connect(torsoC, torsoB,-.2, .2);
  45. connect(torsoC, pelvis,-.2, .2);
  46.  
  47. connect(pelvis, leftLegA, -1, 1, 0, -0.5);
  48. connect(leftLegA, leftLegB, -1, 1);
  49.  
  50. connect(pelvis, rightLegA, -1, 1, 0, -0.5);
  51. connect(rightLegA, rightLegB, -1, 1);
  52.  
  53. connect(torsoA, leftArmA, -2, 1);
  54. connect(leftArmA, leftArmB, -2, 1);
  55.  
  56. connect(torsoA, rightArmA, -1, 2)
  57. connect(rightArmA, rightArmB, -1, 2)
  58.  
  59. sim.start();
  60. sim.mouseDrag();

QuickBox2D alpha 108 is finally finished and different types of joints are now possible. I'll be auto-posting joint demos over the next few days for each type of Box2D joint. Box2D joints are pretty confusing, I struggled a bit to decide on a good way to handle them in QuickBox2D, any suggestions are welcome.


Have a look at the swf:

Also posted in QuickBox2D, motion | Tagged , , , , | 1 Comment

QuickBox2D Skinning

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2.  
  3. var sim:QuickBox2D = new QuickBox2D(this);
  4.  
  5. sim.createStageWalls({lineAlpha:0,fillColor:0x000000})
  6. sim.addBox({x:3, y:3, width:3, height:3, skin:BoxSkin});
  7. sim.addCircle({x:3, y:8,radius:1.5,  skin:CircleSkin});
  8. sim.addPoly({x:6, y:3, verts:[[1.5,0,3,3,0,3,1.5,0]], skin:TriangleSkin});
  9.  
  10. sim.addBox({x:6, y:3, width:3, height:3, skin:BoxSkin});
  11. sim.addCircle({x:6, y:8,radius:1.5,  skin:CircleSkin});
  12. sim.addPoly({x:12, y:3, verts:[[1.5,0,3,3,0,3,1.5,0]], skin:TriangleSkin});
  13.  
  14. sim.start();
  15. sim.mouseDrag();

You'll need this fla to run this snippet since the graphics are in the library. This snippet shows how to easily use linkage classes as the graphics for your rigid bodies. This was actually one of the first features I implemented in QuickBox2D.


Take a look at the swf here...

Also posted in MovieClip, QuickBox2D, motion | Tagged , , , , | 33 Comments