[英]Rotating comet around planet
I'm trying to make a simple game. 我正在尝试制作一个简单的游戏。 Tell me how to make object (comet) fly and rotate around orbit of another object (planet). 告诉我如何使物体(彗星)飞行并围绕另一个物体(行星)的轨道旋转。 I could only make the comet turn and fly toward the planet, but it is necessary that when a certain distance is reached, the comet starts to rotate around the planet's orbit. 我只能让彗星转身飞向行星,但是有必要在到达一定距离后,彗星开始围绕行星的轨道旋转。
this.mesh.translateZ(this.speed);
if (this.target.position.distanceTo(this.mesh.position) >= 100) {
let targetQuaternion = new THREE.Quaternion();
let rotationMatrix = new THREE.Matrix4();
rotationMatrix.lookAt(this.mesh.position, this.target.position, this.mesh.up);
targetQuaternion.setFromRotationMatrix(rotationMatrix);
if (!this.mesh.quaternion.equals(targetQuaternion)) {
let step = 0.01;
this.mesh.quaternion.rotateTowards(targetQuaternion, step);
}
} else {
//how to make a comet(this.mesh) fly around the orbit of the planet(this.target)?
}
The simplest way to make one object orbit another is to utilize parent-child relationships. 使一个物体绕另一轨道运行的最简单方法是利用父子关系。 If you add a child to an object it shares a transformation, so when the parent is rotated the child is rotated with it - and when the child has a large translation offset, it effectively "orbits" the parent. 如果将子项添加到对象,则该子项共享一个转换,因此,旋转父项时,子项也随之旋转-并且当子项具有较大的平移偏移量时,它有效地“环绕”父项。
Very simple example of this: 这样的简单示例:
init() {
this.planet = new Mesh(new SphereBufferGeometry(100))
this.satellite = new Mesh(new SphereBufferGeometry(5))
this.satelliteCenter = new Object3D()
this.satellite.position.set(1000,0,0)
this.satelliteCenter.add(this.satellite)
this.scene.add(this.planet)
this.scene.add(this.satelliteCenter)
}
update() {
this.satelliteCenter.rotateY(Math.PI / 2000) // one full revolution every 1000 frames
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.