question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Per-body events: addedToWorld, removedFromWorld, collision, collisionWith

See original GitHub issue

Plop!

I think it would be cool to be able to listen to events fired by bodies themselves. For example, the following events could be fired:

'addedToWorld' 
  -> { body : someBody, world : /* World it has been added to */ }
'removedFromWorld' 
  -> { body : someBody, world : /* World it has been removed from */ }
'collision' 
  -> { body : someBody, pair : /* Pair of the collision */ }
'collisionWith{{Someone Else}}'
  -> { body : someBody, pair : /* Pair of the collision */ }

The motive behind this is that, when coding a game in which you control a square (a single body), I failed to find an easy way to observe what was happening to the player: Did he collide with something? Did he collide with [this thing]? I was able to do so with the following:

Matter.Events.on(engine, 'collisionActive', function(e) {
  var i, pair,
      length = e.pairs.length;
  for(i = 0; i < length; i++) {
    pair = event.pairs[i];
    if(!(pair.bodyA.label === 'Player' || pair.bodyB.label === 'Player')) {
      continue;
    }
    //Here body with label 'Player' is in the pair, do some stuff with it
  }
});

So to watch a single body I was forced to watch every single collision pair only to find the one I was interested in. Thus, maybe the following would be better:

Matter.Events.on(player.body, 'collision', function(e) {
  //player.body is in e.pair, do stuff with it
}

Or:

Matter.Events.on(player.body, 'collisionWith{{Exit}}', player.win);

The event collisionWith being particular because it would be the first event to my knowledge that can have a parameter.

All of this could be further used with Composites.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
vkovescommented, Feb 23, 2017

@liabru - I’m jumping onto this a bit late (since I am working on something using MatterJS), but from my experience in game development I would think collision events would make perfect sense within the scope of a physics engine. It is a core component of the simulation of the physics simulation, rather than graphics or AI type things that would be more typical of a game engine. For a game engine to be built on top of a physics engine, the physics engine has to communicate all events to the game engine, and that definitely includes collisions.

I’d also cite this wonderful gamedev StackExchange post on this as a reference, specifically:

The game engine may respond to events from the physics engine. The physics engine might raise an event when two objects collide, or when two objects that were touching separate…

2reactions
Araluncommented, Feb 23, 2015

I like the “Keep it clean and nice and let people write their own piggy stuff around” way of doing things.

For now, here’s where I am:

Matter.Events.on(engine, 'collisionActive', function(event) {
  var i, pair,
      length = event.pairs.length;

  for (i = 0; i < length; i++) {
    pair = event.pairs[i];
    if (!(pair.bodyA.label === 'Player' || pair.bodyB.label === 'Player')) {
      continue;
    }
    Matter.Events.trigger(player.body, 'collision', { pair : pair });
  }
});

With the associated listener of course.

I also tried to make Events fire from a closure-wrapped Matter.Pair, failed miserably because no one cares about Matter.Pair specifically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

matter-js from liabru - Giter VIP
Per-body events : addedToWorld, removedFromWorld, collision, collisionWith. Plop! I think it would be cool to be able to listen to events fired by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found