kpded

Get MatterJS collision point and normal

Recommended Posts

Hello!

I'm trying to identify the place and normal vector of collision to launch a bunch of particles when two bodies collide in MatterJS.

So following example on collision filtering https://labs.phaser.io/edit.html?src=src\physics\matterjs\collision event.js

I have something like

function create ()
{
    var blockA = this.matter.add.image(0, 300, 'block').setBounce(1).setFriction(0);

    var blockB = this.matter.add.image(600, 300, 'block').setStatic(true);

    blockA.setVelocityX(10);

    this.matter.world.on('collisionstart', function (event, bodyA, bodyB) {

        // launch particles here at collision point

    });
}

But unfortunately 'collisionstart' event provides just bodies of two gameObject collided and no information on where the collision took place

So is there any way to detect where did the collision happen and what was the normal direction? Or perhaps one can access MatterJS collision object directly, as far as I can see it contains this information?

I'm using MatterJS because I need to collide non-convex polygons, so if you have any possible alternative in mind I would appreciate if you share it with me

Share this post


Link to post
Share on other sites

Thank you @rich

Yeah this data doesn't contain any information on collision point

But matterjs calculates it anyway in Engine.update(), just doesn't pass with event. It can be found here  

https://github.com/photonstorm/phaser/blob/c6faa7c8a7f1dac9a06a629458c0c05f4eaa6602/src/physics/matter-js/lib/core/Engine.js#L192

It feels like we need to pass collisions array along with the pairs. A collision from that collisions array contains 'normal' and 'supports' members, that are a normal vector of the collision and points (may be one or two) that describe a collision point

Changing this directly in phaser.js file gives a result that looks decent at a glance (see gif attached)

I do understand that this can't be a robust solution, so I'm trying to wrap my head around overwriting Phaser.Physics.Matter.Matter.Engine.update() method. With no luck so far: I can't understand how can I get to the scope of the original function

So if anyone can advise me on a way to overwrite that function, it would be awesome

Peek 2018-10-09 11-18.gif

Share this post


Link to post
Share on other sites

People at matter repo helped me to solve this issue without changing the source.

The idea is that one can access all pairs of colliding objects through the array this.matter.world.engine.pairs.list at every update frame

Every member of this array contains bodies of colliding objects and 'collision' object. The 'collision' object in turn contains that 'normal' and 'supports' properties, that I needed

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.