Robert O'Rourke

Best way to recreate old physics.overlap() using P2

Recommended Posts

Hi,
 
I've just started porting a bunch of games from version 1.1.6 and I'm struggling to work out how to get the same effects using P2 that were pretty straight forward with the Arcade physics engine.
 
Because setCircle() has been removed from the Arcade engine I need to use P2 to make sure my collisions are correct.
 

Here's a snippet from my update loop previously where I was checking if a radar 'blip' overlapped any of the other blips in the this.blips array: 

this.game.physics.overlap( plane.blip, this.blips, function( blip1, blip2 ) {	this.onCrash();}, function( blip1, blip2 ) {	return blip1.group.alive && blip2.group.alive;}, this ); 

So I'm looking at using body.onBeginContact.add() but that seems to cause the sprites to start interacting with each other which I don't want. Any suggestion on how to recreate the above?

 

The physics.overlap method before was so simple!

Share this post


Link to post
Share on other sites

This is how i manage it to work :)

group = game.add.group();goup.enableBody = true;group.physicsBodyType = Phaser.Physics.ARCADE;sprite = game.add.sprite(world.centerX, world.centerY, 'sprite');game.physics.enable(sprite, Phaser.Physics.ARCADE);game.physics.arcade.overlap(sprite, group, this.collect);

Share this post


Link to post
Share on other sites

have a look at the   postbroadphase callback example

it looks like you could decide in the callback function if it collides or not and maybe call another function from there to realise something similar..  (but it's still not a real overlap because as far as i understood it could still MISS the other object in the narrow phase )

 

http://examples.phaser.io/_site/view_lite.html?d=p2%20physics&f=postbroadphase+callback.js&t=postbroadphase%20callback

Share this post


Link to post
Share on other sites

Ok so it seems that P2 may not be ideal for this. I loved the simplicity of the arcade physics engine and the setCircle method, they worked perfectly for me.

 

I'm trying now to use the NINJA physics engine as that correlates much more closely with the way Arcade was working in v1.1.6 eg. it has the same overlap and collides methods and it also is more suitable for mobile device CPUs.

 

I'm finding all sorts of problem with NINJA compared to Arcade though especially when using groups and colliding sprites within those groups.

 

I'll either have to dig through and try to patch up NINJA or beg Rich to reconsider adding setCircle and setPolygon back into the Arcade physics engine!

Share this post


Link to post
Share on other sites

 

This is how i manage it to work :)

group = game.add.group();goup.enableBody = true;group.physicsBodyType = Phaser.Physics.ARCADE;sprite = game.add.sprite(world.centerX, world.centerY, 'sprite');game.physics.enable(sprite, Phaser.Physics.ARCADE);game.physics.arcade.overlap(sprite, group, this.collect);

 

Sorry Arlefreak I should have said I specifically need to use circle collision detection which the Arcade engine no longer has.

Share this post


Link to post
Share on other sites

like i wrote before (quote on the bottom)  i think the broadphase function can be used..   i made an example for you to look at

here: http://test.xapient.net/phaser/overlap.html  

 

if you fly into the other object..  it fires a function (change the spirte)  but it does not collide..  maybe that's good enough for now ?

 

 

 

have a look at the   postbroadphase callback example

it looks like you could decide in the callback function if it collides or not and maybe call another function from there to realise something similar..  (but it's still not a real overlap because as far as i understood it could still MISS the other object in the narrow phase )

 

http://examples.phaser.io/_site/view_lite.html?d=p2%20physics&f=postbroadphase+callback.js&t=postbroadphase%20callback

Share this post


Link to post
Share on other sites

Okay – I can see the postbroadphase func could be useful in creating something similar to overlap in P2; however, it p2 doesn't seem to recognise correctly polygons using this method. 

 

I've set up an example here: http://picturesandwriting.com/examples/p2-polygon-overlap/

 

If you inspect with the console you can see that the overlapping call is constantly firing even if it's not touching the polygon area. It's firing if it's within the sprite's enclosing rectangle – as soon as it's out of it's rectangle it stops firing. 

Share this post


Link to post
Share on other sites

I think that's because P2 uses a two stage collision detection.

Stage one is Broadphase which does the simple/quick rectangle(?) tests to see if their is a *potential* collision. If true, then proceed to stage two.

Stage two is Narrowphase which takes the paired objects and runs the expensive/slower math to confirm the collision. If this returns true only then is it really a collision.

If you return false in postbroadphase then its short circuiting collision detection and only doing stage one, meaning you are triggering overlap for a potential collision which might not truely be a collision (if stage two occurred). For simple rectangle objects this approach is probably OK because broadphase is accurate enough, but for complex polygons or rotated objects I don't think this will work.

Have a read of this:

http://ianqvist.blogspot.com.au/2010/07/broad-and-narrow-phase-collision.html

Also see the inline comments in the Postbroadphase Callback example:

// To explain - the post broadphase event has collected together all potential collision pairs in the world

// It doesn't mean they WILL collide, just that they might do.

// This callback is sent each collision pair of bodies. It's up to you how you compare them.

// If you return true then the pair will carry on into the narrow phase, potentially colliding.

// If you return false they will be removed from the narrow phase check all together.

// In this simple example if one of the bodies is our space ship,

// and the other body is the green pepper sprite (frame ID 4) then we DON'T allow the collision to happen.

// Usually you would use a collision mask for something this simple, but it demonstates use.

Share this post


Link to post
Share on other sites

I think that's because P2 uses a two stage collision detection.

Stage one is Broadphase which does the simple/quick rectangle(?) tests to see if their is a *potential* collision. If true, then proceed to stage two.

Stage two is Narrowphase which takes the paired objects and runs the expensive/slower math to confirm the collision. If this returns true only then is it really a collision.

 

Ah thanks for the explanation; however, using the docs I can't see a way to override the default Narrowphase behaviour with my own 'overlap' method. 

 

Cheers

Share this post


Link to post
Share on other sites

Ok, you can kinda get Overlap using the STATIC motionState, ie p2sprite.body.motionState = 2; //STATIC

And then you can use onBeginContact and onEndContact.

There are some odd effects though, as the STATIC object still wants to impart physics into the moving object it seems.

 

After passing through you may get some spinning/rotation. That can be stopped easily enough with body.angularVelocity = 0 and body.rotation = 0;

But there is some jumpy movement/velocity happening, mainly when you enter a complex polygon or through the centre of another shape like a box or circle.

 

I don't have a place to host code, but I've attached how I modified the "contact events" P2 example which you can test on your local.

Try moving the block through the contra, wizball, pink tetris or block2 and you'll see what I mean by jumpy movement.

This can probably be overcome with some tweaking to the STATIC object I guess?...

 

One other note, you have to set motionState = 2 after you clearShapes() and addPolygon/loadPolygon/addCircle/etc otherwise it wont work.

 

(File renamed with .txt suffix to allow attaching to this post)

 

 

contact events.js.txt

Share this post


Link to post
Share on other sites

shouldnt motion state 2.. aka static behave like any normal physics body and properly collide with everything that runs into it??

i thought its a bug that you can almost run through those static objects at the moment..

setting the mass of the static object to something high like 1000 solves this problem and you get static objects where you can jump on.. (floating platform for example)

Share this post


Link to post
Share on other sites

ok, well if moving through a STATIC object is a bug, then its not a viable option for overlap.

 

TBH its not exactly clear what STATIC does with regard to other objects.

From the P2 doco. http://schteppe.github.io/p2.js/docs/classes/Body.html:

// This body will move and interact with other bodiesvar dynamicBody = new Body();dynamicBody.motionState = Body.DYNAMIC;// This body will not move at allvar staticBody = new Body();staticBody.motionState = Body.STATIC;// This body will only move if you change its velocityvar kinematicBody = new Body();kinematicBody.motionState = Body.KINEMATIC;

 

For DYNAMIC it explicitly states it will "...interact with other bodies", but for STATIC it doesn't say anything in that regard.

KINEMATIC sounds like a STATIC object that you can move only by explicitly setting its velocity in code not via an interaction with another body.

But from my brief testing that isn't true, as hitting a KINEMATIC object makes it bounce away. I've not yet noticed a difference between DYNAMIC and KINEMATIC.

 

I guess this is all part of the fun of jumping in early with Phaser2 + P2. There is obviously some things to sort out.

 

 

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.