Jump to content

P2 create and remove springs


spencerTL
 Share

Recommended Posts

I have been having trouble removing a P2 spring once created and could only find this github thread about it where it remained unresolved. https://github.com/photonstorm/phaser/issues/974

 

After some experimenting I have found that creating the spring as shown in the examples and docs results in a spring that I can't remove but by using a different creation method it can be removed. For eg.

    var spr = this.game.physics.p2.createSpring(b1.body, b2.body, 150, 0.15, 0.05);    console.log("getSprings():");    console.log(this.game.physics.p2.world.springs.length) //returns 1  	this.game.physics.p2.removeSpring(spr);     console.log("b getSprings():");    console.log(this.game.physics.p2.world.springs.length) //returns 1

the spring just won't go away.

 

EDIT:I've removed a solution that wasn't a solution in the end.  

Link to comment
Share on other sites

I think I've genuinely got it this time.  To remove the spring, its parent needs to be passed in removeSpring method.  I can't say I quite understand this, nor why the getSprings() method produces an array of nulls equal in length to the number of springs but I've always been slow on the uptake with physics systems.  My solution:

//assuming P2 physics all set up first and two sprites with bodies: b1 and b2.//add the springvar spr = this.game.physics.p2.createSpring(b1.body, b2.body, 150, 0.15, 0.05);//remove the springthis.game.physics.p2.world.removeSpring(spr.parent);

Could anyone explain why this works this way or offer a better way?

Link to comment
Share on other sites

well.. IMHO this shouldn't work.. 

 

when i create a constraint for example i do the following:

swordRC = game.physics.p2.createRevoluteConstraint(sword , [0,0],player, [0,8], 10000

and to remove the constraint of course:

game.physics.p2.removeConstraint(swordRC);

the parent of the swordRC is my game world... 

Link to comment
Share on other sites

  • 1 month later...
 Share

  • Recently Browsing   0 members

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