By Zevan | January 15, 2010
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…
By Zevan | December 1, 2009
Actionscript:
-
[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.\n\n";
-
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.
Have a look at the swf here...
By Zevan | November 18, 2009
Actionscript:
-
import com.actionsnippet.qbox.*;
-
import Box2D.Dynamics.*
-
-
stage.frameRate = 60;
-
-
var sim:QuickBox2D = new QuickBox2D(this, {debug:true});
-
-
// get at the b2DebugDraw instance
-
var debug:b2DebugDraw = sim.w.m_debugDraw;
-
debug.m_drawScale = 30.0;
-
debug.m_fillAlpha = 0.5;
-
debug.m_alpha = 0.5;
-
debug.m_lineThickness = 1.0;
-
debug.m_drawFlags = 0xFF;
-
-
sim.createStageWalls();
-
-
for (var i:int = 0; i<10; i++){
-
sim.addBox({x:3 + i, y:3 + i, width:2, height:0.5});
-
}
-
sim.addCircle({x:12, y:5, radius:2});
-
-
sim.start();
-
sim.mouseDrag();
Note: This snippet requires the QuickBox2D library
This snippet shows an easy way to get at the settings for Box2D's debug renderer.
Have a look at the swf...
By Zevan | November 7, 2009
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.
Download QuickBox2D 1.1
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:
Actionscript:
-
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.
Have a look at the swf...
Thanks to all the people who gave feedback that lead to the discover of these bugs.