1 decade ago by Seeders
Hey guys, First time posting here. I just recently implemented Box2D in to my game, but have run in to trouble with collision detection. Whats strange is that the collision detection works perfectly for the first minute or so, and then all of a sudden errors pop up every frame from box2d/lib.js which is minified and just about impossible to debug. All i get is either "f is undefined" or "c is undefined" or "g is undefined".
I think the easiest way to show you is to just link to the game on my server: http://www.defeatingthepurpose.com/js/myonlyfriend/
Use chrome's web inspector or firebug to view the source. The bullet definition is in human.js towards the bottom of the file. Play the game for a wave or two and you will see the error suddenly pop up. My only theory was that i was killing entities before box2d finished its collision calculations and so it was losing objects, but when I disable the bullets/zombies from being killed the error still pops up.
Collision detection is here called in my bullet's update method:
I think the easiest way to show you is to just link to the game on my server: http://www.defeatingthepurpose.com/js/myonlyfriend/
Use chrome's web inspector or firebug to view the source. The bullet definition is in human.js towards the bottom of the file. Play the game for a wave or two and you will see the error suddenly pop up. My only theory was that i was killing entities before box2d finished its collision calculations and so it was losing objects, but when I disable the bullets/zombies from being killed the error still pops up.
Collision detection is here called in my bullet's update method:
update : function(){
if(this.pos.x < 0 || this.pos.x > ig.system.width || this.pos.y < 0 || this.pos.y + 50 > ig.system.height){
this.kill();
}
if( this.isDead){
this.kill();
}
this.collision();
this.parent();
},
collision : function () {
//for each contact edge
for (var edge = this.body.m_contactList; edge; edge = edge.next) {
//grab normal force, which is the vector
//perpendicular to the surface of contact
var normal = edge.contact.m_manifold.normal;
//calculate point of contact
var x = this.pos.x + normal.x.map(1, -1, 0, 1) * this.size.x;
var y = this.pos.y + normal.y.map(1, -1, 0, 1) * this.size.y;
var point = {
x: x,
y: y
};
//if contact is an entity
var ent = edge.other.entity;
if (!(ent === null)) {
//test for groupIndex & mask bits
var f1 = this.shape.m_filter,
f2 = ent.shape.m_filter;
if (f1.groupIndex != f2.groupIndex || f1.groupIndex > 0) {
this.collideEntity(ent, point, normal);
}
}
}
},
collideEntity: function (other, point, normal) {
if(other.name != "bullet" && other.name != "shotgunblast"){
other.hp -= this.damage;
ig.game.spawnEntity(EntityZombieBlood, other.pos.x,other.pos.y);
this.isDead = true;
}
},
