-
Notifications
You must be signed in to change notification settings - Fork 1
/
stick.js
146 lines (130 loc) · 3.84 KB
/
stick.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class Stick {
/**
* creates a stick between two Point
* takes optional length and stiffness
* @param {Point} p1
* @param {Point} p2
* @param {number=} length
* @param {number=} stiffness
* @param {boolean=} hidden
*/
constructor(p1, p2, length, stiffness, hidden, noColor=false) {
this.startPoint = p1;
this.endPoint = p2;
this.stiffness = stiffness || 2;
this.color = '#f5476a';
this.hidden = hidden;
this.noColor = noColor;
if (!length) {
this.length = this.startPoint.pos.dist(this.endPoint.pos);
} else {
this.length = length;
}
this.startPoint.sticks.push(this);
this.endPoint.sticks.push(this);
}
/**
*
* @param {number=} stepCoef
*/
update(stepCoef) {
// not gonna use vectors for performance optimization
// let dx = this.endPoint.pos.x - this.startPoint.pos.x;
// let dy = this.endPoint.pos.y - this.startPoint.pos.y;
// let dist = Math.sqrt(dx * dx + dy * dy);
// let diff = this.length - dist;
// let percent = diff / dist / 2;
// let offsetx = (dx * percent);
// let offsety = (dy * percent);
// if (!this.startPoint.pinned) {
// this.startPoint.pos.x -= offsetx;
// this.startPoint.pos.y -= offsety;
// }
// if (!this.endPoint.pinned) {
// this.endPoint.pos.x += offsetx;
// this.endPoint.pos.y += offsety;
// }
// ----- algo two
// algo three
let dx = this.endPoint.pos.x - this.startPoint.pos.x;
let dy = this.endPoint.pos.y - this.startPoint.pos.y;
let dist = Math.sqrt(dx * dx + dy * dy);
let diff = (this.length - dist) / dist * this.stiffness;
let offsetx = dx * diff * 0.5;
let offsety = dy * diff * 0.5;
// calculate mass
let m1 = this.startPoint.mass + this.endPoint.mass;
let m2 = this.startPoint.mass / m1;
m1 = this.endPoint.mass / m1;
if (!this.startPoint.pinned) {
this.startPoint.pos.x -= offsetx * m1;
this.startPoint.pos.y -= offsety * m1;
}
if (!this.endPoint.pinned) {
this.endPoint.pos.x += offsetx * m2;
this.endPoint.pos.y += offsety * m2;
}
// calculate mass
// var m1 = this.startPoint.mass + this.endPoint.mass;
// var m2 = this.startPoint.mass / m1;
// m1 = this.endPoint.mass / m1;
// var normal = Vector.sub(this.startPoint.pos, this.endPoint.pos);
// var m = normal.magSq();
// let diff = ((this.length * this.length) - m);
// normal.mult((diff / m) * this.stiffness * stepCoef);
// if (!this.startPoint.pinned) {
// this.startPoint.pos.add(normal);
// }
// if (!this.endPoint.pinned) {
// this.endPoint.pos.sub(normal);
// }
}
/**
* @param {string} color
* @returns {Stick}
*/
setColor(color) {
this.color = color;
return this;
}
/**
* @param {number} length
* @returns {Stick}
*/
setLength(length) {
this.length = length;
return this;
}
/**
* @param {number} value
* @returns {Stick}
*/
setStiffness(value) {
this.stiffness = value;
return this;
}
/**
* @param {boolean} value
* @returns {Stick}
*/
setHidden(value) {
this.hidden = value;
return this;
}
/**
*
* @param {CanvasRenderingContext2D} ctx
*/
render(ctx) {
if (this.hidden) return;
ctx.beginPath();
if(!this.noColor)
ctx.strokeStyle = this.color;
// else
ctx.strokeStyle = "#ffffff";
ctx.moveTo(this.startPoint.pos.x, this.startPoint.pos.y);
ctx.lineTo(this.endPoint.pos.x, this.endPoint.pos.y);
ctx.stroke();
ctx.closePath();
}
}