SolarSystemApp = function() 4 { } SolarSystemApp.prototype = SolarSystemApp.prototype.init = function(container) 14 { Sim.App.prototype.init.call(this, container); .planets = [];.orbits = [];.lastX = 0; 22 this.lastY = 0; 23 this.mouseDown = false; .currentlyPressedKeys=[];sun = sun.init();.addObject(sun);stars = stars.init(Sun.SIZE_IN_EARTHS + SolarSystemApp.EARTH_DISTANCE * SolarSystemApp.PLUTO_DISTANCE_IN_EARTHS); 37 this.addObject(stars); .createPlanets();.camera.position.set(0, 0, Sun.SIZE_IN_EARTHS * 8);amb = .scene.add(amb); .root.rotation.x = Math.PI / 8; } SolarSystemApp.prototype.handleMouseMove = function(x, y) 58 { { (Math.abs(dx) > SolarSystemApp.MOUSE_MOVE_TOLERANCE) { } dy = y - this.lastY; 71 if (Math.abs(dy) > SolarSystemApp.MOUSE_MOVE_TOLERANCE) 72 { 73 this.root.rotation.x += (dy * 0.01); (this.root.rotation.x < 0) 77 this.root.rotation.x = 0; (.root.rotation.x = SolarSystemApp.MAX_ROTATION_X; 81 82 } 83 this.lastY = y; 84 85 } 86 } 87 88 SolarSystemApp.prototype.handleMouseDown = function(x, y) 89 { 90 this.lastX = x; 91 this.lastY = y; 92 this.mouseDown = true; 93 } 94 95 SolarSystemApp.prototype.handleMouseUp = function(x, y) 96 { 97 this.lastX = x; 98 this.lastY = y; 99 this.mouseDown = false; 100 } 101 102 SolarSystemApp.prototype.handleMouseScroll = function(delta) dx = delta; .camera.position.z -= dx*10; (this.camera.position.z < SolarSystemApp.MIN_CAMERA_Z) 110 this.camera.position.z = SolarSystemApp.MIN_CAMERA_Z; 111 if (this.camera.position.z > SolarSystemApp.MAX_CAMERA_Z) 112 this.camera.position.z = SolarSystemApp.MAX_CAMERA_Z; 113 } SolarSystemApp.prototype.handleKeyDown= function(keyCode,charCode) 116 { 117 this.currentlyPressedKeys[keyCode] = true; 118 } 119 SolarSystemApp.prototype.handleKeyUp= function(keyCode,charCode) 120 { 121 this.currentlyPressedKeys[keyCode] = false; 122 } 123 124 SolarSystemApp.prototype.update = function() 125 { 126 showFocus(); adspeed=0(this.currentlyPressedKeys[65]) { adspeed = -0.009; 132 } else if ( this.currentlyPressedKeys[68]) { adspeed = +0.009; 135 } (this.currentlyPressedKeys[87]) { speed = 0.003; 140 } else if ( this.currentlyPressedKeys[83]) { speed = -0.003; 143 } 144 var timeNow = new Date().getTime(); 145 if (this.lastTime != 0) { 146 var elapsed = timeNow - this.lastTime; (speed != 0||adspeed!=0) { 149 this.camera.position.x+=adspeed* elapsed; 150 this.camera.position.y+=speed* elapsed; 151 } } 155 this.lastTime = timeNow; } SolarSystemApp.prototype.createPlanets = function () 161 { 162 var i, len = SolarSystemApp.planet_specs.length; 163 for (i = 0; i < len; i++) 164 { planet = spec.type ? planet.init({animateOrbit:true, animateRotation: true, showOrbit:true, 169 distance:spec.distance * SolarSystemApp.EARTH_DISTANCE + Sun.SIZE_IN_EARTHS, 170 size:spec.size * SolarSystemApp.EXAGGERATED_PLANET_SCALE, 171 period : spec.period, 172 revolutionSpeed : 0.002, 173 map : spec.map}); 174 this.addObject(planet); 175 this.planets.push(planet); orbit = orbit.init(spec.distance * SolarSystemApp.EARTH_DISTANCE + Sun.SIZE_IN_EARTHS); 179 this.addObject(orbit); 180 this.orbits.push(orbit); 181 } 182 } 183 184 SolarSystemApp.MOUSE_MOVE_TOLERANCE = 4; 185 SolarSystemApp.MAX_ROTATION_X = Math.PI / 2; 186 SolarSystemApp.MAX_CAMERA_Z = Sun.SIZE_IN_EARTHS * 50; SolarSystemApp.EARTH_DISTANCE = 50; 189 SolarSystemApp.PLUTO_DISTANCE_IN_EARTHS = 77.2; 190 SolarSystemApp.EARTH_DISTANCE_SQUARED = 45000; 191 SolarSystemApp.EXAGGERATED_PLANET_SCALE = 5.55; 192 SolarSystemApp.planet_specs = [ { size : 1 / 2.54, distance : 0.4, period : 0.24, map : "../IMAGE/SOLAR/Mercury.jpg" }, { size : 1 / 1.05, distance : 0.7, period : 0.62, map : "../IMAGE/SOLAR/venus.jpg" }, { type : Earth, size : 1 , distance : 1, period : 1, map : "../IMAGE/SOLAR/earth_surface_2048.jpg" }, { size : 1 / 1.88, distance : 1.6, period : 1.88, map : "../IMAGE/SOLAR/MarsV3-Shaded-2k.jpg" }, { size : 11.1, distance : 5.2, period : 11.86, map : "../IMAGE/SOLAR/realj2k.jpg" }, { type : Saturn, size : 9.41, distance : 10, period : 29.46, map : "../IMAGE/SOLAR/saturn_bjoernjonsson.jpg" }, { size : 4, distance : 19.6, period : 84.01, map : "../IMAGE/SOLAR/uranus.jpg" }, { size : 3.88, distance : 38.8, period : 164.8, map : "../IMAGE/SOLAR/neptune.jpg" }, { size : 10 / 5.55, distance : 77.2, period : 247.7, map : "../IMAGE/SOLAR/pluto.jpg" }, 212 ];
接下来是Sim.Object的示例代码: