BigRob154

P2 Increase body radius over time

Recommended Posts

Hi :)

 

I'm working on a prototype and have some problems with P2. I've some bubbles in the scene which grow over time. When changing the body's radius in the update function the whole scene gets out of control and the bubbles go crazy, jumping around everywhere.

    create: function() {        this.game.physics.startSystem(Phaser.Physics.P2JS);        for (var i = 0; i < 10; i++) {            var x = (Math.random() * this.game.width) | 0,                y = (Math.random() * this.game.height) | 0,                d = 30;            var s = this.game.add.sprite(x, y, '');            s.anchor.set(0.5, 0.5);            this.game.physics.p2.enable(s);            var p2Shape = s.body.addCircle(d, 0, 0, 0);            s.body.debug = true;            this.objects.push({                sprite: s,                physicShape: p2Shape            });        }    },    update: function() {        for (var i = 0; i < this.objects.length; i++) {            var o = this.objects[i];            // same result            //o.sprite.body.setCircle(o.physicShape.radius + 0.1, 0, 0, 0);            o.physicShape.radius += 0.1;            o.physicShape.boundingRadius = o.physicShape.radius;        }    }

Am I doing something terribly wrong here?

Share this post


Link to post
Share on other sites

I think you might be updating the physics shape too fast. They grow to something that cover the whole physics world, and the contact solver goes bananas.

 

Try setting 

o.physicShape.radius += 0.005;

instead, and you might have a chance to see what's happening.

 

Note that the physics debug rendering of the sprite does not get updated automatically. I tried to do

o.sprite.body.debugBody.draw();
and it works but that will also randomize the color on the debug rendering each frame... Disco!

Share this post


Link to post
Share on other sites

Yep, it's not bouncing around like crazy anymore. Didn't know that I've to call debugBody.draw() manually, thanks for the hint.

But there is another problem, now. The whole simulation gets unstable. Bubbles are overlapping each other und shoot out like crazy. Looks like the collision isn't always triggered.

            o.physicShape.radius += 0.005;            o.physicShape.updateBoundingRadius();            o.sprite.body.debugBody.draw();

Share this post


Link to post
Share on other sites

Okay... Do they overlap at the end of the simulation, or when the bubbles have grows to cover the screen, or in the beginning, or all the time?

I run the latest Phaser and I'm not sure I get the same result.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.