GameDemon Posted August 3, 2015 Share Posted August 3, 2015 I'm using Phaser 2.4.2 with P2 physics. I have a sort of jar shaped body containing some circular bodies (balls). I want to move the jar offscreen but the balls are colliding with the world bounds. I tried setting collideWorldBounds on the balls but this stops them colliding with the jar which I want. You can see a demo here: https://output.jsbin.com/vuyexo Click the red button to make the jar move. Uncomment `collideWorldBounds=false` in the 'balls' section to see my problem. Why is this happening and how can I make the balls collide with the jar body but not the world bounds when the jar is moved offscreen? Link to comment Share on other sites More sharing options...
GameDemon Posted August 4, 2015 Author Share Posted August 4, 2015 Lol I might have phrased this question a bit awkwardly, the TL;DR version is: How do I set a body to collide with other bodies, but not with the world bounds? Having no left+right world bounds is fine too. Link to comment Share on other sites More sharing options...
BdR Posted August 5, 2015 Share Posted August 5, 2015 Like I also answered on stackoverflow, you can set the P2 world bounds using setBoundsToWorld so like this://..game.physics.p2.gravity.y = 300;game.physics.p2.setBoundsToWorld(false, false, true, true); // left=false, right=false, top=true, bottom=trueAlternatively you could just make the P2 world larger, so for example make the width 2000 instead of 768.game.world.setBounds(0, 0, 2000, 1024);Btw as you can see here the world bounds are nothing more than 4 dummy P2.body objects. Link to comment Share on other sites More sharing options...
GameDemon Posted August 5, 2015 Author Share Posted August 5, 2015 Thanks a lot for helping me! By the way do you know why `body.collideWorldBounds=false` didn't work, or what it is for? Link to comment Share on other sites More sharing options...
BdR Posted August 6, 2015 Share Posted August 6, 2015 Thanks a lot for helping me! By the way do you know why `body.collideWorldBounds=false` didn't work, or what it is for? That is a good question, I also don't know why this didn't work.. According to the comments in p2/Body.js the collideWorldBounds property should do exactly what you are trying to accomplish here. One thing though, shouldn't you also add each ball to the balls group? From looking at the code in Body.js, a body will either collide with the world bounds or with a group of bodies as set with the .collides() function. So I would expect you'd have to use collides() explicitly between the jar and the balls. So something like this, however the code below also doesn't work.// ..for (var i = 0; i < 9; i++){ var ball = balls.create(250+(i%3)*128,300+Math.floor(i/3)*128); ball.body.setCircle(64); ball.body.debug = true; ball.body.collideWorldBounds = false; balls.add(ball); // add to group}// jar collides with ballsjar.enableBody = true;jar.physicsBodyType = Phaser.Physics.P2JS;jar.body.collides(balls); Link to comment Share on other sites More sharing options...
Recommended Posts