# p2 physics overlap?

## Recommended Posts

In version 1.1.6 it was possible to use overlap in collisions.

There is something similar in the physics system p2?

Now I use to define collision the "collides" function:

collides (group, callback, callbackContext, shape)

The problem is that the player can walk over enemies for example.

##### Share on other sites

interesting would be how to overlap an arcade with an p2 body.

##### Share on other sites

If you use an Arcade object or the P2 postbroadphase trick, note that will only do overlap at the AABB collision level (ie a rectangle body). How to do precise overlap on polygons and circles still eludes us.

##### Share on other sites

so with a little bit of simple math you can "manually" determine if two bodies are overlapping..

i made a simple example for you to look at here:   disclaimer:  i guess this is very expensive since you check every object every frame - but i guess you could tweek it to only check visible objects so it would be more performant..

http://test.xapient.net/phaser/overlap2.html

THISONE WORKS WITH CIRCLES ONLY !

here is the code of the function:

`function checkOverlapManually(enemy) {    for (var i =0 ; i<enemies.length; i++){        var dx = ship.body.x-enemy.body.x;  //distance ship X to enemy X        var dy = ship.body.y -enemy.body.y;  //distance ship Y to enemy Y        var dist = Math.sqrt(dx*dx + dy*dy);     //pythagoras ^^  (get the distance to each other)        if (dist < shipdiameter+bulletdiameter){  // if distance to each other is smaller than both radii together a collision/overlap is happening            dosomething(enemy);        }    }}`
##### Share on other sites

Interesting.

I would like phaser had overlap with P2 system.

Thank you!

##### Share on other sites

interesting indeed..

i looked into it... the "intersects" function is also available for circles - it does the same thing i did in my function checkOverlapManually(enemy)   (but maybe more efficient )

it is also possible to use "intersectsRectangle"  to check if a circle and a rectangle overlap..   nice..  this should do it in most cases..

##### Share on other sites

Broadphase ? Could you explain more ?

##### Share on other sites

Rich has not yet implemented in Phaser, is not whether it would be useful.

overlap,if you uses a Box,p2.Broadphase.aabbCheck(bodyA,bodyB)bodyA overlap bodyB.

for example two bodies:

//update

var bodyA=game.physics.p2.getBody(sprite1),

bodyB=game.physics.p2.getBody(sprite2);

console.log("ok");

}

sorry for the code as shown and I hope to serve you

##### Share on other sites

You can simply use isSensor true to get overlap with p2, works with any convex polygon shape.

To clarify if you are loading shapes from a file you will have something like this:

`{      "someShape": [    {      "isSensor": true, //will make the shape overlap rather then collide      "filter": {        "group": 1,        "categoryBits": 2, //some category the shape belong to        "maskBits": 1 //what other catagories this shape will react with      },      "polygons":[         //polygon data....              ]         }  ]    }`

This is usually automatically generated using a tool like http://www.codeandweb.com/physicseditor you can use the Phaser exporter along with it, it can be found here

Just put it inside your Physics Editor folder (\PhysicsEditor\Exporter\phaser)

Or if you want to create simple shape, I believe you can directly set the sensor property to true on the shape you create, like this:

`var shape = new p2.Circle(...);shape.sensor = true;`
##### Share on other sites

thats a nice way to achive overlaps but how would a body that "isSensor"  ever collide ?  let's say i want my player to overlap with one thing but collide with the other thing..    and the one thing should also collide with all other things except the player...

##### Share on other sites

If you don't need to know when an object overlap, you can use the categoryBits / maskBits system, you can say which categories you want to collide with. If you need to know when you collide and when you overlap, the only thing i can think about would be to have 2 identical shape, one that is a sensor and one that is not. This may decrease your performance greatly thought.

##### Share on other sites

....

....

Or if you want to create simple shape, I believe you can directly set the sensor property to true on the shape you create, like this:

`var shape = new p2.Circle(...);shape.sensor = true;`

So once you have the P2 sprite set as a Sensor, what function do you use to actually handle the overlap event?

##### Share on other sites

So once you have the P2 sprite set as a Sensor, what function do you use to actually handle the overlap event?

Same as for collision

`this.body.onBeginContact.add(this.overlap, this);`