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.

applyForce on collisionStart event

See original GitHub issue

Hi liabru, I have a question. How can I use “Matter.Body.applyForce” within “collisionStart” event ??

From this I can use “Matter.Body.translate”, but not “applyForce”.

I tried this but it does not work for me:

Events.on(engine, ‘collisionStart’, function (event) { for (var i = 0; i < event.pairs.length; i++) { var pair = event.pairs[i];

  if (pair.bodyB.label == 'regla' && pair.bodyA.label == 'dominoTableB18' ) {
     Matter.Body.applyForce(lemmon, { x: 0, y: 0 }, { x: 0.1, y: 0 });
  }

} });

Thanks!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nicksantancommented, Sep 7, 2016

I’m still having trouble getting this to work using the method you describe. Am I misunderstanding the logic?

Events.on(engine, 'beforeUpdate', function(event) {
    for (i = 0; i<bodiesToBeMovedNextFrame.length; i++){
        var theBody = bodiesToBeMovedNextFrame[i];
        Body.applyForce(theBody, { x: 0, y: 0 }, {x: 100, y: 100});
    }
});
Events.on(engine, 'collisionStart', function(event) {
    var pairs = event.pairs;
    for (var i = 0; i < pairs.length; i++) {
        var pair = pairs[i];
        if (pair.bodyA.label == 'outerHex' && pair.bodyB.label = 'outerHex'){
                bodiesToBeMovedNextFrame.push(pair.bodyA);
                bodiesToBeMovedNextFrame.push(pair.bodyB);
        }
    }
});
1reaction
liabrucommented, Aug 6, 2015

As it stands, forces get cleared in Engine.update right after the event is emitted, so they won’t ever get applied.

This probably should be changed, but for now you should cache the forces you need to apply in some place outside of the event or in some custom body property, then apply them in the next engine beforeUpdate event.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apply force on CollisionStart event - Phaser 3 - Discourse
what i'm trying to do is making a sensor that give force to an object when it collides. i've tried to apply force...
Read more >
Implementing bullet collisions in Matter.js for a shooting game
applyForce ( bullet, this.body.position, { x: Math.cos(this.body.angle) ... Events.on(engine, "collisionStart", event => { for (const {bodyA, ...
Read more >
Matter.js Physics Engine API Docs - brm.io
Events.on(Engine, " collisionStart ", callback). Fired after engine update, provides a list of all pairs that have started to collide in the current...
Read more >
RigidBodyComponent | PlayCanvas API Reference
Teleport an entity to a new world-space position, optionally setting orientation. Events. collisionend. Fired when two rigid bodies stop touching.
Read more >
Game object - Notes of Phaser 3 - GitHub Pages
applyForce (force); ... scene.matter.world.on('collisionstart', function (event, bodyA, bodyB) { // var pairs ... event.pairs : An array of collision pairs.
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