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 Sledge

I am attempting to use collision filtering in a Box2D game in order to improve gameplay. After reading through some of the documentation online, my understanding is that in order to implement this feature one must define collision groups, then implement the collision filter. They way I am doing this is by defining:
...
categoryBits: 0x0001,  	// collsion type player
maskBits: 0x0002,		// collides with enemy
...

in my player.js entity, and:
...
categoryBits: 0x0002,	// collsion type enemy
maskBits: 0x0001,		// collides with player
...

in my enemy.js entity.

I then add the following method to main.js in order to override the default collision filter:
SetContactFilter: function() {
		this.parent();
	};

When I load the game it is clear that the filtering does not work and I am quite confused. Can anyone give me a hint as to what might be going wrong?

1 decade ago by Joncom

The correct way to do it is using collision filtering because if you can do something "right here, right now", that's better than passing it off to do later. However, since I only really know how to do the "later" approach, here is one way in which you could prevent Player and Enemy entities from physically affecting one another:

EntityPlayer = ig.Entity.extend({
    preSolve: function(other, contact, manifold) {
        if(other instanceof EntityEnemy) {
            contact.SetEnabled(false);
        }
    }
});

This tells the player that when he collides with an enemy, disable the contact event before Box2D resolves the collision. You would need to do the same thing in your enemy module as well.

1 decade ago by Sledge

Great stuff man, works as intended. However, I can see how this might get messy when I try to apply it to a few more entity types.

Also, from the documentation here I would have expected the call to the contact class to read: b2Contact.SetEnabled(false). What gives here?

1 decade ago by Joncom

What gives here?
If you called b2Contact.SetEnabled(false), nothing would happen, for a few reasons:

1. No such object exists. The object you&039;re referring to exists at #Box2D.Dynamics.Contacts.b2Contact.

2. Like you said, b2Contact is a class. We don&039;t want a class. We want an "instance" of a class. And in our case, the instance is passed into #preSolve as contact.

1 decade ago by Sledge

OK, I am starting to get it. I will likely have more questions as I attempt to implement collision filtering. Thanks for the explanation.

Also, I think I may have asked this before, but is there an unbaked version of plugins/box2d/lib.js in circulation somewhere? I would like to have a look at the code if that's not against the impact and/or box2d license agreements.

1 decade ago by Joncom

You can get the unminified version here: http://code.google.com/p/box2dweb/downloads/list
Page 1 of 1
« first « previous next › last »