Javascript Three.js围绕轴心向组添加旋转
我正在尝试使用Three.js构建一个魔方。为了旋转一面,我把所有需要旋转的小立方体放在一组中,然后旋转它。 为了指定一个转折点,我使用我发现的这个函数:Javascript Three.js围绕轴心向组添加旋转,javascript,three.js,rotation,Javascript,Three.js,Rotation,我正在尝试使用Three.js构建一个魔方。为了旋转一面,我把所有需要旋转的小立方体放在一组中,然后旋转它。 为了指定一个转折点,我使用我发现的这个函数: //Turn Group Around Center function THREE.Object3D.prototype.rotateAroundWorldAxis = function () { // rotate object around axis in world space (the axis
//Turn Group Around Center function
THREE.Object3D.prototype.rotateAroundWorldAxis = function () {
// rotate object around axis in world space (the axis passes through point)
// axis is assumed to be normalized
// assumes object does not have a rotated parent
var q = new THREE.Quaternion();
return function rotateAroundWorldAxis(point, axis, angle) {
q.setFromAxisAngle(axis, angle);
this.applyQuaternion(q);
this.position.sub(point);
this.position.applyQuaternion(q);
this.position.add(point);
return this;
}
}();
当我使用它旋转组一次时,一切正常,但当我使用它两次时:
myGroup.rotateAroundWorldAxis(rotationPoint, zAxis, Math.PI / 2);
myGroup.rotateAroundWorldAxis(rotationPoint, zAxis, Math.PI / 2);
第二次转弯时什么也没发生
我猜这是因为函数只对未翻转的对象应用旋转,而不是添加旋转。
(旋转=90,而不是旋转+=90)
我的问题:我如何解决这个问题,让函数记住之前的旋转 我不确定我是否完全理解您的问题,但我建议使用,它简化了许多旋转 如果你想要一个简单的例子,而不是doc,我已经将一个小图图编码为three.js(以可视化所有相机参数),其中我代表了一个小太阳系,在这个小太阳系中,我在不同的轨道上围绕太阳旋转不同的行星 您可以将其可视化,并托管代码。我要做的是,在
main.html
中,我为我的行星使用以下动画功能:
function animatePlanets() {
requestAnimationFrame(animatePlanets);
var quaternion, rotationSpeed, rotationSpeedMoon, pivotPoint, pivotPointMoon, rotationAxeMoon, subgroup, planetPosition, planet
for ( var i = 0; i < views.length; ++ i ) {
var view = views[ i ];
var camera = view.camera;
for (let i=1;i<group.children.length;i++){
// Rotate the planet around the sun
pivotPoint = group.children[i]
planetPosition = pivotPoint.children[0].position
// Defining the rotation axe and speed
rotationAxe = (new THREE.Vector3(-planetPosition.y,planetPosition.x,0)).normalize()
rotationSpeed = pivotPoint.speed
// Applying the rotation to the planet
quaternion = new THREE.Quaternion().setFromAxisAngle(rotationAxe,rotationSpeed)
pivotPoint.applyQuaternion(quaternion)
// Applying local rotation to the planet if not satellites
if (pivotPoint.children[0].children.length == 0){
planet = pivotPoint.children[0]
quaternion = new THREE.Quaternion().setFromAxisAngle(rotationAxe,rotationSpeed*10)
planet.applyQuaternion(quaternion)
}
// Render the scene, using the camera specifications
renderer.render(scene, view.camera);
}
}
函数animatePlanets(){
requestAnimationFrame(animatePlanets);
变量四元数,旋转速度,旋转速度月亮,枢轴点,枢轴点月亮,旋转月亮,子组,行星位置,行星
对于(变量i=0;i