Sem to prepsal konecne asi spravne podle puvodnich rovnic. Konecne to rotuje. Ale mesic nejak podivne, tak se moc nesmejte, precijen nejsem fyzik :) (navic, jestli to spravne chapu, tak je to jen rovnice pro rotaci kolem orbity bez uvazovani okolnich teles; To uz by byl lepsi klasicky sin, cos, ne?) Co tam mam spatne?
<html>
<style>
#graph {border:1px solid #f00; width:600px; height:500px; position:relative;}
#graph div {position:absolute;}
</style>
<div id="graph">
<div id="o0"><b>o</b></div>
<div id="o1">o</div>
<div id="o2">o</div>
</div>
<div id="label">
<div id="l0"></div>
<div id="l1"></div>
<div id="l2"></div>
<div>
<input type=button value=start onclick="if (timer!=null) {clearInterval(timer);} timer = setInterval(rotace,k.timer);">
<input type=button value=stop onclick="clearInterval(timer);">
</div>
</div>
<script>
function vector(x,y)
{
this.x = x;
this.y = y;
return this;
}
function kresli(orbit,center)
{
orbit.ppx.x = center.ppx.x + Math.round(orbit.p.x * orbit.zoom.x);
orbit.ppx.y = center.ppx.y + Math.round(orbit.p.y * orbit.zoom.y);
orbit.label.innerHTML = orbit.name + ": x = %1, y = %2".replace('%1',orbit.p.x).replace('%2',orbit.p.y);
orbit.el.style.left = orbit.ppx.x + 'px';
orbit.el.style.top = orbit.ppx.y + 'px';
}
function spocitej(orbit,center)
{
var F,a,v,p,rr;
a = new vector();
v = new vector();
p = new vector();
rr = Math.pow(Math.pow(center.p.x - orbit.p.x, 2) + Math.pow(center.p.y - orbit.p.y, 2), 1.5);
F = - k.kappa * center.m / rr;
a.x = orbit.p.x * F;
a.y = orbit.p.y * F;
v.x = a.x * k.dt + orbit.v.x;
v.y = a.y * k.dt + orbit.v.y;
p.x = v.x * k.dt + orbit.p.x;
p.y = v.y * k.dt + orbit.p.y;
orbit.v = v;
orbit.p = p;
}
function rotace()
{
spocitej(zeme , slunce);
spocitej(mesic, zeme);
kresli(zeme , slunce);
kresli(mesic , zeme);
}
var k = {
// nasa units: http://nssdc.gsfc.nasa.gov/…t_notes.html
// nasa earth: http://nssdc.gsfc.nasa.gov/…rthfact.html
// nasa sun: http://nssdc.gsfc.nasa.gov/…sunfact.html
// nasa moon: http://nssdc.gsfc.nasa.gov/…oonfact.html
zoom : new vector(1e-9,1e-9), // x * zoom, y * zoom
timer: 100, //program timer ms
kappa : 6.67384E-11, // Gravitational Constant [m3*kg-1*s-2] (N*m2*kg-2) - nasaU
AU : 1.49597900E+11, // AU [m] - nasaU
s_m : 1.988500E+30, // mass [kg] - nasaS
z_m : 5.9726E+24, // mass [kg] - nasaE
z_rp : 1.4709E+11, // peri [m] = 0.98329 * AU - nasaE
z_vmax: 30.29E+3, // rychlost v perihelu [m/s] - nasaE
m_m : 0.07342E+24, // mass [kg] - nasaE
m_rp : 0.3633E+9, // peri [m] = 0.98329 * AU - nasaE
m_vmax: 1.076E+3, // rychlost v perihelu [m/s] - nasaE
dt : 3600*24 // 24h time step [s]
}
var slunce = {
name: 'Slunce',
el: document.getElementById('o0'),
label: document.getElementById('l0'),
m : k.s_m,
p : new vector(0, 0),
ppx: new vector(300, 200),
zoom: k.zoom
};
var zeme = {
name : 'Zeme',
el : document.getElementById('o1'),
label: document.getElementById('l1'),
m : k.z_m,
p : new vector(-k.z_rp, 0),
v : new vector(0, k.z_vmax),
ppx: new vector(0, 0),
zoom: k.zoom
};
var mesic = {
name: 'Mesic',
el: document.getElementById('o2'),
label: document.getElementById('l2'),
m : k.m_m,
p : new vector(-k.m_rp, 0),
v : new vector(0, k.m_vmax),
ppx: new vector(0, 0),
zoom: new vector(k.zoom.x*10, k.zoom.y*10)
}
kresli(slunce, slunce);
kresli(zeme , slunce);
kresli(mesic , zeme);
var timer = null;
</script>
</html>