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 tapwater

Hey all, I need a concrete example of detection entity collisions in Box2D.

Up until this point, I've been using Impact's checkAgainst and check methods to detect collisions between entities. Obviously this is the wrong way. Half the time it doesn't detect a collision at all.

Now I've seen ContactListener been mentioned a few times, but never a real example of it being used. Where should this code go? Do I get access to the entity types?

Any entity collision detection example would be greatly appreciated.

Just to be clear: I am using both Box2D and Box2DEntity.

1 decade ago by dominic

Box2D doesn't distinguish between "entity vs. world" and "entity vs. entity" collisions like Impact's collision does.

You can iterate over all collisions an entity currently has, but to detect if the other "body" colliding with this entity is an entity as well, you'd have to store some extra information for the body, I believe.

So you could save a reference to the entity the body belongs to in your Entity's init() method (untested!):
init: function( x, y , settings ) {
	this.parent( x, y, settings );
	
	this.body.entity = this;
}

You can then use this reference when iterating over the contacts for this entity (partly taken from this thread):
update: function() {
	// Iterate over all contact edges for this body. m_contactList is 
	// a linked list, not an array, hence the "funny" way of iterating 
	// over it
	for( var edge = this.body.m_contactList; edge; edge = edge.next ) {

		// Check if the other body for this contact is an entity
		if( edge.other.entity ) {
			// do something
		}
	}
	
	this.parent();
}

Also see the b2ContactEdge Documentation.

If you're using a ContactListener instead, the listener functions get a ContactPoint as argument. This ContactPoint has a reference to both Shapes and the Shapes in turn have a reference to the Body - which should have the reference to the Impact entity like you defined in the entity's init().

Hope that helps!

1 decade ago by Snowleopard

here's a couple of articles about box2d collisions that other users might find useful:
http://blog.sethladd.com/2011/09/box2d-collision-damage-for-javascript.html
http://blog.allanbishop.com/box-2d-2-1a-tutorial-part-6-collision-strength/

1 decade ago by Snowleopard

note that those links explain how contactListeners work in box2d, but the api is different in the box2d version used in impact. Anyway, I got it working by adding the following to ig.Box2DGame:

addContactListener: function() {
var listener = new b2.ContactListener();
listener.Add = function(point){
console.log('Add Contact');
}
listener.Persist = function(point) {
console.log('Persist Contact');
}
listener.Remove = function(point) {
console.log('Remove Contact');
}
listener.Result = function(point) {
console.log('Result Contact');
}

ig.world.SetContactListener(listener);
}


...and adding a call to that function after the world is created. Still trying to figure out what comes next...

1 decade ago by drewhjava

This would be helpful if we could see more code for this. I'm having trouble making making a simple character jump. Hard to do this if you don't know if he's standing on ground or not. Thanks.

1 decade ago by quidmonkey

I wrote an Impact Box2d Collision plugin a few months ago. Be aware this is for the slightly older version 2.0.2. But you may find it helpful.
Page 1 of 1
« first « previous next › last »