-
Notifications
You must be signed in to change notification settings - Fork 2
/
sunEarth.html
147 lines (109 loc) · 2.9 KB
/
sunEarth.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Gravity Simulation</title>
<script src="/js/d3.min.js" charset="utf-8"></script>
<script src="/js/numeric-1.2.6.min.js" charset="utf-8"></script>
</head>
<body>
<svg width="600" height="600">
</svg>
<script>
"use strict"
var drag=d3.behavior.drag()
.origin(function(d){return {x: d.x, y: 600-d.y}})
.on("drag",dragmove);
var data = [{x: 300, vx: (0.1*1+0.4*0.5)/-10, y: 300, vy: 0, r: 10*Math.sqrt(10), m: 10, color: 'yellow'},];
data.push({x: 300, vx: 0.1, y: 500, vy: 0.0, r: 10, m: 1, color: 'steelblue'});
data.push({x: 300, vx: 0.4, y: 520, vy: 0.0, r: 5, m: 0.5, color: 'lightGray'});
var planets = d3.select("svg").selectAll("circle").data(data)
.enter().append("circle")
.attr("cx", function(d) { return d.x})
.attr("cy", function(d) { return 600-d.y})
.attr("r", function(d) { return d.r} )
.style("fill", function(d) { return d.color} )
.call (drag);
function dragmove (d){
d.x = d3.event.x;
d.y = 600-d3.event.y;
d3.select(this)
.attr("cx", d3.event.x)
.attr("cy", d3.event.y);
}
var earth=d3.select("#earth");
var orbitRadius=200;
var theta=0;
var speed=0.0005;
var t0 = Date.now();
var G = 1;
function f_gravity (t,z){
var x0,x1,y0,y1,vx0,vx1,vy0,vy1,m0,m1,r0,r1;
var dz = [];
var n = z.length/4;
for (var i=0; i<n; i++){
dz.push (z[4*i+1]);
dz.push (0);
dz.push (z[4*i+3]);
dz.push (0);
}
for (var i0=0; i0<n; i0++){
x0=z[i0*4];
y0=z[i0*4+2];
m0=data[i0].m;
r0=data[i0].r;
for(var i1=i0+1; i1<n; i1++){
x1=z[i1*4];
y1=z[i1*4+2];
m1=data[i1].m;
r1=data[i1].r;
var dx = x0-x1;
var dy = y0-y1;
var dist = dx*dx+dy*dy;
var f_g = (G*m1*m0)/dist;//gravity force
dist=Math.sqrt(dist);
var f_c = 1/Math.pow(dist/(r0+r1)*1.2,20);//collision force
var fsub = (f_g-f_c)*dx/dist;
dz[4*i0+1]-=fsub/m0;
dz[4*i1+1]+=fsub/m1;
fsub = (f_g-f_c)*dy/dist;
dz[4*i0+3]-=fsub/m0;
dz[4*i1+3]+=fsub/m1;
}
}
return dz;
}
var t=0;
var sol;
var t_chunk = 10;
var t_computed=0;
function update_position(tnew) {
while(tnew > t_computed) {
var z = [];
for(var i=0; i<data.length; i++) {
Array.prototype.push.apply(z, [data[i].x, data[i].vx, data[i].y, data[i].vy]);
}
sol = numeric.dopri(t_computed, t_computed+t_chunk, z, f_gravity);
t_computed += t_chunk;
}
var u = sol.at(tnew);
//console.log(u);
for(var i=0; i<data.length; i++) {
data[i].x=u[i*4];
data[i].vx=u[i*4+1];
data[i].y=u[i*4+2];
data[i].vy=u[i*4+3];
}
planets.attr("cx", function(d) { return d.x})
.attr("cy", function(d) { return 600-d.y});
t = tnew;
}
d3.timer(update_position)
/*d3.timer(function() {
var time = Date.now() - t0;
theta=time*speed;
earth.attr("cx",Math.sin(theta)*orbitRadius+300).attr("cy",Math.cos(theta)*orbitRadius+300);
});*/
</script>
</body>
</html>