When I first created QuickBox2D I simultaneously developed a simple editor to aid in the creation of complex simulations. The result is very alpha and should be used cautiously. There is no UI, it is entirely key controlled. It generates actionscript files that can be copy and pasted into working simulations. It also has a preview mode for previewing simulations as you develop them. This is by no means a full featured editor, there is a good deal of work to be done on it. I am releasing the code as a simple zip for people who would like to develop it further. If there is enough interest I’ll create some kind of code repositiory, but for now I’m just releasing the below zip.
I may post further instructions for the editor in the future… Remember to save your work frequently and to create new versions for every change that you make to a file.
Suggested Features:
Simple GUI
Base64 encoding for get string
Known Issues:
Making joints that don’t touch things can break the preview app.
So I have a QuickBox2D editor that I’ve had since the earliest version of QuickBox2D. It is really pretty buggy and imperfect. I’m wondering if I should release it even though it’s really buggy… my main issue is I won’t be able to guarantee that it is a safe editor to use for real projects.
Lots of people have mentioned that they have problems with QuickBox2D Polygons. The simple solution is not to use the verts 2d array (which is more like how Box2D does polys). So when in doubt about polygons, simply use the points array which will nearly always work as long as the contour you define does not cross over itself. Here is a simple example on wonderfl:
Also… polygons are covered extensively in part two of the tutorial over at active tuts… more on that later.
Here is the timeline code:
import com.actionsnippet.qbox.*;
/*
0
/
0_0 0
| |
0-0
*/
var sim:QuickBox2D = new QuickBox2D(this);
sim.createStageWalls();
// define the contour of your poly
// no limits as long as it doesn't cross over
// itself
sim.addPoly({x:10, y:5, points:[0.5,0,
1, 1,
1, 2,
0.5, 2,
0.5, 1,
0,1,
0.5,0],
wireframe:false});
sim.addCircle({x:11, y:10});
sim.start();
sim.mouseDrag();
Been meaning to mention that I’m going to release an updated version of QuickBox2D in the next few days… It will have two minor bug fixes, one or two minor new features and will work with the latest version of Box2D…
[SWF(width = 800, height = 600, frameRate = 60)]
import com.actionsnippet.qbox.*;
stage.frameRate = 60;
var sim:QuickBox2D = new QuickBox2D(this);
sim.createStageWalls();
sim.start();
var output:TextField = new TextField();
output.text = "Click anywhere to add points to a polygon. Hit any key to test.nn";
output.x = output.y = 50;
with(output) width = 300, height = 400, border = true, selectable = true, wordWrap = true, multiline = true;
addChild(output);
function display(str:*):void{
output.appendText(str.toString() + "n");
}
var points:Array = [];
var poly:Shape = new Shape();
addChild(poly);
stage.addEventListener(MouseEvent.CLICK, onClick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
function onClick(evt:MouseEvent):void {
if (points.length == 0){
poly.graphics.beginFill(0xCCCCCC);
poly.graphics.lineStyle(1, 0xFF0000);
poly.graphics.moveTo(mouseX, mouseY);
}else{
poly.graphics.lineTo(mouseX, mouseY);
}
poly.graphics.drawCircle(mouseX, mouseY, 2);
points.push(mouseX / 30.0, mouseY / 30.0);
}
function onKeyPressed(evt:KeyboardEvent):void {
// average all points
var avgX:Number=0
var avgY:Number = 0;
for (var i:int = 0; i<points.length; i+=2){
avgX += points[i];
avgY += points[i + 1];
}
avgX /= points.length/2;
avgY /= points.length/2;
avgX = avgX;
avgY = avgY;
// subtract averages and fix decimal place
for (i = 0; i<points.length; i+=2){
var yp:int = i + 1;
points[i] -= avgX;
points[yp] -= avgY;
points[i] = Number(points[i].toFixed(2));
points[yp] = Number(points[yp].toFixed(2));
}
display("points array:");
display(points);
try{
var p:QuickObject = sim.addPoly({x:avgX, y:avgY, points:points});
p.userData.graphics.beginFill(0xFF0000);
p.userData.graphics.drawCircle(0,0,5);
}catch(e:*){
display("Invalid polygon data!");
}
poly.graphics.clear();
points = [];
}
This snippet shows the basic concepts needed to go about creating a polygon editor for QuickBox2D. I have an unreleased editor that I use for my QuickBox2D projects, at some point I may release it… but for now I figured I’d post this extremely simplified version for people to expand on.
Since the release of QuickBox2D 1.0 two bugs were discovered by numerous developers. The first bug was the inability to properly destroy group objects. The second bug was a small memory leak that caused most QuickObjects to remain in memory. Both of these bugs are now resolved.
Testing the memory leak. In QuickBox2D 1.0 if you created and destroyed 100s of rigid bodies, the ram would very slowly rise… Sometimes it would get garbage collected, but the memory would never fully be released. I created a simple test to make sure this is fixed in 1.1:
import com.actionsnippet.qbox.*;
var sim:QuickBox2D = new QuickBox2D(this, {debug:false});
sim.createStageWalls();
var levelParts:Array = [];
const TWO_PI:Number = Math.PI * 2;
// destroys and then creates a bunch of rigid bodies
// called every second
buildRandomLevel();
setInterval(buildRandomLevel, 1000);
var txt:TextField = TextField(addChild(new TextField()));
txt.x = txt.y = 30;
txt.backgroundColor = 0xFFFFFF;
sim.start();
// display amount of ram used, to make sure garbage collection is working
sim.addEventListener(QuickBox2D.STEP, onStep);
function onStep(evt:Event):void{
txt.text = (System.totalMemory / 100000).toFixed(2) + "mb";
}
function buildRandomLevel():void{
// destroy all rigid bodies
for (var i:int = 0; i<levelParts.length; i++){
levelParts[i].destroy();
}
levelParts = [];
// create a bunch of circles and boxes
for (i = 0; i<16; i++){
var rad:Number = 0.4 ;
levelParts.push(sim.addCircle({x:1 + i * rad * 4, y : 2, radius:rad - Math.random()*0.3}));
var rot:Number = Math.random() * TWO_PI;
levelParts.push(sim.addBox({x:4+Math.random() * i * 2, y:4+Math.random()*i,
width:3, height:0.25, angle:rot, density:0}));
}
}
This snippet creates and destroys a bunch of rigid bodies again and again…. On my macbook 2.6 Ghz intel core duo… the ram runs between 79mb and 112mb. I let it run for 40 minutes and this did not change – it always eventually returned down to 79mb.
Well… while on the topic of skinning I figured I’d post something I’ve been meaning to post for awhile. Currently, joint skinning leaves something to be desired in QuickBox2D. This is because I haven’t had the need to skin many joints … and also because with joints like pulley, prismatic, revolute etc… I’m not entirely sure what the best skinning solution even is… For instance, on a pulley you probably don’t want three duplicate MovieClips used for the skin. Anyway, early on in my Box2D experimentation I needed distance joint skins so that feature has been around since maybe alpha 108…
It’s pretty simple, Check out the demo and code below:
import com.actionsnippet.qbox.*;
[SWF(width = 800, height = 600, backgroundColor = 0xFFFFFF, frameRate=60)]
var sim:QuickBox2D = new QuickBox2D(this, {debug:false});
sim.createStageWalls({fillColor:0xFFFFFF});
var circleA:QuickObject = sim.addCircle({x:3, y:3, radius:1, skin:CircleSkin});
var circleB:QuickObject = sim.addCircle({x:6, y:6, radius:0.5, skin:CircleSkin});
sim.addJoint({type:"distance", a:circleA.body, b:circleB.body, skin:JointSkin});
sim.start();
sim.mouseDrag();