0

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.

Luca
  • 558
  • 4
  • 18
  • possible duplicate of [famo.us access to collisionData upon collision event](http://stackoverflow.com/questions/27714976/famo-us-access-to-collisiondata-upon-collision-event) – talves Feb 23 '15 at 15:36

3 Answers3

1

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);
Kraig Walker
  • 812
  • 13
  • 25
  • Thanks, but this is not wha I am looking for. Actually, I already did that. I have about 30 bodies which collide with each other. The problem is that as soon as they collide, I want to stop them. So I need to listen on the 'collision' event. But that gives me, as explained above, only access to one of the collision "agents", where as I need the other body too to decide what to do next. – Luca Feb 23 '15 at 10:22
1

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

Community
  • 1
  • 1
Kraig Walker
  • 812
  • 13
  • 25
0

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.

Answer:

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;
});
talves
  • 13,993
  • 5
  • 40
  • 63