Jump to content

p2 physics overlap?


AlexArroyoDuque
 Share

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.

Link to comment
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);        }    }}
Link to comment
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..

Link to comment
Share on other sites

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

ok.. mmm... you can use p2.Broadphase.boundingRadiusCheck(bodyA,bodyB)radius of two bodies

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);

if(p2.Broadphase.aabbCheck(bodyA,bodyB)){

console.log("ok");

}

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

Link to comment
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;
Link to comment
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.

Link to comment
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?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...