I'm writing a game with impactjs and i'm using Box2D as physics-engine (Box2DFlash to be specific). So there is a move off the player where he should not collide with the enemys. To achieve this Collision Filtering seems to be the best method.
So I set for the enemy fixture:
var shapeDef = new b2.PolygonDef();
shapeDef.filter.categoryBits = 0x0002;
And for the player:
var shapeDef = new b2.PolygonDef();
shapeDef.filter.categoryBits = 0x0004;
When I want to turn on/off the collision of the player with the enemys i call these functions:
ghost: function(){
this.setMaskBits( 0x0001 ); // only collide with boundary
},
deghost: function () {
this.setMaskBits( 0xFFFF ); // collide with everything
},
setMaskBits: function (bits) {
this.body.GetShapeList().m_filter.maskBits = bits;
}
And it is working... but only when they are not colliding with each other. The same problem occurs when i'm facing a wall and I set the categoryBits to 0x0000
.
UPDATE
Ok, I fixed it with a dirty hack: The Problem was, that contacts were still existing, so I'd have to delete them somehow. I tried to delete them at first manually but failed.
In the Manual of Box2D is written:
Contacts are destroyed with the AABBs cease to overlap.
So I wrote a function that moves the player about 6px (the smallest amount that seem to work) in the directory he is facing (if he's not colliding with a wall at that moment), this would overlap the entities and solve the problem.
Here is the function in question:
changePosition: function (x,y) {
this.facingWall = false;
this.collision(); // will set facingWall to true if he's facing a wall.
if(!this.facingWall) {
this.body.m_xf.position.x = x;
this.body.m_xf.position.y = y;
}
}