API Docs for: 0.6.1
Show:

File: src/constraints/Constraint.js

  1. module.exports = Constraint;
  2.  
  3. var Utils = require('../utils/Utils');
  4.  
  5. /**
  6. * Constraint base class
  7. * @class Constraint
  8. * @author schteppe
  9. * @constructor
  10. * @param {Body} bodyA
  11. * @param {Body} bodyB
  12. * @param {object} [options]
  13. * @param {boolean} [options.collideConnected=true]
  14. * @param {boolean} [options.wakeUpBodies=true]
  15. */
  16. function Constraint(bodyA, bodyB, options){
  17. options = Utils.defaults(options,{
  18. collideConnected : true,
  19. wakeUpBodies : true,
  20. });
  21.  
  22. /**
  23. * Equations to be solved in this constraint
  24. * @property equations
  25. * @type {Array}
  26. */
  27. this.equations = [];
  28.  
  29. /**
  30. * @property {Body} bodyA
  31. */
  32. this.bodyA = bodyA;
  33.  
  34. /**
  35. * @property {Body} bodyB
  36. */
  37. this.bodyB = bodyB;
  38.  
  39. /**
  40. * @property {Number} id
  41. */
  42. this.id = Constraint.idCounter++;
  43.  
  44. /**
  45. * Set to true if you want the bodies to collide when they are connected.
  46. * @property collideConnected
  47. * @type {boolean}
  48. */
  49. this.collideConnected = options.collideConnected;
  50.  
  51. if(options.wakeUpBodies){
  52. if(bodyA){
  53. bodyA.wakeUp();
  54. }
  55. if(bodyB){
  56. bodyB.wakeUp();
  57. }
  58. }
  59. }
  60.  
  61. /**
  62. * Update all the equations with data.
  63. * @method update
  64. */
  65. Constraint.prototype.update = function(){
  66. throw new Error("method update() not implmemented in this Constraint subclass!");
  67. };
  68.  
  69. /**
  70. * Enables all equations in the constraint.
  71. * @method enable
  72. */
  73. Constraint.prototype.enable = function(){
  74. var eqs = this.equations;
  75. for(var i=0; i<eqs.length; i++){
  76. eqs[i].enabled = true;
  77. }
  78. };
  79.  
  80. /**
  81. * Disables all equations in the constraint.
  82. * @method disable
  83. */
  84. Constraint.prototype.disable = function(){
  85. var eqs = this.equations;
  86. for(var i=0; i<eqs.length; i++){
  87. eqs[i].enabled = false;
  88. }
  89. };
  90.  
  91. Constraint.idCounter = 0;
  92.