-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforceCollideElastic.js
157 lines (134 loc) · 4.26 KB
/
forceCollideElastic.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
var constant = function(x) {
return function() {
return x;
};
}
var jiggle = function() {
return (Math.random() - 0.5) * 1e-6;
}
function x(d) {
return d.x + d.vx;
}
function y(d) {
return d.y + d.vy;
}
function forceCollideElastic(radius) {
var nodes,
radii,
masses,
strength = 1,
iterations = 1;
if (typeof radius !== "function") radius = constant(radius == null ? 1 : +radius);
function force() {
var i, n = nodes.length,
tree,
node,
xi,
yi,
ri,
ri2;
for (var k = 0; k < iterations; ++k) {
tree = d3.quadtree(nodes, x, y).visitAfter(prepare);
for (i = 0; i < n; ++i) {
node = nodes[i];
ri = radii[i], ri2 = ri * ri;
xi = node.x + node.vx;
yi = node.y + node.vy;
tree.visit(apply);
}
}
function apply(quad, x0, y0, x1, y1) {
var data = quad.data, rj = quad.r, r = ri + rj;
if (data) {
if (data.index > i) {
var x = xi - data.x - data.vx,
y = yi - data.y - data.vy,
l = x * x + y * y;
if (l < r * r) {
if (x === 0) x = jiggle(), l += x * x;
if (y === 0) y = jiggle(), l += y * y;
console.log('Collide!');
var π = Math.PI,
x1 = node.x,
y1 = node.y,
x2 = data.x,
y2 = data.y,
m1 = masses[i],
m2 = masses[data.index],
v1x = node.vx,
v1y = node.vy,
v2x = data.vx,
v2y = data.vy,
v1 = Math.sqrt(Math.pow(v1x,2) + Math.pow(v1y,2)),
v2 = Math.sqrt(Math.pow(v2x,2) + Math.pow(v2y,2));
// get contact angle
var φ = Math.atan2(y2-y1, x2-x1);
// get movement angles
var θ1 = Math.atan2(v1y, v1x);
var θ2 = Math.atan2(v2y, v2x);
var v1x_new =
( v1 * Math.cos(θ1 - φ) * (m1 - m2) +
2 * m2 * v2 * Math.cos(θ2 - φ) ) /
( m1 + m2 ) *
Math.cos(φ) +
v1 * Math.sin(θ1 - φ) * Math.cos(φ + π/2);
var v1y_new =
( v1 * Math.cos(θ1 - φ) * (m1 - m2) +
2 * m2 * v2 * Math.cos(θ2 - φ) ) /
( m1 + m2 ) *
Math.sin(φ) +
v1 * Math.sin(θ1 - φ) * Math.cos(φ + π/2);
var v2x_new =
( v2 * Math.cos(θ2 - φ) * (m2 - m1) +
2 * m1 * v1 * Math.cos(θ1 - φ) ) /
( m2 + m1 ) *
Math.cos(φ) +
v2 * Math.sin(θ2 - φ) * Math.cos(φ + π/2);
var v2y_new =
( v2 * Math.cos(θ2 - φ) * (m2 - m1) +
2 * m1 * v1 * Math.cos(θ1 - φ) ) /
( m2 + m1 ) *
Math.sin(φ) +
v2 * Math.sin(θ2 - φ) * Math.cos(φ + π/2);
node.vx = v1x_new;
node.vy = v1y_new;
data.vx = v2x_new;
data.vy = v2y_new;
// l = (r - (l = Math.sqrt(l))) / l * strength;
// node.vx += (x *= l) * (r = (rj *= rj) / (ri2 + rj));
// node.vy += (y *= l) * r;
// data.vx -= x * (r = 1 - r);
// data.vy -= y * r;
}
}
return;
}
return x0 > xi + r || x1 < xi - r || y0 > yi + r || y1 < yi - r;
}
}
function prepare(quad) {
if (quad.data) return quad.r = radii[quad.data.index];
for (var i = quad.r = 0; i < 4; ++i) {
if (quad[i] && quad[i].r > quad.r) {
quad.r = quad[i].r;
}
}
}
force.initialize = function(_) {
var i, n = (nodes = _).length; radii = new Array(n); masses = new Array(n);
for (i = 0; i < n; ++i) {
radii[i] = +radius(nodes[i], i, nodes);
masses[i] = Math.PI * Math.pow(radii[i],2);
}
};
force.iterations = function(_) {
return arguments.length ? (iterations = +_, force) : iterations;
};
force.strength = function(_) {
return arguments.length ? (strength = +_, force) : strength;
};
force.radius = function(_) {
return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), force) : radius;
};
return force;
}