Monthly Archives: August 2009

QuickBox2D Floating Walker

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Common.Math.*;
  3. import Box2D.Dynamics.Joints.*;
  4.  
  5. [SWF (backgroundColor=0x000000, width=700, height=600, frameRate=60)]
  6.  
  7. var sim:QuickBox2D = new QuickBox2D(this, {gravityY:.2});
  8.  
  9. sim.setDefault({fillColor:0xEFEFEF, fillAlpha:0.5, lineColor:0xFFFFFF});
  10.  
  11. sim.createStageWalls();
  12.  
  13. for (var i:int = 0; i<20; i++){
  14.     sim.addCircle({x:1+i, y:1, radius:0.2, restitution:1});
  15. }
  16.  
  17. var head:QuickObject = sim.addCircle({x:0, y:0, radius:0.5});
  18. var neck:QuickObject = sim.addBox({x:0, y:1, width:0.5, height:2});
  19. var top:QuickObject = sim.addGroup({objects:[head, neck], x:10, y:5, fixedRotation:true});
  20.  
  21. var upperLegA:QuickObject = sim.addBox({x:10, y:7.5, width:0.25, height:1.5,groupIndex:-2});
  22. var lowerLegA:QuickObject = sim.addBox({x:10, y:9, width:0.25, height:1.5,groupIndex:-2});
  23.  
  24. var upperLegB:QuickObject = sim.addBox({x:10, y:7.5, width:0.25, height:1.5,groupIndex:-2});
  25. var lowerLegB:QuickObject = sim.addBox({x:10, y:9, width:0.25, height:1.5,groupIndex:-2});
  26.  
  27. var anchor:b2Vec2 = new b2Vec2();
  28. function connect(a:QuickObject, b:QuickObject, lower:Number, upper:Number, offX:Number=0, offY:Number = 0):QuickObject{
  29.      var min:Number = Math.min(a.y, b.y);
  30.      var max:Number = Math.max(a.y, b.y);
  31.      anchor.y = min + (max - min) * 0.5 + offY;
  32.      min = Math.min(a.x, b.x);
  33.      max = Math.max(a.x, b.x);
  34.      anchor.x = min + (max - min) * 0.5 + offX;
  35.      return sim.addJoint({a:a.body, b:b.body, x1:anchor.x, y1:anchor.y, lowerAngle:lower, upperAngle:upper});
  36. }
  37.  
  38. sim.setDefault({type:"revolute", collideConnected:false, enableLimit:true, enableMotor:true, maxMotorTorque:250, lineColor:0xFFFFFF});
  39.  
  40. var upperLegAJoint:QuickObject = connect(top, upperLegA, -1, 1, 0, 0.8);
  41. var upperLegBJoint:QuickObject = connect(top, upperLegB, -1, 1, 0, 0.8);
  42.  
  43. var lowerLegAJoint:QuickObject = connect(upperLegA, lowerLegA, -1, 1);
  44. var lowerLegBJoint:QuickObject = connect(upperLegB, lowerLegB, -1, 1);
  45.  
  46. sim.start();
  47. sim.mouseDrag();
  48.  
  49.  
  50. var j:b2RevoluteJoint;
  51. var t:Number = -Math.PI * 0.5;
  52. addEventListener(Event.ENTER_FRAME, onLoop);
  53. function onLoop(evt:Event):void {
  54.    
  55.     var s1:Number = 1*Math.sin(t);
  56.     var s2:Number = 1*Math.sin(t+Math.PI);
  57.     t+= 0.05;
  58.    
  59.     top.body.SetAngularVelocity(s1 * 0.25);
  60.    
  61.     j =  upperLegAJoint.joint as b2RevoluteJoint;
  62.     j.SetMotorSpeed(s1);
  63.    
  64.     j =  upperLegBJoint.joint as b2RevoluteJoint;
  65.     j.SetMotorSpeed(s2);
  66.      
  67.     j = lowerLegAJoint.joint as b2RevoluteJoint;
  68.     j.SetMotorSpeed(-s2);
  69.    
  70.     j = lowerLegBJoint.joint as b2RevoluteJoint;
  71.     j.SetMotorSpeed(-s1);
  72. }

Experimenting with primitive robot designs... this was just an improvised design - it looks like it may make sense to break out a pencil and some paper - or my wacom... to get something interesting...


Have a look at the swf...


Posted in Box2D, QuickBox2D, motion | Tagged , , , , | 1 Comment

QuickBox2D Prismatic Joint

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);
  7.  
  8. sim.setDefault({fillColor:0xCCCCCC, lineColor:0x3355AA});
  9.  
  10. sim.createStageWalls();
  11.  
  12. var box:QuickObject = sim.addBox({x:10, y:10, density:0});
  13. var circle:QuickObject = sim.addCircle({x:10, y:13});
  14.  
  15. sim.addJoint({type:"prismatic", a:box.body, b:circle.body, axis:new b2Vec2(1, 0), upperTranslation:3, lowerTranslation:-3, enableLimit:true, motorSpeed:10, maxMotorForce:10, enableMotor:true});
  16.  
  17. sim.start();
  18. sim.mouseDrag();

Simple prismatic joint demo. Prismatic joints are odd, it took me awhile to realize how to use them and what they can be used for... Plan on using them in the creation of some Box2D machines in the near futrure...

Check out the swf

Posted in Box2D, QuickBox2D, motion | Tagged , , , , | 1 Comment

QuickBox2D Gear 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,{gravityY:10});
  7.  
  8. sim.setDefault({fillColor:0x113366, fillAlpha:0.8, lineColor:0x3355AA});
  9. sim.createStageWalls();
  10.  
  11. var boxA:QuickObject = sim.addBox({x:10, y:8, width:4, height:0.5, density:10});
  12. var boxB:QuickObject = sim.addBox({x:5, y:5, density:10});
  13. boxB.body.SetLinearVelocity(new b2Vec2(5, 0));
  14.  
  15. var rev1:QuickObject = sim.addJoint({type:"revolute", a:sim.w.GetGroundBody(), b:boxA.body, anchor:boxA.body.GetWorldCenter()})
  16.            
  17. var rev2:QuickObject = sim.addJoint({type:"prismatic", a:sim.w.GetGroundBody(), axis:new b2Vec2(1, 0), b:boxB.body, anchor:boxB.body.GetWorldCenter()})
  18.                                                                                      
  19. sim.addJoint({type:"gear", a:boxA.body, b:boxB.body, joint1:rev1.joint, joint2:rev2.joint});
  20.              
  21. sim.start();
  22. sim.mouseDrag();

Simple gear joint demo - meant to serve as a reference for QuickBox2D...


Have a look at the swf...

Posted in Box2D, QuickBox2D, motion | Tagged , , , | 5 Comments

QuickBox2D Connect to GroundBody

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2.  
  3. [SWF (backgroundColor=0x000000, width=700, height=600)]
  4.  
  5. var sim:QuickBox2D = new QuickBox2D(this);
  6.  
  7. sim.setDefault({fillColor:0x113366, fillAlpha:0.8, lineColor:0x3355AA});
  8.  
  9. sim.createStageWalls();
  10.  
  11. var boxA:QuickObject = sim.addBox({x:10, y:8, width:4, height:0.5, angularDamping:1});
  12. var boxB:QuickObject = sim.addBox({x:7, y:8, width:3, height:0.25, angle:-.5, angularDamping:1});
  13. var boxC:QuickObject = sim.addBox({x:12, y:2, width:2, height:1});
  14.  
  15. var rev:QuickObject = sim.addJoint({type:"revolute", a:boxA.body, b:sim.w.GetGroundBody()});
  16. // add a red dot to boxA
  17. with (boxA.userData.graphics) lineStyle(), beginFill(0xFF0000), drawCircle(0,0,2);
  18.  
  19. var rev2:QuickObject = sim.addJoint({type:"revolute", a:boxB.body, b:sim.w.GetGroundBody()});
  20. with (boxB.userData.graphics) lineStyle(), beginFill(0xFF0000), drawCircle(0,0,2);
  21.  
  22. sim.start();
  23. sim.mouseDrag();

In order to use gear joints (covered in tomorrows post) you'll need to know how to connect to the ground body...

Check out the swf...

Posted in Box2D, QuickBox2D, motion | Tagged , , , , | 3 Comments