I know how I can listen on collisions:
collision.on('collision', function(e){
//e is one element involved in my collision
});
But how to I get both bodies involved? Only one body is passed to this function.
I know how I can listen on collisions:
collision.on('collision', function(e){
//e is one element involved in my collision
});
But how to I get both bodies involved? Only one body is passed to this function.
I'm not 100% sure about what you're trying to achieve, but in order to get two (or more) Physics Bodies to collide with each other you should apply()
the same collision object.
I like to think of collision objects as managers, which control who gets to collide with who. I've known some game engines to picture this differently, with collisions being purely events or types.
The most simplistic example I could give:
physics.attach(collision, myFirstFamousPhysicsBody);
physics.attach(collision, mySecondFamousPhysicsBody);
I think the answer you're looking for might already be here. And the good news is you were already onto the right thing :) Looking at the Famo.us source code (line 122), the Collision class emits pre/collision/post events along with a collisionData
object which contains the source and target bodies of the collision.
Here's an existing answer which demonstrates how to access this data in the collision event.
famo.us access to collisionData upon collision event
Hope this helps
Note: I flagged this as a duplicate and the moderators felt it was a different question, although you marked the answer pointing to a correctly answered duplicate.
The target
and source
returned are the Body
objects you attached to create collision.
collision.on('collision', function(e) {
var targetBody = e.target;
var sourceBody = e.source;
});