Phaser P2 body.collideWorldBounds stops body collisions

I am using Phaser 2.4.2 with P2 physics. I have a kind of jar-shaped body containing several circular bodies (balls). I want to move the jar off-screen, but the balls are colliding with the world borders. I tried setting collideWorldBounds

on balls like this:

ball.body.setCircle(64);
ball.body.collideWorldBounds = false;

      

but it stops them from colliding with the jar I want - the result is gravity makes them descend on the screen.

You can see a demo here: https://output.jsbin.com/vuyexo
Press the red button to make the jar move.
Uncomment collideWorldBounds=false

in the "balls" section.

Why is this happening and how can I get the balls to collide with the body of the jar, but not the borders of the world when the jar is moved off-screen?

+3


source to share


2 answers


You can set the boundaries of the P2 world using setBoundsToWorld

this how to do it

//..
game.physics.p2.gravity.y = 300;
game.physics.p2.setBoundsToWorld(false, false, true, true); // left=false, right=false, top=true, bottom=true

      



Alternatively, you could just make the P2 world larger, for example make the width 2000 instead of 768.

game.world.setBounds(0, 0, 2000, 1024);

      

+3


source


By default, p2 collides with everything else, but if you want it to do something different, you need to explicitly specify which objects are colliding with each other by adding each body to the collision group and then calling body.collides like this:

        var jarCG = this.physics.p2.createCollisionGroup();
        var ballsCG = this.physics.p2.createCollisionGroup();
        jar.body.setCollisionGroup(jarCG);
        jar.body.collides(ballsCG);
        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;
            ball.body.setCollisionGroup(ballsCG);
            ball.body.collides([jarCG, ballsCG]);
        }

      



It works for me, but using animation to move the flask still makes the physics weird. You can fix this either by using jar.body.velocity.x instead of tweening, or by overtaking all the balls as well. You can also try tweening stack.x if you just want to move the sprites, but for some reason it doesn't move their bodies.

0


source







All Articles