Actionscript:
-
import com.actionsnippet.qbox.*;
-
import Box2D.Common.Math.*;
-
import Box2D.Dynamics.Joints.*;
-
-
[SWF (backgroundColor=0x000000, width=700, height=600, frameRate=60)]
-
-
var sim:QuickBox2D = new QuickBox2D(this, {gravityY:.2});
-
-
sim.setDefault({fillColor:0xEFEFEF, fillAlpha:0.5, lineColor:0xFFFFFF});
-
-
sim.createStageWalls();
-
-
for (var i:int = 0; i<20; i++){
-
sim.addCircle({x:1+i, y:1, radius:0.2, restitution:1});
-
}
-
-
var head:QuickObject = sim.addCircle({x:0, y:0, radius:0.5});
-
var neck:QuickObject = sim.addBox({x:0, y:1, width:0.5, height:2});
-
var top:QuickObject = sim.addGroup({objects:[head, neck], x:10, y:5, fixedRotation:true});
-
-
var upperLegA:QuickObject = sim.addBox({x:10, y:7.5, width:0.25, height:1.5,groupIndex:-2});
-
var lowerLegA:QuickObject = sim.addBox({x:10, y:9, width:0.25, height:1.5,groupIndex:-2});
-
-
var upperLegB:QuickObject = sim.addBox({x:10, y:7.5, width:0.25, height:1.5,groupIndex:-2});
-
var lowerLegB:QuickObject = sim.addBox({x:10, y:9, width:0.25, height:1.5,groupIndex:-2});
-
-
var anchor:b2Vec2 = new b2Vec2();
-
function connect(a:QuickObject, b:QuickObject, lower:Number, upper:Number, offX:Number=0, offY:Number = 0):QuickObject{
-
var min:Number = Math.min(a.y, b.y);
-
var max:Number = Math.max(a.y, b.y);
-
anchor.y = min + (max - min) * 0.5 + offY;
-
min = Math.min(a.x, b.x);
-
max = Math.max(a.x, b.x);
-
anchor.x = min + (max - min) * 0.5 + offX;
-
return sim.addJoint({a:a.body, b:b.body, x1:anchor.x, y1:anchor.y, lowerAngle:lower, upperAngle:upper});
-
}
-
-
sim.setDefault({type:"revolute", collideConnected:false, enableLimit:true, enableMotor:true, maxMotorTorque:250, lineColor:0xFFFFFF});
-
-
var upperLegAJoint:QuickObject = connect(top, upperLegA, -1, 1, 0, 0.8);
-
var upperLegBJoint:QuickObject = connect(top, upperLegB, -1, 1, 0, 0.8);
-
-
var lowerLegAJoint:QuickObject = connect(upperLegA, lowerLegA, -1, 1);
-
var lowerLegBJoint:QuickObject = connect(upperLegB, lowerLegB, -1, 1);
-
-
sim.start();
-
sim.mouseDrag();
-
-
-
var j:b2RevoluteJoint;
-
var t:Number = -Math.PI * 0.5;
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
-
var s1:Number = 1*Math.sin(t);
-
var s2:Number = 1*Math.sin(t+Math.PI);
-
t+= 0.05;
-
-
top.body.SetAngularVelocity(s1 * 0.25);
-
-
j = upperLegAJoint.joint as b2RevoluteJoint;
-
j.SetMotorSpeed(s1);
-
-
j = upperLegBJoint.joint as b2RevoluteJoint;
-
j.SetMotorSpeed(s2);
-
-
j = lowerLegAJoint.joint as b2RevoluteJoint;
-
j.SetMotorSpeed(-s2);
-
-
j = lowerLegBJoint.joint as b2RevoluteJoint;
-
j.SetMotorSpeed(-s1);
-
}
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...