Tag Archives: Box2D

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 | Also 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 | Also 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 | Also tagged , , , | 3 Comments

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...

Posted in Box2D, QuickBox2D, motion | Also tagged , , , | 3 Comments