# p2 Physics contact point between 2 bodies

## Recommended Posts

Is there a way of finding the point of contact between 2 bodies?

In some of the p2 demos it draws a line from body centre to contact, so clearly the data is in there somwhere, but I can't seem to find a way of extract it...

##### Share on other sites

Well, the hacked solution I've got to is that you can get the body positions of the "contacting" shapes

add a listener to the body.

``ball.body.onBeginContact.add(this.contactHandler);``

``````contactHandler: function(phaserP2Body, p2Body, shapeA, shapeB, equArray)
{
// as I'm working with circles I know the contact position will be r1/r2 between the body positions
// shapeA.body.parent.x, shapeA.body.parent.y
// shapeB.body.parent.x, shapeB.body.parent.y
}``````

##### Share on other sites

• 3 weeks later...

Just came accross the same issue and found this solution:

``````        public contactHandler(body: Phaser.Physics.P2.Body, bodyB /* : p2.Body*/, shapeA: p2.Shape, shapeB: p2.Shape, equation: p2.ContactEquation[]): void {

let pos = equation[0].bodyA.position;
let pt = equation[0].contactPointA;

let cx = this.game.physics.p2.mpxi(pos[0] + pt[0]);
let cy = this.game.physics.p2.mpxi(pos[1] + pt[1]);

console.log("collision at:" + cx + ", " + cy);
}``````

##### Share on other sites

Hi @Tom Atom, thanks for that, it definitely works (in my game at least). But, I've looked at the docs, and I'm not too clear how it works.

Apparently, mpxi is used to "Convert p2 physics value (meters) to pixel scale and inverses it" (http://phaser.io/docs/2.4.4/Phaser.Physics.P2.html#mpxi) which doesn't seem to be what I'm trying to do...

But, nonetheless a good find. Thanks for sharing!!

##### Share on other sites

On 23. 11. 2016 at 11:13 PM, charlie_says said:

and I'm not too clear how it works

Many physics engines use their own units - most often from real world like kilograms, meters, ... Games use usually pixels. So, there is need to convert it between game engine and physics engine.

Second problem is, that Phaser has coordinate system with origin in top left corner, while lot of other systems has it in lower left one. This is, why y coordinate has to be reversed and probably @rich has some trick, because I noticed, that x is reversed too - not only in P2 integration, but also in Box2D integration ... not sure why (makes things simplier?)