RBrNx Posted April 14, 2015 Share Posted April 14, 2015 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 momentTurnOffCollisions: 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._onLoopIs 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 More sharing options...
RBrNx Posted April 15, 2015 Author Share Posted April 15, 2015 Anyone got any ideas? Link to comment Share on other sites More sharing options...
sloth6 Posted May 17, 2015 Share Posted May 17, 2015 Bump. Wondering the same thing. Apparently it was available in an older dev version but doesn't seem to work anymore for me. http://www.html5gamedevs.com/topic/7193-disable-remove-phusics/ Link to comment Share on other sites More sharing options...
rich Posted May 18, 2015 Share Posted May 18, 2015 Body.enable is for Arcade Physics only. There is no way to turn off a P2 Body - you literally have to remove it from the P2 world entirely, and add it back in again when you need it again. Link to comment Share on other sites More sharing options...
Recommended Posts