From aeb82dbee9b940f114e2c11934f66c95321db8e5 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Thu, 28 Aug 2014 00:58:50 +0100 Subject: [PATCH] World.createRotationalSpring will now let you create rotational springs. --- README.md | 6 ++-- build/config.php | 13 +------- src/physics/p2/RotationalSpring.js | 52 ++++++++++++++++++++++++++++++ src/physics/p2/Spring.js | 6 ++-- src/physics/p2/World.js | 32 +++++++++++++++--- 5 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 src/physics/p2/RotationalSpring.js diff --git a/README.md b/README.md index d8e877fdf..b826fd187 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ Version 2.1.0 - "Cairhien" - -in development- ### Updates -* Updated to the latest build of [p2.js](https://github.com/schteppe/p2.js/commit/d1c7a340c42e4d5d1d939fba5fd13c5e49d6abd2) +* Updated to [p2.js 0.6.0](https://github.com/schteppe/p2.js/commit/d1c7a340c42e4d5d1d939fba5fd13c5e49d6abd2) - this was an API breaking change, so please see the p2.js section of this change log specifically if you're using p2 in your game. * TypeScript definition updates to help fix for the `noimplicitany` option (thanks @Waog #1088) * All of the Pixi geom classes have been removed from the build file as they aren't needed (the Phaser.Geom classes overwrite them), saving some space in the process. @@ -91,12 +91,14 @@ Version 2.1.0 - "Cairhien" - -in development- * Group.swap() updates the Z index values properly (thanks @Blank101 #1090) * Device now recognises ChromeOS as a desktop (thanks @alvinsight @hilts-vaughan #1091) -### p2.js 0.6.0 Changes +### p2.js 0.6.0 Changes and New Features * DistanceConstraint signature changed to take the new localAnchors. * RevoluteConstraint signature changed to include worldPivot * P2.Body now uses the new Body.type value instead of Body.motionState, however as P2.Body already have a property called `type` we have left the `motionState` getter/setter in for now. * World.enableBodySleeping has been removed and replaced with World.sleepMode. +* Phaser P2.Springs are now LinearSprings by default. +* World.createRotationalSpring will now let you create rotational springs. #### Breaking changes diff --git a/build/config.php b/build/config.php index 01b092387..2c06ba211 100644 --- a/build/config.php +++ b/build/config.php @@ -24,18 +24,6 @@ echo " "; } - // Testing a build without these - pretty sure we're safe to ignore them (as we over-write them) - // - // - // - // - // - // "src/pixi/core/Point.js", - // "src/pixi/core/Rectangle.js", - // "src/pixi/core/Polygon.js", - // "src/pixi/core/Circle.js", - // "src/pixi/core/Ellipse.js", - echo << @@ -201,6 +189,7 @@ EOL; + diff --git a/src/physics/p2/RotationalSpring.js b/src/physics/p2/RotationalSpring.js new file mode 100644 index 000000000..99acfd4e7 --- /dev/null +++ b/src/physics/p2/RotationalSpring.js @@ -0,0 +1,52 @@ +/** +* @author Richard Davey +* @copyright 2014 Photon Storm Ltd. +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} +*/ + +/** +* Creates a rotational spring, connecting two bodies. A spring can have a resting length, a stiffness and damping. +* +* @class Phaser.Physics.P2.RotationalSpring +* @classdesc Physics Spring Constructor +* @constructor +* @param {Phaser.Physics.P2} world - A reference to the P2 World. +* @param {p2.Body} bodyA - First connected body. +* @param {p2.Body} bodyB - Second connected body. +* @param {number} [restAngle] - The relative angle of bodies at which the spring is at rest. If not given, it's set to the current relative angle between the bodies. +* @param {number} [stiffness=100] - Stiffness of the spring. A number >= 0. +* @param {number} [damping=1] - Damping of the spring. A number >= 0. +*/ +Phaser.Physics.P2.RotationalSpring = function (world, bodyA, bodyB, restAngle, stiffness, damping) { + + /** + * @property {Phaser.Game} game - Local reference to game. + */ + this.game = world.game; + + /** + * @property {Phaser.Physics.P2} world - Local reference to P2 World. + */ + this.world = world; + + if (typeof restAngle === 'undefined') { restAngle = null; } + if (typeof stiffness === 'undefined') { stiffness = 100; } + if (typeof damping === 'undefined') { damping = 1; } + + if (restAngle) + { + restAngle = world.pxm(restAngle); + } + + var options = { + restAngle: restAngle, + stiffness: stiffness, + damping: damping + }; + + p2.RotationalSpring.call(this, bodyA, bodyB, options); + +}; + +Phaser.Physics.P2.Spring.prototype = Object.create(p2.RotationalSpring.prototype); +Phaser.Physics.P2.Spring.prototype.constructor = Phaser.Physics.P2.Spring; diff --git a/src/physics/p2/Spring.js b/src/physics/p2/Spring.js index 47e20e12e..61c1188a8 100644 --- a/src/physics/p2/Spring.js +++ b/src/physics/p2/Spring.js @@ -5,7 +5,7 @@ */ /** -* Creates a spring, connecting two bodies. A spring can have a resting length, a stiffness and damping. +* Creates a linear spring, connecting two bodies. A spring can have a resting length, a stiffness and damping. * * @class Phaser.Physics.P2.Spring * @classdesc Physics Spring Constructor @@ -65,9 +65,9 @@ Phaser.Physics.P2.Spring = function (world, bodyA, bodyB, restLength, stiffness, options.localAnchorB = [ world.pxm(localB[0]), world.pxm(localB[1]) ]; } - p2.Spring.call(this, bodyA, bodyB, options); + p2.LinearSpring.call(this, bodyA, bodyB, options); }; -Phaser.Physics.P2.Spring.prototype = Object.create(p2.Spring.prototype); +Phaser.Physics.P2.Spring.prototype = Object.create(p2.LinearSpring.prototype); Phaser.Physics.P2.Spring.prototype.constructor = Phaser.Physics.P2.Spring; diff --git a/src/physics/p2/World.js b/src/physics/p2/World.js index c7855057d..82ed26512 100644 --- a/src/physics/p2/World.js +++ b/src/physics/p2/World.js @@ -1295,7 +1295,7 @@ Phaser.Physics.P2.prototype = { }, /** - * Creates a spring, connecting two bodies. A spring can have a resting length, a stiffness and damping. + * Creates a linear spring, connecting two bodies. A spring can have a resting length, a stiffness and damping. * * @method Phaser.Physics.P2#createSpring * @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyA - First connected body. @@ -1303,9 +1303,6 @@ Phaser.Physics.P2.prototype = { * @param {number} [restLength=1] - Rest length of the spring. A number > 0. * @param {number} [stiffness=100] - Stiffness of the spring. A number >= 0. * @param {number} [damping=1] - Damping of the spring. A number >= 0. - * @param {number} [restLength=1] - Rest length of the spring. A number > 0. - * @param {number} [stiffness=100] - Stiffness of the spring. A number >= 0. - * @param {number} [damping=1] - Damping of the spring. A number >= 0. * @param {Array} [worldA] - Where to hook the spring to body A in world coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32]. * @param {Array} [worldB] - Where to hook the spring to body B in world coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32]. * @param {Array} [localA] - Where to hook the spring to body A in local body coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32]. @@ -1328,6 +1325,33 @@ Phaser.Physics.P2.prototype = { }, + /** + * Creates a rotational spring, connecting two bodies. A spring can have a resting length, a stiffness and damping. + * + * @method Phaser.Physics.P2#createRotationalSpring + * @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyA - First connected body. + * @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyB - Second connected body. + * @param {number} [restAngle] - The relative angle of bodies at which the spring is at rest. If not given, it's set to the current relative angle between the bodies. + * @param {number} [stiffness=100] - Stiffness of the spring. A number >= 0. + * @param {number} [damping=1] - Damping of the spring. A number >= 0. + * @return {Phaser.Physics.P2.RotationalSpring} The spring + */ + createRotationalSpring: function (bodyA, bodyB, restAngle, stiffness, damping) { + + bodyA = this.getBody(bodyA); + bodyB = this.getBody(bodyB); + + if (!bodyA || !bodyB) + { + console.warn('Cannot create Rotational Spring, invalid body objects given'); + } + else + { + return this.addSpring(new Phaser.Physics.P2.RotationalSpring(this, bodyA, bodyB, restAngle, stiffness, damping)); + } + + }, + /** * Creates a new Body and adds it to the World. *