Jump to content

Turning P2 Physics On and Off


RBrNx
 Share

Recommended Posts

In the game that I am currently making using Phaser and P2 Physics I am looking to turn off the collisions between 3 objects when the game is paused or when the level has ended and the scoreboard shows so that unnecessary collisions are not being checked and dropping the frame rate (game is intended to run on mobile devices).

 

I am using the following two functions at the moment

TurnOffCollisions: function() {        console.log("TurnOffCollisions")        SavedBallVelX = Ball.body.velocity.x;        SavedBallVelY = Ball.body.velocity.y;        Ball.body.velocity.x = 0;        Ball.body.velocity.y = 0;        this.game.physics.p2.gravity.y = 0;        FairwayHole.body.clearCollision();        Ball.body.clearCollision();        FairwayHole.body.clearShapes();        Ball.body.clearShapes();        Block.body.clearCollision();        Block.body.clearShapes();    },

And to turn on the Collisions again when the game is unpaused or the next level starts:

    TurnOnCollisions: function() {        console.log("TurnOnCollisions");        this.game.physics.p2.enable(FairwayHole);        this.game.physics.p2.enable(Ball);        this.game.physics.p2.enable(Block);        FairwayHole.body.loadPolygon("Physics", "Level1-Hole");        FairwayHole.kinematic = true;        Ball.body.loadPolygon("Physics", "Ball2");        Ball.body.velocity.x = SavedBallVelX;        Ball.body.velocity.y = SavedBallVelY;        Block.body.static = true;        this.game.physics.p2.gravity.y = 1400;        ballMaterial = this.game.physics.p2.createMaterial("ballMaterial", Ball.body);        groundMaterial = this.game.physics.p2.createMaterial("groundMaterial", FairwayHole.body);        this.game.physics.p2.setWorldMaterial(groundMaterial, true, true, true, true);        contactMaterial = this.game.physics.p2.createContactMaterial(ballMaterial, groundMaterial);        contactMaterial.friction = 0.5;        contactMaterial.restitution = 0.5;    }

However when I do this it seems to work OK, until the Level Finishes and the Ball object and FairwayHole object are still touching, even though I think I have turned off the Physics I get the following error:

Uncaught TypeError: Cannot read property 'collisionMask' of undefined54.c.runNarrowphasephaser.js:76867 54.c.internalStepphaser.js:76685 54.c.stepphaser.js:78255 c.Physics.P2.updatephaser.js:61785 c.Physics.updatephaser.js:26388 c.Game.updateLogicphaser.js:26327 c.Game.updatephaser.js:44881 c.RequestAnimationFrame.updateRAFphaser.js:44865 c.RequestAnimationFrame.start.window.requestAnimationFrame.forceSetTimeOut._onLoop

Is there a better way of turning on and off collisions using P2 Physics? Or Am I doing something wrong with the way I am doing it that is causing the game to crash?

Link to comment
Share on other sites

  • 1 month later...
 Share

  • Recently Browsing   0 members

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