schteppe / p2.js

JavaScript 2D physics library
Other
2.64k stars 330 forks source link

One way platforms in p2.js #123

Closed josimard closed 9 years ago

josimard commented 10 years ago

Hi, I am trying to figure out how I could make my character only collide with the up-side of a platform. (And also let the player drop down when the player is trying to go down)

I can imagine one-way shapes that would be useful to create collisions from one side only, is there something to achieve this already?

Right now, in the pre-solve phase, I am trying to cancel the collision but I can't figure out how to deal with the contacts nor use a normal that makes sense to create a working scenario. In Box2D, we had to deal with a contact manifold to give us a "local" normal of each shape boundaries to cancel the collision.

In Nape, there was an example to achieve just that, but I can't do it in p2.js: https://github.com/deltaluca/www.napephys.com/blob/gh-pages/samples/OneWayPlatforms/OneWayPlatforms.hx

It would be nice to see it in action in the "Character.html" example.

Cheers, Jo

schteppe commented 10 years ago

I think this can be done using beginContact and endContact events. On beginContact, check the direction of the contact normal. If the character is below the platform, cancel all contacts between character and platform until the next endContact event.

What do you think? Would this approach suffice?

josimard commented 10 years ago

Hi, by cancelling the contacts you mean by using World.disabledBodyCollisionPairs[] or there is another way?

schteppe commented 10 years ago

Don't think that would work. Setting enabled to false on all related equations in the preSolve event would probably work better. http://schteppe.github.io/p2.js/docs/classes/World.html#events

valueerrorx commented 10 years ago

http://www.html5gamedevs.com/topic/6148-the-moving-jumpthrough-platform-sum-up-thread-help-very-much-appreciated/

have a look here for inspiration.. if you are able to improve it - pls comment in this thread .. thx

schteppe commented 9 years ago

@josimard I added one way platforms to the character demo (examples/canvas/character.html): https://github.com/schteppe/p2.js/commit/4966305e4eab9dcd82f418873cd4b3d384b1d568 Might need some tweaking but it works :)