Per-body events: addedToWorld, removedFromWorld, collision, collisionWith
See original GitHub issuePlop!
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:
- Created 9 years ago
- Comments:13 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:
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:
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 aboutMatter.Pair
specifically.