ÿþ<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" id="html"> <head> <title>The N-Body Simulation</title> </head> <body style="margin: 0; background-image: url('/images/stars/space-the-final-frontier.jpg'); background-repeat: repeat;"> <div id='space' style="width: 100%; height: 100%;"> <img src="/images/stars/space-the-final-frontier.jpg" width="500" height="500"/> </div> </body> <script type="text/javascript"> //Universal Constants var AU = 1.49598E+11; //m = Astronomical Unit (distance between earth in sun) var G = 5.764608E-4; //5.7646078E-08; //Constant of Gravity var KG = 1E+03; //g = Kilograms var colors = [ '#FFFFFF', '#007F00', '#FFCC00', '#FF6633', '#FF0000', '#CC33CC', '#7F00FF', '#0000FF' ]; var b = document.getElementById("html"); var height = b.offsetHeight; var width = b.offsetWidth; var centerx = width / 2; var centery = height / 2; var ZZ = 0; function Velocity(x, y, z) { this.x = x; this.y = y; } Velocity.prototype.Add = function(a) { this.x += a.x; this.y += a.y; } function Position(x, y, z) { this.x = x; this.y = y; } Position.prototype.Add = function(a) { this.x += a.x; this.y += a.y; } function Body(position, velocity, mass) { this.position = position; this.velocity = velocity; this.mass = mass; this.element = null; this.thumb = null; this.color = colors[ZZ % colors.length]; this.trails = []; for(var i = 0; i < 100; i++) this.trails[i] = null; this.trailptr = 0; ZZ++; } Body.prototype.Distance = function(p) { var deltax = p.position.x - this.position.x; var deltay = p.position.y - this.position.y; return Math.sqrt((deltax * deltax) + (deltay * deltay)); } Body.prototype.Gravity = function(p) { var deltax = p.position.x - this.position.x; var deltay = p.position.y - this.position.y; var distance = Math.sqrt((deltax * deltax) + (deltay * deltay)); var accel = (G / (distance * distance)) * p.mass; this.acceleration.Add(new Velocity((deltax/distance) * accel, (deltay/distance) * accel)); } Body.prototype.Plot = function() { var x = Math.round(centerx + (this.position.x * 25/AU)); var y = Math.round(centery - (this.position.y * 25/AU)); if(this.trails[this.trailptr] == null) { var doc = document.getElementById('space'); this.trails[this.trailptr] = doc.appendChild(document.createElement('div')); this.trails[this.trailptr].style.position = 'absolute'; this.trails[this.trailptr].innerHTML = '·'; this.trails[this.trailptr].style.color = this.color; this.trails[this.trailptr].style.visibility = 'visible'; } this.trails[this.trailptr].style.left = x + "px"; this.trails[this.trailptr].style.top = y + "px"; this.trailptr = (this.trailptr + 1) % 100; if(this.element == null) { var doc = document.getElementById('space'); this.element = doc.appendChild(document.createElement('div')); this.element.style.position = 'absolute'; this.element.innerHTML = 'Ï%'; this.element.style.color = this.color; this.element.style.visibility = 'visible'; } this.element.style.left = x + "px"; this.element.style.top = y + "px"; /* var x = Math.round(25 + this.position.x / 40); var y = Math.round(25 - this.position.y / 40); if(this.thumb == null) { var doc = document.getElementById('space'); this.thumb = doc.appendChild(document.createElement('div')); this.thumb.style.position = 'absolute'; this.thumb.innerHTML = 'ª%'; this.thumb.style.color = this.color; } if(x < 0 || y < 0 || x > 50 || y > 50) { } else { this.thumb.style.left = x + "px"; this.thumb.style.top = y + "px"; } */ } Body.prototype.Destroy = function() { var doc = document.getElementById('space'); for(var i = 0; i < 100; i++) { if(this.trails[i] != null) doc.removeChild(this.trails[i]); } doc.removeChild(this.element); } Body.prototype.Reset = function() { this.acceleration = new Velocity(0, 0); } function MoveAll() { var i1; var i2; for(i1 in Bodies) Bodies[i1].Reset(); var timeScale = 10; for(var i = 0; i < 1; i++) { for(i1 in Bodies) Bodies[i1].Reset(); for(i1 in Bodies) { var body1 = Bodies[i1]; body1.position.x += body1.velocity.x; body1.position.y += body1.velocity.y; } for(i1 in Bodies) { var body1 = Bodies[i1]; for(i2 in Bodies) { var body2 = Bodies[i2]; if(body1 != body2) { body1.Gravity(body2); } } } for(i1 in Bodies) { var body1 = Bodies[i1]; body1.velocity.x += body1.acceleration.x; body1.velocity.y += body1.acceleration.y; } } var x0 = Bodies[0].position.x; var y0 = Bodies[0].position.y; for(i1 in Bodies) { Bodies[i1].position.x -= x0; Bodies[i1].position.y -= y0; } for(i1 in Bodies) Bodies[i1].Plot(); // for(i1 in Bodies) // { // if(Bodies[i1].Distance(Bodies[0]) > 1000) // { // Bodies[i1].Destroy(); // Bodies[i1] = new Body(new Position(Bodies[0].position.x + Math.random() * 400 - 200, Bodies[0].position.y + Math.random() * 400 - 200), new Velocity(Bodies[0].velocity.x + Math.random() * 20 - 10, Bodies[0].velocity.y + Math.random() * 20 - 10), Math.random() * 100000); // } // } setTimeout(MoveAll, timeScale); } var Bodies = [ new Body(new Position( 0.000000000000000E+00 * AU, 0.000000000000000E+00 * AU, 0.000000000000000E+00 * AU), new Velocity( 0.000000000000000E+00 * AU, 0.000000000000000E+00 * AU, 0.000000000000000E+00 * AU), 1.9891E30 * KG), //SUN new Body(new Position(-1.101694065133387E-02 * AU, -4.627459946836336E-01 * AU, -3.679439537218468E-02 * AU), new Velocity( 2.248379877130665E-02 * AU, 7.741832622393350E-04 * AU, -2.000052808785571E-03 * AU), 3.3020E23 * KG), //MERCURY new Body(new Position( 5.378357762066760E-01 * AU, -4.894694874779497E-01 * AU, -3.773990054258105E-02 * AU), new Velocity( 1.348318340906038E-02 * AU, 1.488292046997766E-02 * AU, -5.745425614074939E-04 * AU), 4.8685E24 * KG), //VENUS new Body(new Position(-1.721041076989816E-01 * AU, 9.681072708702687E-01 * AU, -1.766288579466907E-05 * AU), new Velocity(-1.721235722280550E-02 * AU, -3.078469223808449E-03 * AU, -7.775930922984637E-08 * AU), 5.9736E24 * KG), //EARTH new Body(new Position(-1.711423088734627E-01 * AU, 9.704079886563293E-01 * AU, 1.977776822205624E-04 * AU), new Velocity(-1.776717093979287E-02 * AU, -2.831034837693510E-03 * AU, 1.176624807323343E-05 * AU), 7.3490E22 * KG), //LUNA new Body(new Position(-6.538074989187658E-01 * AU, -1.366082079817985E+00 * AU, -1.256065550271462E-02 * AU), new Velocity( 1.315073618350288E-02 * AU, -4.841752535151738E-03 * AU, -4.244775139504463E-04 * AU), 6.4185E23 * KG), //MARS new Body(new Position(-2.474679193883905E+00 * AU, -4.760524109817909E+00 * AU, 7.513884588477229E-02 * AU), new Velocity( 6.607795008478355E-03 * AU, -3.128453163660247E-03 * AU, -1.348870163475158E-04 * AU), 1.8986E27 * KG), //JUPITER new Body(new Position(-7.036502535576534E+00 * AU, 5.889824512924870E+00 * AU, 1.774740254592805E-01 * AU), new Velocity(-3.875709133420537E-03 * AU, -4.293214645210232E-03 * AU, 2.289367285693264E-04 * AU), 5.6846E26 * KG), //SATURN new Body(new Position( 1.929821276495023E+01 * AU, -5.567836856202042E+00 * AU, -2.706233576472611E-01 * AU), new Velocity( 1.066081900379735E-03 * AU, 3.592026863127670E-03 * AU, -4.155804968605364E-07 * AU), 8.6832E25 * KG), //URANUS new Body(new Position( 2.273883666063009E+01 * AU, -1.964575868056128E+01 * AU, -1.194993987923258E-01 * AU), new Velocity( 2.036182236634615E-03 * AU, 2.389806450959143E-03 * AU, -9.625143587760285E-05 * AU), 1.0243E26 * KG), //NEPTUNE new Body(new Position(-1.879727866016690E+00 * AU, -3.092476101835807E+01 * AU, 3.851834990016669E+00 * AU), new Velocity( 3.191271345796025E-03 * AU, -7.835406400933414E-04 * AU, -8.486375416093025E-04 * AU), 1.3140E22 * KG), //PLUTO new Body(new Position(-1.879812026227606E+00 * AU, -3.092479340918641E+01 * AU, 3.851930281176825E+00 * AU), new Velocity( 3.237429931649916E-03 * AU, -6.880690707329203E-04 * AU, -7.757127200856482E-04 * AU), 1.9000E21 * KG), //CHARON //new Body(new Position( 1.000000000000000E+01 * AU, 3.000000000000000E+00 * AU, 0.000000000000000E+00 * AU), new Velocity(-1.000000000000000E-01 * AU, 0.000000000000000E-00 * AU, -0.000000000000000E-00 * AU), 1.0000E31 * KG), //DEATH! ] setTimeout(MoveAll, 1); </script> </html>