I have two problems adding a sensor fixture in the following code. The repositioning vector b2Vec2(0,50)
is not working; the second fixture is still centered at the origin of the body. Both fixtures can be seen in debug mode but I can't move the sensor fixture to the foot of the main fixture.
Secondly, when isSensor
is true
I am not receiving any PostSolve
events. When it is set to false
I get the appropriate events (along with collision). How do I make this a sensor that will not collide with other bodies but still raise events. Thank you for your help.
// FIXTURE DEF
var fixDef = new box2d.b2FixtureDef();
fixDef.shape = new box2d.b2PolygonShape;
fixDef.shape.SetAsBox((25 / 2 / SCALE), (46 / 2 / SCALE));
fixDef.density = 0.99;
fixDef.friction = 0.39;
fixDef.restitution = 0.0;
fixDef.userData = "SBody";
fixDef.filter.categoryBits = CAT.SOLDIER;
fixDef.filter.maskBits = CAT.GROUND;
this.view.body.CreateFixture(fixDef);
// ADD FOOT SENSOR
fixDef.density = 0.1;
fixDef.friction = 1.;
fixDef.restitution = 0.1;
fixDef.userData = "Foot";
fixDef.shape.SetAsBox((10 / 2 / SCALE), (100 / 2 / SCALE), new box2d.b2Vec2(0,50), 0);
fixDef.isSensor = true;
fixDef.filter.categoryBits = CAT.SOLDIER_FOOT_SENSOR;
fixDef.filter.maskBits = CAT.SHIP | CAT.GROUND;
this.view.body.CreateFixture(fixDef);
I was using the tip in this answer https://stackoverflow.com/a/4707127/1172891 that said to add positioning as the 3rd parameter, but I recently found that SetAsBox
cannot take a 3rd argument, maybe it used to. Instead I found SetAsOrientedBox
is a similar method and accepts the 3rd parameter for positioning. Found on the Box2dFlash reference http://www.box2dflash.org/docs/2.1a/reference/Box2D/Collision/Shapes/b2PolygonShape.html
For the sensor callbacks, I was only trying the PostSolve
at first. I recently thought to try the other events like EndContact
and found that it worked. I then found this page that seems to be the only place that explicitly states that sensors only raise BeginContact
and EndContact
events: http://www.box2dflash.org/docs/2.1a/updating in the Events section.
Hope that saves someone some time!