phaser/v3/merge/physics/p2/Spring.js

77 lines
2.8 KiB
JavaScript
Raw Normal View History

/**
* @author Richard Davey <rich@photonstorm.com>
2016-04-04 21:15:01 +00:00
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Creates a linear spring, connecting two bodies. A spring can have a resting length, a stiffness and damping.
*
* @class Phaser.Physics.P2.Spring
* @constructor
* @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.
* @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].
*/
Phaser.Physics.P2.Spring = function (world, bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB) {
/**
* @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 (restLength === undefined) { restLength = 1; }
if (stiffness === undefined) { stiffness = 100; }
if (damping === undefined) { damping = 1; }
restLength = world.pxm(restLength);
var options = {
restLength: restLength,
stiffness: stiffness,
damping: damping
};
2014-02-15 02:56:24 +00:00
if (typeof worldA !== 'undefined' && worldA !== null)
{
options.worldAnchorA = [ world.pxm(worldA[0]), world.pxm(worldA[1]) ];
}
2014-02-15 02:56:24 +00:00
if (typeof worldB !== 'undefined' && worldB !== null)
{
options.worldAnchorB = [ world.pxm(worldB[0]), world.pxm(worldB[1]) ];
}
2014-02-15 02:56:24 +00:00
if (typeof localA !== 'undefined' && localA !== null)
{
options.localAnchorA = [ world.pxm(localA[0]), world.pxm(localA[1]) ];
}
2014-02-15 02:56:24 +00:00
if (typeof localB !== 'undefined' && localB !== null)
{
options.localAnchorB = [ world.pxm(localB[0]), world.pxm(localB[1]) ];
}
/**
* @property {p2.LinearSpring} data - The actual p2 spring object.
*/
this.data = new p2.LinearSpring(bodyA, bodyB, options);
this.data.parent = this;
2014-03-23 06:31:26 +00:00
};
Phaser.Physics.P2.Spring.prototype.constructor = Phaser.Physics.P2.Spring;