schteppe / p2.js

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

[bug] Box collides with floor made of boxes. #173

Open AdrianNostromo opened 9 years ago

AdrianNostromo commented 9 years ago

Description: In a side scroller, if the player is a rectangle and moves over a floor made of identical rectangles, from time to time he will hit the edge of a floor rectangle and loose his velocity and get stuck. The player rectangle also gets stuck on polygon vertexes if they are in a straight line.

Reproduction: In the "Canvas: Character demo", drop 2 boxes on the floor one next to the other and walk over them a few times (about 20 times) and a box will suddenly move. I can make a recording if needed.

Problem: As far as I know this is a common problem with physics engines because the of the way the math is done. Box2D also has this problem but they have "Ghost Vertices" to fix it. (Article here: http://www.iforce2d.net/b2dtut/ghost-vertices ). Note: I tried the "clipping polygon corners" like in the article above and it still gets stuck. Note: using circle instead of a box fixes the problem buy doesn't work well for other things.

Possible fix: Because the collided area is very small, a easy fix may be to add a option to shapes (something like a "collisionThreshold") to ignore a small amount of collision (ignore 0.01 pixels) and just reposition the body that has the threshold but not affect it's velocities. This should work well.

schteppe commented 9 years ago

As I understood it, Box2D only got ghost vertices working for Edge shapes, correct?

Could you give me a more detailed algorithm description for your threshold? Will this make the ground boxes smaller (from the character bodys point of view) just before the character lands on a new box? I guess it needs to make the the previous box big again at some point, to overcome the edge of the next?

There is already a "threshold" parameter, but it is called "contactSkin" and it does not currently change over time. You could probably use it to make a prototype. http://schteppe.github.io/p2.js/docs/classes/ContactMaterial.html#property_contactSkinSize

schteppe commented 9 years ago

Btw I've already planned to make Edge and Chain shapes, with ghost vertex support. See https://github.com/schteppe/p2.js/issues/170

photonstorm commented 9 years ago

@schteppe am really enjoying seeing this sudden renewed interested in updating p2 again. It's a shame so much time passes between each 'wave', but I know too well how that goes! Keep up the good work! (and I look forward to edge chains massively, they'd be a dream for tilemap conversion)

schteppe commented 9 years ago

@photonstorm thanks for those words! Yep, the "waves" usually comes with projects. I wish I could work more continuously on my physics :)