Skip to content

Commit 9b6ffc3

Browse files
committed
Moved Springs to not extend p2 Springs as they break when added to the p2.World. Springs and spring removal now working properly (phaserjs#1134)
1 parent 26a55bd commit 9b6ffc3

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/physics/p2/RotationalSpring.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ Phaser.Physics.P2.RotationalSpring = function (world, bodyA, bodyB, restAngle, s
4444
damping: damping
4545
};
4646

47-
p2.RotationalSpring.call(this, bodyA, bodyB, options);
47+
/**
48+
* @property {p2.RotationalSpring} data - The actual p2 spring object.
49+
*/
50+
this.data = new p2.RotationalSpring(bodyA, bodyB, options);
4851

4952
};
5053

51-
Phaser.Physics.P2.Spring.prototype = Object.create(p2.RotationalSpring.prototype);
5254
Phaser.Physics.P2.Spring.prototype.constructor = Phaser.Physics.P2.Spring;

src/physics/p2/Spring.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ Phaser.Physics.P2.Spring = function (world, bodyA, bodyB, restLength, stiffness,
6565
options.localAnchorB = [ world.pxm(localB[0]), world.pxm(localB[1]) ];
6666
}
6767

68-
p2.LinearSpring.call(this, bodyA, bodyB, options);
68+
/**
69+
* @property {p2.LinearSpring} data - The actual p2 spring object.
70+
*/
71+
this.data = new p2.LinearSpring(bodyA, bodyB, options);
6972

7073
};
7174

72-
Phaser.Physics.P2.Spring.prototype = Object.create(p2.LinearSpring.prototype);
7375
Phaser.Physics.P2.Spring.prototype.constructor = Phaser.Physics.P2.Spring;

src/physics/p2/World.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,19 @@ Phaser.Physics.P2.prototype = {
764764
* Adds a Spring to the world.
765765
*
766766
* @method Phaser.Physics.P2#addSpring
767-
* @param {Phaser.Physics.P2.Spring} spring - The Spring to add to the World.
767+
* @param {Phaser.Physics.P2.Spring|p2.LinearSpring|p2.RotationalSpring} spring - The Spring to add to the World.
768768
* @return {Phaser.Physics.P2.Spring} The Spring that was added.
769769
*/
770770
addSpring: function (spring) {
771771

772-
this.world.addSpring(spring);
772+
if (spring instanceof Phaser.Physics.P2.Spring || spring instanceof Phaser.Physics.P2.RotationalSpring)
773+
{
774+
this.world.addSpring(spring.data);
775+
}
776+
else
777+
{
778+
this.world.addSpring(spring);
779+
}
773780

774781
this.onSpringAdded.dispatch(spring);
775782

@@ -786,7 +793,14 @@ Phaser.Physics.P2.prototype = {
786793
*/
787794
removeSpring: function (spring) {
788795

789-
this.world.removeSpring(spring);
796+
if (spring instanceof Phaser.Physics.P2.Spring || spring instanceof Phaser.Physics.P2.RotationalSpring)
797+
{
798+
this.world.removeSpring(spring.data);
799+
}
800+
else
801+
{
802+
this.world.removeSpring(spring);
803+
}
790804

791805
this.onSpringRemoved.dispatch(spring);
792806

0 commit comments

Comments
 (0)