-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics.js
174 lines (137 loc) · 4.94 KB
/
physics.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
class Layout {
constructor(graph, stiffness, repulsion, damping, energy_threshold, max_speed) {
this.graph = graph
this.stiffness = stiffness
this.repulsion = repulsion
this.damping = damping
this.energy_threshold = energy_threshold || 0.01
this.max_speed = max_speed || Infinity
this.node_points = {}
this.edge_springs = {}
this.domain_size = 2
this.domain_padding = .075
}
point(node) {
let node_point = this.node_points[node.id]
if (node_point !== undefined) return node_point
return this.node_points[node.id] = new Layout.Point(
Vector.random().multiply(this.domain_size), node.data.mass || 1.)
}
spring(edge) {
let edge_spring = this.edge_springs[edge.id]
if (edge_spring !== undefined) return edge_spring
return this.edge_springs[edge.id] = new Layout.Spring(
this.point(edge.source), this.point(edge.target), edge.data.length || 1., this.stiffness)
}
getEdge(source, dest) {
if (dest === null || source === null) return null
return source.entity.tentacles.find(t => t.id == dest.id) || null
}
mapPoints(f) {
return this.graph.nodes.map(n => f(n, this.point(n)))
}
mapSprings(f) {
return this.graph.edges.map(e => f(e, this.spring(e)))
}
applyCoulombsLaw() {
this.mapPoints((n1, point1) => {
this.mapPoints((n2, point2) => {
if (point1 === point2) return
const d = point1.p.subtract(point2.p)
const distance = d.norm(), direction = d.normalized()
const force = direction.multiply(this.repulsion).divide(Math.pow(distance, 2)/2)
point1.applyForce(force)
point2.applyForce(force.opposite())
})
})
}
applyHookesLaw() {
this.mapSprings((_, spring) => {
const d = spring.point2.p.subtract(spring.point1.p)
const displacement = spring.equilibrium - d.norm()
const direction = d.normalized()
const force = direction.multiply(-spring.k*displacement)
spring.point1.applyForce(force)
spring.point2.applyForce(force.opposite())
})
}
attractToCentre() {
const attraction = this.repulsion*.02
this.mapPoints((node, point) => {
const direction = point.p.opposite()
const force = direction.multiply(attraction)
point.applyForce(force)
})
}
propagateChange(delta) {
const activation_energy = .01
this.mapPoints((node, point) => {
const new_speed = point.v.add(point.a.multiply(delta)).multiply(this.damping)
point.a = Vector.zero()
if (point.energy(new_speed.norm()) < activation_energy)
return point.v = Vector.zero()
point.v = new_speed.clipNorm(this.max_speed)
point.p = point.p.add(point.v.multiply(delta))
})
}
totalEnergy() {
return this.mapPoints((_, point) => point.energy()).reduce((t, e) => t + e, 0)
}
tick(delta) {
this.applyCoulombsLaw()
this.applyHookesLaw()
this.attractToCentre()
this.propagateChange(delta)
}
nearest(pos) {
let min = {node: null, point: null, distance: Infinity}
this.mapPoints((n, point) => {
const distance = point.p.subtract(pos).norm()
if (distance < min.distance)
min = {node: n, point: point, distance: distance}
})
return min
}
getBoundingBox() {
let br = Vector.unit().multiply(this.domain_size), tl = br.opposite()
this.mapPoints((_, point) => {
tl = tl.min(point.p)
br = br.max(point.p)
})
const padding = br.subtract(tl).multiply(this.domain_padding)
return [tl.subtract(padding), br.add(padding)]
}
}
Layout.Point = class Point {
constructor(pos, mass) {
this.p = pos
this.m = mass
this.v = Vector.zero()
this.a = Vector.zero()
}
applyForce(force) {
const max_update = 1e2
if (force.norm() < 1) return
this.a = this.a.add(force.clipNorm(max_update).divide(this.m))
}
energy(hypothetical) {
return this.m*Math.pow(hypothetical || this.v.norm(), 2)/2
}
}
Layout.Spring = class Spring {
constructor(point1, point2, equilibrium, k) {
this.point1 = point1
this.point2 = point2
this.equilibrium = equilibrium
this.k = k
}
distanceToPoint(point) {
const n = this.point2.p.subtract(this.point1.p).normalized().normal()
const ac = point.p.subtract(this.point1.p)
return Math.abs(ac.inner(n))
}
energy(hypothetical) {
const length = this.point2.p.subtract(this.point1.p).norm()
return this.k*Math.pow(hypothetical || (this.equilibrium - length), 2)/2
}
}