# Passing through walls instead of bouncing off when going fast using p2 physics

## Recommended Posts

Hi guys,

I am having some trouble using p2 physics and collision groups. I have walls and a ball that can be flung around with the mouse and bounces off the walls. However when the ball is going fast it simply passes through the walls instead of bouncing off them.

Any idea what the problem may be?

##### Share on other sites

jep...   tunneling is something completely normal when working without continuous collision detection..

http://www.stencyl.com/help/view/continuous-collision-detection/

to avoid it use a function to limit the maximum speed of your moving objects or use thicker walls, or bigger moving objects OR  use walls made out of objects instead of polylines... it works much better (i already filed a github issue concerning objects VS poligons

##### Share on other sites

and here one approach to limit the maximum speed   (run this on every frame)

`function constrainVelocity(sprite, maxVelocity) {    if (!sprite || !sprite.body) {return;}    var body = sprite.body    var angle, currVelocitySqr, vx, vy;    vx = body.data.velocity[0];    vy = body.data.velocity[1];    currVelocitySqr = vx * vx + vy * vy;    if (currVelocitySqr > maxVelocity * maxVelocity) {        angle = Math.atan2(vy, vx);        vx = Math.cos(angle) * maxVelocity;        vy = Math.sin(angle) * maxVelocity;        body.data.velocity[0] = vx;        body.data.velocity[1] = vy;    }};`
##### Share on other sites

Schteppe, the creator of p2, had the idea to integrate CCD very early and is aware of the issue. Unfortunately CCD is still missing. Maybe you should raise your voice in the original issue opened by schteppe himself. https://github.com/schteppe/p2.js/issues/28

Box2D is another physics engine which has CCD builtin. It's is not available in Phaser yet. It will be available as a Premium Plugin. See this announcement. http://www.photonstorm.com/phaser/phaser-v2-1-3-and-pixi-v2-are-out

If you're open for adventures you could try to integrate Box2D yourself in Phaser. I successfully tried it before and finished it within some hours for a limited scope. It is based on the box2d-html5(https://code.google.com/p/box2d-html5/) port of Box2D.

https://georgiee.github.io/phaser+box2d/

Regards George

##### Share on other sites

wow..  box2d integration..   it looks like box2d does one thing better than p2 :  every body that falls on the floor stops exactly above the floor while with p2 physics the bodies always go into the other bodies (the floor) and then move out of them quickly..

##### Share on other sites

wow..  box2d integration..   it looks like box2d does one thing better than p2 :  every body that falls on the floor stops exactly above the floor while with p2 physics the bodies always go into the other bodies (the floor) and then move out of them quickly..

I don't think that's the case in his demo, looks to me as if the behaviour is pretty much identical.

##### Share on other sites

in HIS demo this isn't the case..  like i said.. the bodies stop before entering the floor.. this looks very good...  with p2 it's different..  try this demo: https://schteppe.github.io/p2.js/demos/collisions.html   the objects sink in to the floor first if you let them go in great height.. it feels like the floor is made out of something soft..

##### Share on other sites

No what I meant was, that in his demo it also happens, The speeds aren't as high, and the restitution and separation is a little different, but the shapes still overlap slighty.

##### Share on other sites

oh sorry.. i got it the other way around..

but still.. i don't see the behaviour in the box2d demo but i can always see it in the p2 demo.. i changed the gravity to something very low and created circles in the same height above the ground as in the box2d demo..  almost all circles overlapped..

##### Share on other sites

Try setting p2 restitution to 1.

##### Share on other sites

i cant do this in the example - can i ?

if this is the way to make it look like it doesn't sink into the ground i can not use it for obvious reasons..  but never mind..  it actually isnt a problem..   i just was surprised because it looks like (at leas to  me) that georges demo doesn't do this and i thought it's unavoidable

##### Share on other sites

Oh I'm sorry, it's relaxation

I can't really speak with any authority about the actual features of Box2D Just saw that there was overlap in George's example (which isn't a criticism btw)

##### Share on other sites

oh you mean relaxation.. this doesn't change the amount of overlap it just speeds up the correction..  with very hight relaxation you can see the overlap in slomo

i guess i need to play around with the box2d example a little bit more to make out the differences ....

do you know if there is any difference between p2 and box2d performancewise?

## 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.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.