File: src/constraints/GearConstraint.js
var Constraint = require('./Constraint')
, Equation = require('../equations/Equation')
, AngleLockEquation = require('../equations/AngleLockEquation')
, vec2 = require('../math/vec2');
module.exports = GearConstraint;
/**
* Constrains the angle of two bodies to each other to be equal. If a gear ratio is not one, the angle of bodyA must be a multiple of the angle of bodyB.
* @class GearConstraint
* @constructor
* @author schteppe
* @param {Body} bodyA
* @param {Body} bodyB
* @param {Object} [options]
* @param {Number} [options.angle=0] Relative angle between the bodies. Will be set to the current angle between the bodies (the gear ratio is accounted for).
* @param {Number} [options.ratio=1] Gear ratio.
* @param {Number} [options.maxTorque] Maximum torque to apply.
* @extends Constraint
*
* @example
* var constraint = new GearConstraint(bodyA, bodyB);
* world.addConstraint(constraint);
*
* @example
* var constraint = new GearConstraint(bodyA, bodyB, {
* ratio: 2,
* maxTorque: 1000
* });
* world.addConstraint(constraint);
*/
function GearConstraint(bodyA, bodyB, options){
options = options || {};
Constraint.call(this, bodyA, bodyB, Constraint.GEAR, options);
/**
* The gear ratio.
* @property ratio
* @type {Number}
*/
this.ratio = options.ratio !== undefined ? options.ratio : 1;
/**
* The relative angle
* @property angle
* @type {Number}
*/
this.angle = options.angle !== undefined ? options.angle : bodyB.angle - this.ratio * bodyA.angle;
// Send same parameters to the equation
options.angle = this.angle;
options.ratio = this.ratio;
this.equations = [
new AngleLockEquation(bodyA,bodyB,options),
];
// Set max torque
if(options.maxTorque !== undefined){
this.setMaxTorque(options.maxTorque);
}
}
GearConstraint.prototype = new Constraint();
GearConstraint.prototype.constructor = GearConstraint;
GearConstraint.prototype.update = function(){
var eq = this.equations[0];
if(eq.ratio !== this.ratio){
eq.setRatio(this.ratio);
}
eq.angle = this.angle;
};
/**
* Set the max torque for the constraint.
* @method setMaxTorque
* @param {Number} torque
*/
GearConstraint.prototype.setMaxTorque = function(torque){
this.equations[0].setMaxTorque(torque);
};
/**
* Get the max torque for the constraint.
* @method getMaxTorque
* @return {Number}
*/
GearConstraint.prototype.getMaxTorque = function(torque){
return this.equations[0].maxForce;
};