-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics2.js
256 lines (202 loc) · 7.06 KB
/
physics2.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Copyright 2010 Ashley Hewson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
addToy({
name: "physics2",
contributors: "ash",
constructor: function(ctx, width, height) {
var S = 100
function Vector(x, y) {
this.x = x
this.y = y
this.lengthSquared = x*x + y*y
}
Vector.prototype.add = function(other) {
return new Vector(this.x + other.x, this.y + other.y)
}
Vector.prototype.sub = function(other) {
return new Vector(this.x - other.x, this.y - other.y)
}
Vector.prototype.scale = function (k) {
return new Vector(this.x * k, this.y * k)
}
Vector.prototype.perp = function() {
return new Vector(-this.y, this.x)
}
Vector.prototype.unit = function() {
var l = this.lengthSquared
if (l != 0.0) {
return this.scale(1.0/Math.sqrt(l))
} else {
return this
}
}
Vector.prototype.dot = function(other) {
return this.x*other.x + this.y*other.y
}
function Circle(center, r) {
this.center = center
this.r = r
}
Circle.prototype.draw = function() {
ctx.beginPath()
ctx.arc(S*this.center.x,height - S*this.center.y,S*this.r,0,2*Math.PI,true)
ctx.stroke()
}
Circle.prototype.getContactsWithCircle = function(other) {
var displ = other.center.sub(this.center)
var l = Math.sqrt(displ.lengthSquared)
var overlap = this.r + other.r - l
if (overlap > 0) {
var normal = displ.scale(1.0 / l)
var c = new Contact(this, other, normal, overlap)
return [c]
} else {
return []
}
}
function Line(a, b) { // r = a + tb
this.a = a
this.b = b
}
Line.prototype.draw = function() {
var worldWidth = width / S
var worldHeight = height / S
var v = this.a
while( (v.x >= 0 && v.x <= worldWidth) ||
(v.y >= 0 && v.y <= worldHeight) ) {
v = v.add(this.b)
}
ctx.beginPath()
ctx.moveTo(S*v.x, height - S*v.y)
v = this.a
while( (v.x >= 0 && v.x <= worldWidth) ||
(v.y >= 0 && v.y <= worldHeight) ) {
v = v.add(this.b.scale(-1))
}
ctx.lineTo(S*v.x, height - S*v.y)
ctx.stroke()
}
Line.prototype.getContactsWithCircle = function (circle) {
var normal = this.b.perp().unit()
var d = normal.dot(circle.center.sub(this.a))
var overlap = circle.r - Math.abs(d)
if (overlap > 0) {
var c = new Contact(this, circle, normal, overlap)
return [c]
} else {
return []
}
}
function Contact(body1, body2, normal, overlap) {
this.body1 = body1
this.body2 = body2
this.normal = normal
this.overlap = overlap
}
Contact.prototype.resolve = function() {
var rv = this.body2.velocity.sub(this.body1.velocity)
var vn = rv.dot(this.normal)
if (this.body1 instanceof Circle) {
this.body1.velocity = this.body1.velocity.add(
this.normal.scale(vn)
)
}
if (this.body2 instanceof Circle) {
this.body2.velocity = this.body2.velocity.sub(
this.normal.scale(vn)
)
}
}
function lineThru(x0, y0, x1, y1) {
// a x0 + b y0 + c = 0
// a x1 + b y1 + c = 0
// a(x0-x1) + b(y0-y1) = 0
return new Line(
new Vector(x0, y0),
new Vector(x1 - x0, y1 - y0)
)
}
var lines = []
var circles = []
var Zero = new Vector(0, 0)
var gravity = new Vector(0, -5)
var invDt = 1.0/60.0
function addLine(x0, y0, x1, y1) {
l = lineThru(x0, y0, x1, y1)
l.id = lines.length
l.invMass = 0.0
l.velocity = Zero
lines.push(l)
}
function addCircle(x, y, r) {
c = new Circle(new Vector(x, y), r)
c.id = circles.length
c.invMass = 1.0
c.velocity = Zero
circles.push(c)
}
function r() {
return 0.15*Math.random() + 0.1
}
addLine(0, 2, 2, 0)
addLine(2, 0, 5, 2)
for (var i = 1; i < 6; i++) {
addCircle(i, 4, r())
addCircle(i, 5, r())
addCircle(i, 6, r())
}
this.tick = function() {
var contacts = []
ctx.fillStyle = "white"
ctx.fillRect(0,0,width,height)
ctx.strokeStyle = "black"
ctx.lineCap = "square"
for (var i in circles) {
circles[i].velocity = circles[i].velocity.add(gravity.scale(invDt))
}
for (var i in lines) {
var line = lines[i]
for (var j in circles) {
var circle = circles[j]
contacts = contacts.concat(
line.getContactsWithCircle(circle)
)
}
}
for (var i in circles) {
for (var j in circles) {
if (circles[i].id < circles[j].id) {
contacts = contacts.concat(
circles[i].getContactsWithCircle(circles[j])
)
}
}
}
for (i = 0; i < 10; i++) {
contacts.forEach(function (contact) {
contact.resolve()
})
}
for (var i in circles) {
circles[i].center = circles[i].center.add(circles[i].velocity.scale(invDt))
}
for (var i in lines) {
lines[i].draw()
}
for (var i in circles) {
circles[i].draw()
}
}
}
})