Browse the notebook

Tag: QuickBox2D

QuickBox2D groupIndex

import com.actionsnippet.qbox.*;
import Box2D.Common.Math.*;

stage.frameRate = 60;

var sim:QuickBox2D = new QuickBox2D(this);

sim.createStageWalls();

createTraveler(3, 3);

addObstacles();

function addObstacles():void{
	sim.setDefault({groupIndex:-1, density:0, height:0.4});
	sim.addBox({x:6, y:5, width:8, angle:0.17})
    sim.addBox({x:7, y:7.1, width:8, angle:-0.17})
	sim.addBox({x:5, y:9.1, width:8,  angle:0.10})
	sim.addBox({x:6, y:11.5, width:8.9,  angle:-0.20})
	sim.addBox({x:5.5, y:16, width:9, angle:0.20})
	sim.addBox({x:5.5, y:18, width:9})
	sim.addCircle({x:11, y:20, radius:2.5, groupIndex:1});
	sim.addBox({x:16, y:19, width:2, height:2, angle:0.0, groupIndex:1})
}

function createTraveler(x:Number, y:Number):QuickObject{
	var parts:Array = [];
	parts[0] = sim.addCircle({x:0, y:1, radius:0.25, friction:0.01});
	parts[1] = sim.addCircle({x:0, y:3, radius:0.25, friction:0.01});
	parts[2] = sim.addBox({x:0, y:2, width:0.3, height:1.5 , groupIndex:-1});
	return sim.addGroup({objects:parts, x:x, y:y });
}

sim.start();
sim.mouseDrag();

One of the more advanced and useful properties of rigid bodies is the groupIndex. It allows you to specify which rigid bodies collide with one another and which rigid bodies pass through one another. This snippit demo’s the groupIndex property. For more information take a look at what the Box2D manual says.

Take a look at the swf here.

Read snippet: QuickBox2D groupIndex

QuickBox2D Chain

import com.actionsnippet.qbox.*;

var sim:QuickBox2D = new QuickBox2D(this, {debug:true});
// use the box2D default renderer (debug : true)

sim.createStageWalls();

// anchor
var pre:QuickObject = sim.addCircle({x:9, y:3, radius:.5, density:0});

// create a chain of boxes
for (var i:int = 0; i<12; i++){
  var curr:QuickObject = sim.addBox({x:10 + i, y:3, width:.9, height:.9, angularDamping:1});
									                             
  // currently always adds a b2DistanceJoint
  sim.addJoint({a:pre.body, b:curr.body, 
			               x1:9 + i, y1: 3, x2: 10 + i, y2:3, collideConnected:false});
  pre = curr;
}

// add a circle, use CCD (isBullet)
sim.addCircle({x:20, y:10, radius:1, isBullet:true});

// start simulation
sim.start();
sim.mouseDrag();

This snippet uses my QuickBox2D library to create a chain. QuickBox2D is a wrapper for Box2DFlashAS3 that greatly simplifies world setup, instantiation and graphical skinning of rigid bodies (more info on yesterdays post)…

See the swf here .

Download Box2DFlashAS3 here

Download QuickBox2D Alpha 104 here

See yesterdays post for additional information. I’m still in the process of writing some docs and possibly a tutorial for QuickBox2D… hopefully I’ll finish with that soon…

If your somewhat familiar with Box2D and just want to just dig right in, the following scrap of information may help:


// Methods of the QuickBox2D class, used for creating rigid bodies and joints
addCircle();
addBox();
addPoly();
// currently only creates b2DistanceJoints
addJoint();

these function all take one parameter Object as their argument. The common parameter properties for rigid bodies are:

// QuickBox2D simpleRender settings:
lineColor:0x000000
lineAlpha:1
lineThickness:0
fillColor:0xCCCCCC
fillAlpha:1
// wrapped Box2D properties
x:3
y:3
density:1
friction:0.5
restitution:0.2
angle: 0
linearDamping:0
angularDamping:0
isBullet:false,
fixedRotation:false
allowSleep: true
isSleeping:false
// advanced use
maskBits:0xFFFF
categoryBits:1
groupIndex:0

//and the specific param properties are:
addCircle() : radius
addBox() : width, height
addPoly() : verts

//Joints have the following properties... not going to bother explaining these until I write the docs:
addJoint() : a, b, x1, x2, y1, y2

Read snippet: QuickBox2D Chain