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