2014-02-15 02:19:37 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2014-02-15 02:19:37 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-08-27 23:58:50 +00:00
|
|
|
* Creates a linear spring, connecting two bodies. A spring can have a resting length, a stiffness and damping.
|
2014-02-15 02:19:37 +00:00
|
|
|
*
|
2014-03-05 02:36:08 +00:00
|
|
|
* @class Phaser.Physics.P2.Spring
|
2014-02-15 02:19:37 +00:00
|
|
|
* @constructor
|
2014-03-12 00:07:27 +00:00
|
|
|
* @param {Phaser.Physics.P2} world - A reference to the P2 World.
|
2014-03-13 10:14:06 +00:00
|
|
|
* @param {p2.Body} bodyA - First connected body.
|
|
|
|
* @param {p2.Body} bodyB - Second connected body.
|
2014-02-15 02:19:37 +00:00
|
|
|
* @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.
|
2014-03-13 12:14:14 +00:00
|
|
|
* @param {Array} [worldA] - Where to hook the spring to body A in world coordinates. This value is an array with 2 elements matching 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 with 2 elements matching 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 with 2 elements matching x and y, i.e: [32, 32].
|
|
|
|
* @param {Array} [localB] - Where to hook the spring to body B in local body coordinates. This value is an array with 2 elements matching x and y, i.e: [32, 32].
|
2014-02-15 02:19:37 +00:00
|
|
|
*/
|
2014-03-12 00:07:27 +00:00
|
|
|
Phaser.Physics.P2.Spring = function (world, bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB) {
|
2014-02-15 02:19:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
|
|
*/
|
2014-03-12 00:07:27 +00:00
|
|
|
this.game = world.game;
|
|
|
|
|
|
|
|
/**
|
2014-03-13 09:43:00 +00:00
|
|
|
* @property {Phaser.Physics.P2} world - Local reference to P2 World.
|
2014-03-12 00:07:27 +00:00
|
|
|
*/
|
|
|
|
this.world = world;
|
2014-02-15 02:19:37 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (restLength === undefined) { restLength = 1; }
|
|
|
|
if (stiffness === undefined) { stiffness = 100; }
|
|
|
|
if (damping === undefined) { damping = 1; }
|
2014-02-15 02:19:37 +00:00
|
|
|
|
2014-03-13 10:32:34 +00:00
|
|
|
restLength = world.pxm(restLength);
|
|
|
|
|
2014-02-15 02:19:37 +00:00
|
|
|
var options = {
|
|
|
|
restLength: restLength,
|
|
|
|
stiffness: stiffness,
|
|
|
|
damping: damping
|
|
|
|
};
|
|
|
|
|
2014-02-15 02:56:24 +00:00
|
|
|
if (typeof worldA !== 'undefined' && worldA !== null)
|
2014-02-15 02:19:37 +00:00
|
|
|
{
|
2014-03-12 00:07:27 +00:00
|
|
|
options.worldAnchorA = [ world.pxm(worldA[0]), world.pxm(worldA[1]) ];
|
2014-02-15 02:19:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 02:56:24 +00:00
|
|
|
if (typeof worldB !== 'undefined' && worldB !== null)
|
2014-02-15 02:19:37 +00:00
|
|
|
{
|
2014-03-12 00:07:27 +00:00
|
|
|
options.worldAnchorB = [ world.pxm(worldB[0]), world.pxm(worldB[1]) ];
|
2014-02-15 02:19:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 02:56:24 +00:00
|
|
|
if (typeof localA !== 'undefined' && localA !== null)
|
2014-02-15 02:19:37 +00:00
|
|
|
{
|
2014-03-12 00:07:27 +00:00
|
|
|
options.localAnchorA = [ world.pxm(localA[0]), world.pxm(localA[1]) ];
|
2014-02-15 02:19:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 02:56:24 +00:00
|
|
|
if (typeof localB !== 'undefined' && localB !== null)
|
2014-02-15 02:19:37 +00:00
|
|
|
{
|
2014-03-12 00:07:27 +00:00
|
|
|
options.localAnchorB = [ world.pxm(localB[0]), world.pxm(localB[1]) ];
|
2014-02-15 02:19:37 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 13:13:20 +00:00
|
|
|
/**
|
|
|
|
* @property {p2.LinearSpring} data - The actual p2 spring object.
|
|
|
|
*/
|
|
|
|
this.data = new p2.LinearSpring(bodyA, bodyB, options);
|
2014-02-15 02:19:37 +00:00
|
|
|
|
2014-09-02 14:06:43 +00:00
|
|
|
this.data.parent = this;
|
|
|
|
|
2014-03-23 06:31:26 +00:00
|
|
|
};
|
2014-02-15 02:19:37 +00:00
|
|
|
|
2014-03-05 02:36:08 +00:00
|
|
|
Phaser.Physics.P2.Spring.prototype.constructor = Phaser.Physics.P2.Spring;
|