Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

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:

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;
			}
		},

1 decade ago by quidmonkey

Double-check your remove entity code. My guess is that you're removing an entity and then testing for a collision against an entity that's no longer there.

1 decade ago by Seeders

Thanks for the replay quidmonkey. I thought the same thing. But even if I remove the damage from the bullets, and dont kill the bullet or zombies, the error still pops up.

I just updated the site to not remove any bullets or zombies, and it still happens. It actually happens immediately this way when one of the bullets goes off the screen. I re-added killing the bullet when it goes off screen, but the error still pops up relatively quickly.

1 decade ago by quidmonkey

Which version of Impact are you using?

Are you sure it's the bullet and not another entity?

1 decade ago by quidmonkey

The issue is that kill() on a Box2DEntity removes it from ig.world. Change your bullet update to this:

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();
                return;
            }
            if(this.isDead){
                this.kill();
                return;
            }
            this.collision();
            this.parent();    
        },  

1 decade ago by Seeders

Quote from quidmonkey
Which version of Impact are you using?

Are you sure it's the bullet and not another entity?


Impact Version 1.9.

I actually just solved my problem. I was tipped off by the fact that not removing bullets when they go off screen immediately threw the error. So I added some padding to the game screen's borders equal the bullet's speed so that they are never outside the screen and it finally stopped throwing the error.


...but now 2 or 3 minutes in to the game i get a different error:

"invalid array length" lib.js line 234. At least Ive made some progress.

1 decade ago by quidmonkey

Unsure if you saw my previous post, but the offending code is from Box2DEntity, line 68:

kill: function() {
		ig.world.DestroyBody( this.body );
		this.parent();
	}

1 decade ago by Seeders

Oh ya I missed that post. Thanks I'll take a look.

1 decade ago by Seeders

thank you!! solved both problems. =)

update : function(){

			this.collision();
			if(this.pos.x < this.speed || this.pos.x > ig.system.width - this.speed || this.pos.y < this.speed || this.pos.y > ig.system.height - ig.game.interfaceHeight - this.speed){
				this.kill();			
				return;
			} else	if( this.isDead){
				this.kill();
				return;
			}
			this.parent();	
		},	
Page 1 of 1
« first « previous next › last »