Jump to content

P2 Increase body radius over time


BigRob154
 Share

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?

Link to comment
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!
Link to comment
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();
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...