Simple Quickbox2d Sensors

without comments

sensorpicHey, wouldn’t be nice if you could make sensors easily in quickbox2d (which is already fantastic and easy). Well, you can, and here’s how, really easily. I’d post the source code, but if you haven’t figured it out by yourself, you  might need the practice tinkering (besides Zevan will add it soon, I’m sure). This is painless, I promise.

If you don’t know what a sensor is, or why you’d want one – think of it as a physics object that detects collision but doesn’t actually affect the movement of anything its collides with – like passing through a ghost. No category or mask bits needed here, just a simple “true” or  ”false”.

here’s how to add it to quickbox2d:

OVERVIEW:

you are going to open and add one tiny line of code to 3 files.

Step 1) open com.actionsnippet.QuickObject.as

Step) find the function “defineDefaults” should look something like this:

196
197
198
199
200
201
202
203
204
205
206
207
208
209
private function defineDefaults():void{
defaults = {x:3, y:3, linearDamping:0,
angularDamping:0, isBullet:false,
fixedRotation:false,
allowSleep: true,
isSleeping:false,
scaleSkin:true,
density:1.0, friction:0.5, restitution:0.2, angle:0.0,
maskBits:0xFFFF, categoryBits:1, groupIndex:0,
draggable: true,
lineColor:0x000000, lineAlpha:1,
lineThickness:0,
fillColor:0xCCCCCC, fillAlpha:1
}

When you create an object, you pass parameters. these are the default parameters and we need to do as add “, isSensor:false” at the end so it looks like this

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
private function defineDefaults():void{
defaults = {x:3, y:3, linearDamping:0,
angularDamping:0, isBullet:false,
fixedRotation:false,
allowSleep: true,
isSleeping:false,
scaleSkin:true,
density:1.0, friction:0.5, restitution:0.2, angle:0.0,
maskBits:0xFFFF, categoryBits:1, groupIndex:0,
draggable: true,
lineColor:0x000000, lineAlpha:1,
lineThickness:0,
fillColor:0xCCCCCC, fillAlpha:1,
isSenor:false
}

1 of 3 completed. Now lets add a couple of tiny bits of code to the “BoxObject.as” and “CircleObject.as” files as well

BoxObject code: “boxDef.isSensor = p.isSensor;” see example below

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
override protected function build():void{
var p:Object = params;

if(p.skin is DisplayObject){
bodyDef.userData = p.skin;
var t:Number = p.skin.rotation;

p.skin.rotation = 0;
if (!p.width){
p.width = p.skin.width / 30;
}
if (!p.height){
p.height = p.skin.height / 30;
}
p.skin.rotation = t;
}

if (!p.width) p.width = 1;
if (!p.height) p.height = 1;

var boxDef:b2PolygonDef = new b2PolygonDef();
shapeDef = boxDef;
var hw:Number = p.width / 2;
var hh:Number = p.height / 2;
boxDef.SetAsBox(hw, hh);

boxDef.density = p.density;
boxDef.friction = p.friction;
boxDef.restitution = p.restitution;
boxDef.filter.maskBits = p.maskBits;
boxDef.filter.categoryBits = p.categoryBits;
boxDef.filter.groupIndex = p.groupIndex;
boxDef.isSensor = p.isSensor; // <------ HERE IT IS!

CircleObject code: “circDef.isSensor = p.isSensor;” see example below

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
protected override function build():void{

var p:Object = params;

var circDef:b2CircleDef = new b2CircleDef();
shapeDef = circDef;

if (p.skin is DisplayObject){
bodyDef.userData = p.skin;
if (!p.radius){
p.radius = p.skin.width/60;
}
}
if (!p.radius) p.radius = .5;

circDef.radius = p.radius;
circDef.density = p.density;
circDef.friction = p.friction;
circDef.restitution = p.restitution;
circDef.filter.maskBits = p.maskBits;
circDef.filter.categoryBits = p.categoryBits;
circDef.filter.groupIndex = p.groupIndex;
circDef.isSensor = p.isSensor; // HERE IT IS!!!

so, now, anytime you want an object to be a sensor, just tell it so on creation.
Examples:

1
2
3
sim.addBox({x:someNumber, y:someNumber, width: x:someNumber, height: x:someNumber, density:0, isSensor:true});

sim.addCircle({x: x:someNumber, y: x:someNumber, radius: x:someNumber, density: 0, isSensor:true});

Written by Jesse

June 14th, 2010 at 12:12 pm