-
Notifications
You must be signed in to change notification settings - Fork 0
/
spring.js
35 lines (28 loc) · 1.08 KB
/
spring.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
(function () {
"use strict";
function Spring(firstPoint, secondPoint) {
this.first = firstPoint;
this.second = secondPoint;
this.restLength = 100;
this.springyness = 16;
}
Spring.prototype.length = function () {
return this.second.pos.subtract(this.first.pos).length();
};
Spring.prototype.draw = function (context) {
context.strokeStyle = '#DEA5A4';
context.beginPath();
context.moveTo(this.first.pos.x, this.first.pos.y);
context.lineTo(this.second.pos.x, this.second.pos.y);
context.stroke();
};
Spring.prototype.update = function (context, adjust) {
var difference = this.second.pos.subtract(this.first.pos);
var distance = this.length();
var extension = distance - this.restLength;
var vel = difference.normalize().divide(this.springyness).multiply(extension).multiply(adjust);
this.first.vel = this.first.vel.add(vel);
this.second.vel = this.second.vel.subtract(vel);
};
phys.Spring = Spring;
})(window.phys = window.phys || {});