最简单(虽然在科学上不准确)的做法是将 Math.cos/sin 与更新角度值配对使用。框架更新中的类似内容:
earthOrbitAngle += earthOrbitSpeed; //advance angle in degrees
var orbitAngleInRadians = earthOrbitAngle * Math.PI / 180; //convert to radians
//update position of earth...
earth.position.x = Math.cos(orbitAngleInRadians) * earthOrbitRadius;
earth.position.z = Math.sin(orbitAngleInRadians) * earthOrbitRadius;
您可以通过将 earthOrbitRadius 乘以某个因子来拉长 x 或 z 值以使轨道呈椭圆形。
你可以通过设置它的 rotation.z 来倾斜地球。但是,如果您想让月球绕倾斜轴运行,那么最简单的方法是创建一个空的 Object3D 容器来容纳这两个容器,并让月球运行自己的轨道运行脚本。然后,您围绕太阳为该容器设置动画。所以设置看起来像这样:
theSun = new THREE.Mesh(new THREE.SphereGeometry(30, 16, 15), new THREE.MeshBasicMaterial({
color: 'yellow'
}));
scene.add(theSun);
theEarthAndMoon = new THREE.Object3D();
theEarthAndMoon.rotation.z = 23.439281 * Math.PI / 180; //tilt of earth in radians;
scene.add(theEarthAndMoon);
theEarth = new THREE.Mesh(new THREE.SphereGeometry(5, 16, 15), new THREE.MeshLambertMaterial({
color:"blue"
}));
theEarthAndMoon.add(theEarth);
theMoon = new THREE.Mesh(new THREE.SphereGeometry(1, 16, 15), new THREE.MeshLambertMaterial({
color:"white"
}));
theEarthAndMoon.add(theMoon);
在帧更新中:
//run the earth's orbit around the Sun
earthOrbitAngle += earthOrbitSpeed;
var radians = earthOrbitAngle * Math.PI / 180;
theEarthAndMoon.position.x = Math.cos(radians) * earthOrbitRadius;
theEarthAndMoon.position.z = Math.sin(radians) * earthOrbitRadius;
//run the Moon's orbit around the Earth
moonOrbitAngle += moonOrbitSpeed;
var moonRadians = moonOrbitAngle * Math.PI / 180;
theMoon.position.x = Math.cos(moonRadians) * moonOrbitRadius;
theMoon.position.z = Math.sin(moonRadians) * moonOrbitRadius;
你可以看到它在这里运行:Simple Sun, Earth, and Moon at JSFiddle
现在,如果您想深入了解精确轨道力学的兔子洞,我建议您从查看这个令人难以置信的 Three.js 模拟所有行星和 600,000 颗小行星的引擎盖开始:Asterank project