p2s
Version:
A JavaScript 2D physics engine.
58 lines (51 loc) • 1.68 kB
JavaScript
module.exports = Spring;
/**
* Base class for {{#crossLink "LinearSpring"}}{{/crossLink}} and {{#crossLink "RotationalSpring"}}{{/crossLink}}. Not supposed to be used directly.
*
* @class Spring
* @constructor
* @param {Body} bodyA
* @param {Body} bodyB
* @param {Object} [options]
* @param {number} [options.stiffness=100] Spring constant (see Hookes Law). A number >= 0.
* @param {number} [options.damping=1] A number >= 0. Default: 1
* @param {Array} [options.localAnchorA] Where to hook the spring to body A, in local body coordinates. Defaults to the body center.
* @param {Array} [options.localAnchorB]
* @param {Array} [options.worldAnchorA] Where to hook the spring to body A, in world coordinates. Overrides the option "localAnchorA" if given.
* @param {Array} [options.worldAnchorB]
*/
function Spring(bodyA, bodyB, options){
options = options || {};
/**
* Stiffness of the spring.
* @property stiffness
* @type {number}
*/
this.stiffness = options.stiffness !== undefined ? options.stiffness : 100;
/**
* Damping of the spring.
* @property damping
* @type {number}
*/
this.damping = options.damping !== undefined ? options.damping : 1;
/**
* First connected body.
* @property bodyA
* @type {Body}
*/
this.bodyA = bodyA;
/**
* Second connected body.
* @property bodyB
* @type {Body}
*/
this.bodyB = bodyB;
}
/**
* Apply the spring force to the connected bodies. Called automatically by the World.
* @private
* @method applyForce
*/
Spring.prototype.applyForce = function(){
// To be implemented by subclasses
};