P2 Physics get onBeginContact point of collision/overlap between collision bodies/shapes

Recommended Posts

Hey all!

So i've been working on a topdown style game using the P2 physics system. I've really enjoyed working with Arcade physics, but needed to switch to P2 to utilize polygon body shapes and polyline world collision maps.

I'm trying to implement a "pit" object and currently have things working as I'd like (collision-wise) with the player in the following setup:

- The pit is an object with a polygonal body I define in tiled and import into the game.

- The pit body shapes are set as sensors to disable default P2 collision behavior.

- When the player contacts the "pit" shape its velocities are stopped and it enters a flail state on the edge of contact.

- If the players flail timer runs out it will fall into the pit.

What I'm having trouble with is figuring out how to determine the correct angle for the player to fall (into the pit) once the flail timer runs out. The player has 8 way movement and the pits are generally laid out as polygonal shapes with right angles.

I looked around the boards and found talk of getting the centers of mass for the objects and calculate the vector angle between them and thought this might work, but can't seem to find a way to make it happen.

I thought if I could get some point where the bodies/shapes collided I could jut calculate the angle between the player origin and the collision point, but can't find any way to actually get this point.

Is this data available?

Share on other sites

• 2 weeks later...

In case anyone was curious on my solution I ended up solving it using the following method:

I created an objects layer in tiled for pits which are defined as polygons. I also created a "pit hole" layer which is a tile layer that contains tiles that cover the inner areas of the defined polygon pits as best possible.

When there is a P2 collision between the player and the pit polygon, I have the player enter a "flail state." If the player needs to fall into the pit, I first get the tile the player is standing on and then check the adjacent tiles (in order of closes to current player facing) looking for any tile that is a "pit hole" tile. Once found, I have the payer fall towards the center of this tile which is easily calculated by tile position and tile dimensions. Since this calculation is only performed once before each "falling", the extra map performance is negligible.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.